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.*;
import java.util.*;

public class A implements Runnable {
	public static void main (String[] args) {new Thread(null, new A(), "_cf", 1 << 28).start();}

	public void run() {
		FastScanner fs = new FastScanner();
		PrintWriter out = new PrintWriter(System.out);
		System.err.println("");

		int MOD = fs.nextInt();
		int[] a = fs.nextIntArray(4);
		int[] b = fs.nextIntArray(4);
		int[][] r1 = doit(a[0], a[1], a[2], a[3], MOD);
		int[][] r2 = doit(b[0], b[1], b[2], b[3], MOD);
		
		if(r1[0][a[1]] == -1 || r2[0][b[1]] == -1) exit(-1);
		
		
		if(r1[1][0] == -1 && r2[1][0] == -1) {
			int v = -1;
			if(r1[0][a[1]] == r2[0][b[1]]) v = r1[0][a[1]];
			exit(v);
		}
		if(r1[1][0] == -1) {
			long t1 = r1[0][a[1]]-r2[0][b[1]];
			int v = -1;
			if(t1 % r2[1][0] == 0 && t1/r2[1][0] >= 0) {
				v = r1[0][a[1]];
			}
			exit(v);
		}
		if(r2[1][0] == -1) {
			long t1 = r2[0][b[1]]-r1[0][a[1]];
			int v = -1;
			if(t1 % r1[1][0] == 0 && t1/r1[1][0] >= 0) {
				v = r2[0][b[1]];
			}
			exit(v);
		}
		
		int cur2 = b[0];
		for(int i = 0; i < r1[0][a[1]]; i++) {
			long temp = (long)cur2 * b[2];
			temp += b[3]; temp %= MOD;
			cur2 = (int)temp;
		}
		
		if(cur2 == b[1]) {
			exit(r1[0][a[1]]);
		}
		
		
		int[] aPow = new int[MOD+10];
		int[] aSum = new int[MOD+10];
		aPow[0] = 1;
		aSum[0] = aPow[0];
		for(int i = 1; i < aPow.length; i++) {
			long temp = aPow[i-1];
			temp *= b[2];
			temp %= MOD;
			aPow[i] = (int)temp;
			aSum[i] = (aSum[i-1]+aPow[i]) % MOD;
		}
		
		
		int cnts = -1;
		for(int i = 1; i <= MOD; i++) {
			int next = eval(r1[1][0]+1, cur2, aPow, aSum, b[2], b[3], MOD);
			if(next == b[1]) {
				cnts = i;
				break;
			}
			cur2 = next;
		}
		if(cnts == -1) exit(-1);
		else {
			System.out.println((long)r1[1][0]*cnts + r1[0][a[1]]);
		}
		
		out.close();
	}
	
	
	int eval(int i, int base, int[] aPow, int[] aSum, int a, int b, int MOD) {
		if(i == 1) return base;
		if(i == 2) {
			long res = (long)a*base + b;
			return (int)(res%MOD);
		}
		long res = (long)aPow[i-1]*base;
		res %= MOD;
		long t1 = (long)b*aSum[i-2];
		t1 %= MOD;
		res += t1;
		return (int)(res%MOD);
	}
	
	void exit(int v) {
		System.out.println(v);
		System.exit(0);
	}
	
	int[][] doit(int h1, int a1, long x1, long y1, int MOD) {
		int[][] res = new int[2][];
		res[1] = new int[1]; res[1][0] = -1;
		res[0] = new int[MOD]; Arrays.fill(res[0], -1);
		
		int startCycle = -1, cycleLen = 0;
		for(int time = 0, curHeight = h1; ; time++) {
			if(res[0][curHeight] == -1) {
				res[0][curHeight] = time;
			}
			else {
				startCycle = curHeight;
				cycleLen = time-res[0][curHeight];
				break;
			}
			long temp = x1*curHeight + y1;
			if(temp >= MOD) temp %= MOD;
			curHeight = (int)temp;
		}

		int curHeight = startCycle;
		for(int i = 0; i <= cycleLen; i++) {
			if(curHeight == a1) {
				res[1][0] = cycleLen;
				break;
			}
			long temp = x1*curHeight + y1;
			if(temp >= MOD) temp %= MOD;
			curHeight = (int)temp;
		}
		
		return res;
	}

	class FastScanner {
		public int BS = 1<<16;
		public char NC = (char)0;
		byte[] buf = new byte[BS];
		int bId = 0, size = 0;
		char c = NC;
		double num = 1;
		BufferedInputStream in;

		public FastScanner() {
			in = new BufferedInputStream(System.in, BS);
		}

		public FastScanner(String s) {
			try {
				in = new BufferedInputStream(new FileInputStream(new File(s)), BS);
			}
			catch (Exception e) {
				in = new BufferedInputStream(System.in, BS);
			}
		}

		public char nextChar(){
			while(bId==size) {
				try {
					size = in.read(buf);
				}catch(Exception e) {
					return NC;
				}                
				if(size==-1)return NC;
				bId=0;
			}
			return (char)buf[bId++];
		}

		public int nextInt() {
			return (int)nextLong();
		}

		public long nextLong() {
			num=1;
			boolean neg = false;
			if(c==NC)c=nextChar();
			for(;(c<'0' || c>'9'); c = nextChar()) {
				if(c=='-')neg=true;
			}
			long res = 0;
			for(; c>='0' && c <='9'; c=nextChar()) {
				res = (res<<3)+(res<<1)+c-'0';
				num*=10;
			}
			return neg?-res:res;
		}

		public double nextDouble() {
			double cur = nextLong();
			return c!='.' ? cur:cur+nextLong()/num;
		}

		public String next() {
			StringBuilder res = new StringBuilder();
			while(c<=32)c=nextChar();
			while(c>32) {
				res.append(c);
				c=nextChar();
			}
			return res.toString();
		}

		public String nextLine() {
			StringBuilder res = new StringBuilder();
			while(c<=32)c=nextChar();
			while(c!='\n') {
				res.append(c);
				c=nextChar();
			}
			return res.toString();
		}

		public boolean hasNext() {
			if(c>32)return true;
			while(true) {
				c=nextChar();
				if(c==NC)return false;
				else if(c>32)return true;
			}
		}
		
		public int[] nextIntArray(int n) {
			int[] res = new int[n];
			for(int i = 0; i < n; i++) res[i] = nextInt();
			return res;
		}
		
	}

}