Mike has a frog and a flower. His frog is named Xaniar and his flower is named Abol. Initially(at time 0), height of Xaniar is h1 and height of Abol is h2. Each second, Mike waters Abol and Xaniar.

<image>

So, if height of Xaniar is h1 and height of Abol is h2, after one second height of Xaniar will become <image> and height of Abol will become <image> where x1, y1, x2 and y2 are some integer numbers and <image> denotes the remainder of a modulo b.

Mike is a competitive programmer fan. He wants to know the minimum time it takes until height of Xania is a1 and height of Abol is a2.

Mike has asked you for your help. Calculate the minimum time or say it will never happen.

Input

The first line of input contains integer m (2 ≤ m ≤ 106).

The second line of input contains integers h1 and a1 (0 ≤ h1, a1 < m).

The third line of input contains integers x1 and y1 (0 ≤ x1, y1 < m).

The fourth line of input contains integers h2 and a2 (0 ≤ h2, a2 < m).

The fifth line of input contains integers x2 and y2 (0 ≤ x2, y2 < m).

It is guaranteed that h1 ≠ a1 and h2 ≠ a2.

Output

Print the minimum number of seconds until Xaniar reaches height a1 and Abol reaches height a2 or print -1 otherwise.

Examples

Input

5
4 2
1 1
0 1
2 3


Output

3


Input

1023
1 2
1 0
1 2
1 1


Output

-1

Note

In the first sample, heights sequences are following:

Xaniar: <image>

Abol: <image>
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.InputMismatchException;

public class Main {

	public static void main(String[] args) {
		new Main().run();
	}

	void run() {
		InputReader in = new InputReader(System.in);
		PrintWriter out = new PrintWriter(System.out);
		int m = in.nextInt();
		int h1 = in.nextInt();
		int a1 = in.nextInt();
		int x1 = in.nextInt();
		int y1 = in.nextInt();
		int h2 = in.nextInt();
		int a2 = in.nextInt();
		int x2 = in.nextInt();
		int y2 = in.nextInt();
		int used1[] = new int[m];
		int used2[] = new int[m];
		int th1 = h1;
		int th2 = h2;
		Arrays.fill(used1, -1);
		Arrays.fill(used2, -1);
		boolean roop1 = false, roop2 = false;
		long res = -1;
		int i = 0, k1 = -1, k2 = -1;
		while (!roop1 || !roop2) {
			if (a1 == h1 && a2 == h2) {
				res = i;
				break;
			}
			used1[h1] = used2[h2] = i;
			h1 = next(h1, x1, y1, m);
			h2 = next(h2, x2, y2, m);
			if (used1[h1] != -1) {
				if (used1[a1] == -1 || used1[a1] < used1[h1])
					break;
				roop1 = true;
				k1 = i - used1[h1] + 1;
			}
			if (used2[h2] != -1) {
				if (used2[a2] == -1 || used2[a2] < used2[h2])
					break;
				roop2 = true;
				k2 = i - used2[h2] + 1;
			}
			i++;
		}
		if (roop1 && roop2) {
			int m1 = used1[a1];
			int m2 = used2[a2];
			long y = 0, max = 10000000;
			while ((m2 + y * k2 - m1) % k1 != 0 && y < max)
				++y;
			if (y < max) {
				res = m2 + y * k2;
				// out.println(m2 + " " + y + " " + k2 + " " + res);
			}
		}
		// out.println(used1[a1] + " " + k1 + " " + used2[a2] + " " + k2);
		//		if (res >= 0) {
		//			for (int a = 0; a <= res; ++a) {
		//				out.println(a + " " + th1 + " " + th2);
		//				th1 = next(th1, x1, y1, m);
		//				th2 = next(th2, x2, y2, m);
		//			}
		//		}
		out.println(res);
		out.flush();
	}

	private static final int next(int h, int x, int y, int m) {
		return (int) (((long) x * h + y) % m);
	}

	public static class SegmentTreeRMQ {
		public int M, H, N;
		public int[] st;

		public SegmentTreeRMQ(int n) {
			N = n;
			M = Integer.highestOneBit(Math.max(N - 1, 1)) << 2;
			H = M >>> 1;
			st = new int[M];
			Arrays.fill(st, 0, M, Integer.MAX_VALUE);
		}

		public SegmentTreeRMQ(int[] a) {
			N = a.length;
			M = Integer.highestOneBit(Math.max(N - 1, 1)) << 2;
			H = M >>> 1;
			st = new int[M];
			for (int i = 0; i < N; i++) {
				st[H + i] = a[i];
			}
			Arrays.fill(st, H + N, M, Integer.MAX_VALUE);
			for (int i = H - 1; i >= 1; i--)
				propagate(i);
		}

		public void update(int pos, int x) {
			st[H + pos] = x;
			for (int i = (H + pos) >>> 1; i >= 1; i >>>= 1)
				propagate(i);
		}

		private void propagate(int i) {
			st[i] = Math.min(st[2 * i], st[2 * i + 1]);
		}

		public int min(int l, int r) {
			return l >= r ? 0 : min(l, r, 0, H, 1);
		}

		private int min(int l, int r, int cl, int cr, int cur) {
			if (l <= cl && cr <= r) {
				return st[cur];
			} else {
				int mid = cl + cr >>> 1;
				int ret = Integer.MAX_VALUE;
				if (cl < r && l < mid) {
					ret = Math.min(ret, min(l, r, cl, mid, 2 * cur));
				}
				if (mid < r && l < cr) {
					ret = Math.min(ret, min(l, r, mid, cr, 2 * cur + 1));
				}
				return ret;
			}
		}

		public int firstle(int l, int v) {
			int cur = H + l;
			while (true) {
				if (st[cur] <= v) {
					if (cur < H) {
						cur = 2 * cur;
					} else {
						return cur - H;
					}
				} else {
					cur++;
					if ((cur & cur - 1) == 0)
						return -1;
					if ((cur & 1) == 0)
						cur >>>= 1;
				}
			}
		}

		public int lastle(int l, int v) {
			int cur = H + l;
			while (true) {
				if (st[cur] <= v) {
					if (cur < H) {
						cur = 2 * cur + 1;
					} else {
						return cur - H;
					}
				} else {
					if ((cur & cur - 1) == 0)
						return -1;
					cur--;
					if ((cur & 1) == 1)
						cur >>>= 1;
				}
			}
		}
	}

	static class InputReader {
		private InputStream stream;
		private byte[] buf = new byte[1024];
		private int curChar;
		private int numChars;
		private SpaceCharFilter filter;

		public InputReader(InputStream stream) {
			this.stream = stream;
		}

		public InputReader(String input) {
			this.stream = new ByteArrayInputStream(input.getBytes());
		}

		public int next() {
			if (numChars == -1)
				throw new InputMismatchException();
			if (curChar >= numChars) {
				curChar = 0;
				try {
					numChars = stream.read(buf);
				} catch (IOException e) {
					throw new InputMismatchException();
				}
				if (numChars <= 0)
					return -1;
			}
			return buf[curChar++];
		}

		public int nextInt() {
			int c = next();
			while (isSpaceChar(c))
				c = next();
			int sgn = 1;
			if (c == '-') {
				sgn = -1;
				c = next();
			}
			int res = 0;
			do {
				if (c < '0' || c > '9')
					throw new InputMismatchException();
				res *= 10;
				res += c - '0';
				c = next();
			} while (!isSpaceChar(c));
			return res * sgn;
		}

		public long nextLong() {
			int c = next();
			while (isSpaceChar(c))
				c = next();
			long sgn = 1;
			if (c == '-') {
				sgn = -1;
				c = next();
			}
			long res = 0;
			do {
				if (c < '0' || c > '9')
					throw new InputMismatchException();
				res *= 10;
				res += c - '0';
				c = next();
			} while (!isSpaceChar(c));
			return res * sgn;
		}

		public boolean isSpaceChar(int c) {
			if (filter != null)
				return filter.isSpaceChar(c);
			return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
		}

		public interface SpaceCharFilter {
			public boolean isSpaceChar(int ch);
		}
	}
}
