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.IOException;
import java.util.Arrays;
import java.util.InputMismatchException;

public class MikeAndFrog {
    public static void main(String[] args) {
        FasterScanner sc = new FasterScanner();
        
        int M = sc.nextInt();
        int H1 = sc.nextInt();
        int A1 = sc.nextInt();
        int X1 = sc.nextInt();
        int Y1 = sc.nextInt();
        int H2 = sc.nextInt();
        int A2 = sc.nextInt();
        int X2 = sc.nextInt();
        int Y2 = sc.nextInt();
        
        Graph graph1 = makeGraph(H1, X1, Y1, M);
        Graph graph2 = makeGraph(H2, X2, Y2, M);
        
//        graph1 = new Graph(new long[] { 1, 1, 1, 1, 1, 1 }, new long[] { 1, 1, 1, 7, 1, 1 )
        
        if (graph1.D[A1] < 0 || graph2.D[A2] < 0) {
        	System.out.println(-1);
        	return;
        }
        
        long d1 = graph1.D[A1];
        long d2 = graph2.D[A2];
        
//        System.out.format("g1 loop = %d, g1 len = %d, g1 init = %d, g1 looplen = %d\ng2 loop = %d, g2 len = %d, g2 init = %d, g2 looplen = %d\n", graph1.loopIdx, graph1.length, graph1.initLen(), graph1.loopLen(), graph2.loopIdx, graph2.length, graph2.initLen(), graph2.loopLen());
//        System.out.format("d1 = %d, d2 = %d\n", d1, d2);
        
      if (d1 < graph1.initLen() && d2 < graph2.initLen()) {
    	if (d1 == d2) {
    		System.out.println(d1);
    	} else {
    		System.out.println(-1);
    	}
    } else if (d1 < graph1.initLen() && d2 >= graph2.initLen()) {
    	if (graph2.getNum(d1) == A2) {
    		System.out.println(d1);
    	} else {
    		System.out.println(-1);
    	}
    } else if (d1 >= graph1.initLen() && d2 < graph2.initLen()) {
    	if (graph1.getNum(d2) == A1) {
    		System.out.println(d2);
    	} else {
    		System.out.println(-1);
    	}
    } else {
    	long ans = Long.MAX_VALUE;
    	for (long i = 0; i <= M; i++) {
    		long idx1 = d1 + i * graph1.loopLen();
    		if (graph2.getNum(idx1) == A2) {
    			ans = Math.min(ans, idx1);
    		}
    		long idx2 = d2 + i * graph2.loopLen();
    		if (graph1.getNum(idx2) == A1) {
    			ans = Math.min(ans, idx2);
    		}
    	}
    	System.out.println((ans == Long.MAX_VALUE) ? -1 : ans);
    }
        
//        System.out.format("g1 loop = %d, g1 len = %d, g1 init = %d, g1 looplen = %d\ng2 loop = %d, g2 len = %d, g2 init = %d, g2 looplen = %d\n", graph1.loopIdx, graph1.length, graph1.initLen(), graph1.loopLen(), graph2.loopIdx, graph2.length, graph2.initLen(), graph2.loopLen());
        
//        if (d1 < graph1.loopLen() && d2 < graph2.loopLen()) {
//        	if (d1 == d2) {
//        		System.out.println(d1);
//        	} else {
//        		System.out.println(-1);
//        	}
//        } else if (d1 < graph1.loopLen() && d2 >= graph2.loopLen()) {
//        	for (long i = 0; i <= M; i++) {
//        		if (d1 == d2 + i * graph2.loopLen()) {
//        			System.out.println(d1);
//        			return;
//        		}
//        	}
//        	System.out.println(-1);
//        } else if (d1 >= graph1.loopLen() && d2 < graph2.loopLen()) {
//        	for (long i = 0; i <= M; i++) {
//        		if (d1 + i * graph1.loopLen() == d2) {
//        			System.out.println(d2);
//        			return;
//        		}
//        	}
//        	System.out.println(-1);
//        } else {
//        	long ans = Long.MAX_VALUE;
//        	for (long i = 0; i <= M; i++) {
//        		long tmp1 = d1 + i * graph1.loopLen();
//        		long top1 = tmp1 - d2;
//        		if (top1 >= 0 && top1 % graph2.loopLen() == 0) {
//        			ans = Math.min(ans, tmp1);
//        		}
//        		long tmp2 = d2 + i * graph2.loopLen();
//        		long top2 = tmp2 - d1;
//        		if (top2 >= 0 && top2 % graph1.loopLen() == 0) {
//        			ans = Math.min(ans, tmp2);
//        		}
////        		long tmp2 = d2 + i * graph2.loopLen();
////        		if (tmp1 == tmp2) {
////        			System.out.println(tmp1);
////        			return;
////        		}
//        	}
//        	System.out.println((ans == Long.MAX_VALUE) ? -1 : ans);
//        }
    }
    
    public static Graph makeGraph(int H, int X, int Y, int M) {
    	long[] D = new long[M];
    	long[] N = new long[M];
    	Arrays.fill(D, -1);
    	Arrays.fill(N, -1);
    	int d = 0;
    	while (D[H] == -1) {
//    		System.out.print(H + " -> ");
    		int next = (int)(((long)X * H + Y) % M);
    		D[H] = d;
    		N[d] = H;
    		H = next;
    		d++;
    	}
//    	System.out.println(H);
    	return new Graph(D, N, H, d);
    }
    
    public static class Graph {
    	long[] D;
    	long[] N;
    	int loopIdx;
    	long length;
    	
    	public Graph(long[] d, long[] n, int lp, long len) {
    		this.D = d;
    		this.N = n;
    		this.loopIdx = lp;
    		this.length = len;
    	}
    	
    	public long initLen() {
    		return this.D[loopIdx];
    	}
    	
    	public long loopLen() {
    		return this.length - initLen();
    	}
    	
    	public long getNum(long idx) {
    		if (idx < initLen()) {
    			return N[(int)idx];
    		} else {
    			int newIdx = (int)(((idx - initLen()) % loopLen()) + initLen());
    			return N[newIdx];
    		}
    	}
    }
    
	public static class FasterScanner {
		private byte[] buf = new byte[1024];
		private int curChar;
		private int numChars;

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

		public String nextLine() {
			int c = read();
			while (isSpaceChar(c))
				c = read();
			StringBuilder res = new StringBuilder();
			do {
				res.appendCodePoint(c);
				c = read();
			} while (!isEndOfLine(c));
			return res.toString();
		}

		public String nextString() {
			int c = read();
			while (isSpaceChar(c))
				c = read();
			StringBuilder res = new StringBuilder();
			do {
				res.appendCodePoint(c);
				c = read();
			} while (!isSpaceChar(c));
			return res.toString();
		}

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

		public int nextInt() {
			int c = read();
			while (isSpaceChar(c))
				c = read();
			int sgn = 1;
			if (c == '-') {
				sgn = -1;
				c = read();
			}
			int res = 0;
			do {
				if (c < '0' || c > '9')
					throw new InputMismatchException();
				res *= 10;
				res += c - '0';
				c = read();
			} while (!isSpaceChar(c));
			return res * sgn;
		}
	        
	    public int[] nextIntArray(int n) {
	        int[] arr = new int[n];
	        for (int i = 0; i < n; i++) {
	            arr[i] = nextInt();
	        }
	        return arr;
	    }
        
		public long[] nextLongArray(int n) {
		    long[] arr = new long[n];
		    for (int i = 0; i < n; i++) {
		        arr[i] = nextLong();
		    }
		    return arr;
		}

	    private boolean isSpaceChar(int c) {
			return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
		}

		private boolean isEndOfLine(int c) {
			return c == '\n' || c == '\r' || c == -1;
		}
	}
}