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.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.StringTokenizer;

public class Abood1A {

	static long x, y, g;
	 
	static long gcd(long a, long b) {
		if(a == 0) {
			x = 0;
			y = 1;
			return b;
		}
 
		long g = gcd(b % a, a);
		long x1 = y - (b / a) * x;
		long y1 = x;
		x = x1;
		y = y1;
		return g;
 
	}
 
	static boolean find_any_solution (long a, long b, long c) {
		g = gcd (Math.abs(a), Math.abs(b));
		if (c % g != 0)
			return false;
		x *= c / g;
		y *= c / g;
		if (a < 0) 
			x *= -1;
		if (b < 0) 
			y *= -1;
		return true;
	}

	public static void main(String[] args) throws NumberFormatException, IOException {
		Scanner sc = new Scanner(System.in);
		PrintWriter out = new PrintWriter(System.out);


		int m = sc.nextInt();
		long h1 = sc.nextInt();
		int a1 = sc.nextInt();
		int x1 = sc.nextInt();
		int y1 = sc.nextInt();
		long h2 = sc.nextInt();
		int a2 = sc.nextInt();
		int x2 = sc.nextInt();
		int y2 = sc.nextInt();

		int[] time1 = new int[m];
		int[] time2 = new int[m];
		Arrays.fill(time1, -1);
		Arrays.fill(time2, -1);

		time1[(int) h1] = 0;
		time2[(int) h2] = 0;

		int startC1 = -1;
		int endC1 = -1;
		for(int i = 1; ; i++) {
			h1 *= x1;
			h1 += y1;
			h1 %= m;

			if(time1[(int) h1] == -1)
				time1[(int) h1] = i;
			else
			{
				startC1 = time1[(int) h1];
				endC1 = i;
				break;
			}
		}

		int startC2 = -1;
		int endC2 = -1;
		for(int i = 1; ; i++) {
			h2 *= x2;
			h2 += y2;
			h2 %= m;

			if(time2[(int) h2] == -1)
				time2[(int) h2] = i;
			else
			{
				startC2 = time2[(int) h2];
				endC2 = i;
				break;
			}
		}
		
		if(time1[a1] == -1 || time2[a2] == -1)
			out.println(-1);
		else if(time1[a1] == time2[a2])
			out.println(time1[a1]);
		else
			if(time1[a1] < startC1 && time2[a2] < startC2) {
				out.println(-1);
			}else if(time1[a1] < startC1) {
				while(time2[a2] < time1[a1])
					time2[a2] += endC2 - startC2;
				if(time2[a2] == time1[a1])
					out.println(time1[a1]);
				else
					out.println(-1);
			}else if(time2[a2] < startC2){
				while(time1[a1] < time2[a2])
					time1[a1] += endC1 - startC1;
				if(time1[a1] == time2[a2])
					out.println(time1[a1]);
				else
					out.println(-1);
			}else {
				long a = endC1 - startC1;
				long b = -endC2 + startC2;
				long c = time2[a2] - time1[a1];

				if(find_any_solution(a, b, c)){	
					long g = gcd(a, b);
					
					x = x * c / g;
					y = y * c / g;
					
					while(x > 0 || y > 0) {
						x -= Math.abs(b / g);
						y -= Math.abs(a / g);
					}
					
					while(x < 0 || y < 0) {
						x += Math.abs(b / g);
						y += Math.abs(a / g);
					}
					
					long ans = a * x + time1[a1];
					ans = -b * y + time2[a2];
					out.println(ans);
				}else
					out.println(-1);

			}

		out.flush();
		out.close();
	}


	static class Scanner {
		StringTokenizer st;
		BufferedReader br;

		public Scanner(InputStream s) {
			br = new BufferedReader(new InputStreamReader(s));
		}
		public Scanner(String file) throws FileNotFoundException{	br = new BufferedReader(new FileReader(file));}


		public String next() throws IOException {
			while(st == null || !st.hasMoreTokens())
				st = new StringTokenizer(br.readLine());
			return st.nextToken();
		}

		public String nextLine() throws IOException {
			return br.readLine();
		}

		public int nextInt() throws NumberFormatException, IOException {
			return Integer.parseInt(next());
		}
		public long nextLong() throws NumberFormatException, IOException {
			return Long.parseLong(next());
		}

		public boolean ready() throws IOException {
			return br.ready();
		}

		public double nextDouble() throws IOException
		{
			String x = next();
			StringBuilder sb = new StringBuilder("0");
			double res = 0, f = 1;
			boolean dec = false, neg = false;
			int start = 0;
			if(x.charAt(0) == '-')
			{
				neg = true;
				start++;
			}
			for(int i = start; i < x.length(); i++)
				if(x.charAt(i) == '.')
				{
					res = Long.parseLong(sb.toString());
					sb = new StringBuilder("0");
					dec = true;
				}
				else
				{
					sb.append(x.charAt(i));
					if(dec)
						f *= 10;
				}
			res += Long.parseLong(sb.toString()) / f;
			return res * (neg?-1:1);
		}

	}

}

