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.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.StringTokenizer;


public class Main {
	public static int index1 = -1, index2 = -1;
	public static long aa1 = -1;
	public static long aa2 = -1;
	public static long cb1= 0 , cb2 = 0;
	public static long[] fill(long flags[], long h, long x, long y, long m, boolean firsst) {
		flags[(int)h] = 1; long step = 1;
		while (true) {
			h = (x * h + y) % m;
			if (flags[(int)h] == 0) {
				flags[(int)h] = step + 1;
			} else {
				if (firsst) cb1 = flags[(int)h] - 1;
				if (!firsst) cb2 = flags[(int)h] - 1;
				long dist = step - flags[(int)h] + 1;
				long cycle[] = new long[(int)dist];
				cycle[0] = h;
				if (firsst && cycle[0] == aa1) {
					index1 = 0;
				}
				if (!firsst && cycle[0] == aa2) {
					index2 = 0;
				}
				for (int i = 1; i < dist; i++) {
					cycle[i] = (x * cycle[i - 1] + y) % m;
					if (firsst && cycle[i] == aa1) {
						index1 = i;
					}
					if (!firsst && cycle[i] == aa2) {
						index2 = i;
					}
				}
				return cycle;
			}
			step++;
		}
	}
	public static void main(String[] args) {
		int x1, y1, x2, y2, a1, a2, m, h1, h2;
		InputReader in = new InputReader(System.in);
		m = in.nextInt();
		h1 = in.nextInt(); a1 = in.nextInt();	
		x1 = in.nextInt(); y1 = in.nextInt();
		h2 = in.nextInt(); a2 = in.nextInt();	
		x2 = in.nextInt(); y2 = in.nextInt();aa1 = a1;aa2 = a2;
		
		long flags1[] = new long[m];
		long cycle1[] = fill(flags1, h1, x1, y1, m, true);
		long state1 = 0; // not present
		if (flags1[a1] != 0) {
			state1 = 1; // present not in cycle
		}
		if (index1 != -1) state1 = 2;
		long flags2[] = new long[m];
		long cycle2[] = fill(flags2, h2, x2, y2, m, false);
		long state2 = 0; // not present
		if (flags2[a2] != 0) {
			state2 = 1; // present not in cycle
		}
		if (index2 != -1) state2 = 2;
		
		if (state1 * state2 == 0) {
			System.out.println("-1");
			return;
		}
		if (state1 * state2 != 4) {
			long step = 0; boolean f1 = false; boolean f2 = false;
			while (!(f1&&f2)) {
				h1 = (x1 * h1 + y1) % m;
				h2 = (x2 * h2 + y2) % m;
				step++;
				if (h1 == a1 && h2 == a2) {
					System.out.println(step);
					return;
				}
				if (h1 == cycle1[0]) f1 = true;
				if (h2 == cycle2[0]) f2 = true;
			}
			System.out.println(-1);
			return;
		}
		long step = 0;
		step = cb1 + index1;
		h1 = a1;
		int i1 = 0; int i2 = 0;
		//while (cycle1[i1] != h1) i1++; int a1index = i1;
		i1 = index1; int a1index = index1;
		if (step < cb2) {
			i1 = (int) ((i1 + (cb2 - step)) % cycle1.length);
			i2 = 0;
			step = cb2;
		} else {
			i2 = (int) ((step - cb2) % cycle2.length);
		}
		
		int distToa1 = 0;
			if (a1index > i1) {
				distToa1 = a1index - i1;
			} else {
				distToa1 = cycle1.length - i1 + a1index;
			}
			if (distToa1 >= cycle1.length) distToa1 -= cycle1.length;
			i2 = (i2 + distToa1); if (i2 >= cycle2.length) i2 -= cycle2.length;
			i1 = a1index;
			step = step + distToa1;
			/*
			if (cycle1.length < cycle2.length) {
				int tt = a1; a1 = a2; a2 = tt;
				int ttt[] = cycle1; cycle1 = cycle2; cycle2 = ttt;
				tt = i1; i1 = i2; i2 = tt;
			}
			*/
			int it = 1000000;
			while (it > 0 && cycle2[i2] != a2) {
				i2 = (i2 + cycle1.length) % cycle2.length;
				it--;
				step = step + cycle1.length;
			}
			if (cycle2[i2] == a2) {
				System.out.println(step);
			} else {
				System.out.println(-1);
			}
	}
}
class InputReader {
	public BufferedReader reader;
	public StringTokenizer tokenizer;

	public InputReader(InputStream stream) {
		reader = new BufferedReader(new InputStreamReader(stream));
		tokenizer = null;
	}
	public String nextLine() throws IOException {
		return reader.readLine();
	}
	public String next() {
		while (tokenizer == null || !tokenizer.hasMoreTokens()) {
			try {
				tokenizer = new StringTokenizer(reader.readLine());
			} catch (IOException e) {
				throw new RuntimeException(e);
			}
		}
		return tokenizer.nextToken();
	}
	
	public double nextDouble() {
		return Double.parseDouble(next());
	}
	
	public long nextLong() {
		return Long.parseLong(next());
	}

	public int nextInt() {
		return Integer.parseInt(next());
	}
}