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

import static java.lang.Math.*;


public class ProblemA implements Runnable{
	
	Random rnd = new Random();
	
	final boolean DEBUG = false;
	final int ITERATIONS = DEBUG ? 1000 : 1;
	
	void solve() throws IOException {
		int modulo = readInt();
		
		int h1 = readInt();
		int a1 = readInt();
		
		int x1 = readInt();
		int y1 = readInt();
		
		int h2 = readInt();
		int a2 = readInt();
		
		int x2 = readInt();
		int y2 = readInt();
		
		for (int it = 0; it < ITERATIONS; ++it) {
			if (DEBUG) {
				modulo = 1 + rnd.nextInt(10000);

				h1 = rnd.nextInt(modulo);
				a1 = rnd.nextInt(modulo);
				
				x1 = rnd.nextInt(modulo);
				y1 = rnd.nextInt(modulo);
				
				h2 = rnd.nextInt(modulo);
				a2 = rnd.nextInt(modulo);
				
				x2 = rnd.nextInt(modulo);
				y2 = rnd.nextInt(modulo);
			}
			
			long timeFast = solveFast(modulo, h1, a1, x1, y1, h2, a2, x2, y2);
			if (DEBUG) {
				long timeBrute = solveBrute(modulo, h1, a1, x1, y1, h2, a2, x2, y2);

				if (timeFast == -1 && timeBrute != -1 || timeFast > timeBrute) {
					System.err.println(modulo);
					System.err.println(h1 + " " + a1 + " " + x1 + " " + y1);
					System.err.println(h2 + " " + a2 + " " + x2 + " " + y2);
					System.err.println(timeFast + " " + timeBrute);
				}
			}
			
			out.println(timeFast);
		}
	}
	
	private long solveBrute(int modulo, int h1, int a1, int x1, int y1, int h2,
			int a2, int x2, int y2) {
		long cur1 = h1, cur2 = h2;
		
		for (int it = 1; it <= 10 * 1000; ++it) {
			cur1 = cur1 * x1 + y1;
			cur1 %= modulo;
			
			cur2 = cur2 * x2 + y2;
			cur2 %= modulo;
			
			if (cur1 == a1 && cur2 == a2) {
				return it;
			}
		}
		
		return -1;
	}

	private long solveFast(int modulo, int h1, int a1, int x1, int y1, int h2,
			int a2, int x2, int y2) {
		int[] sd1 = getSD(h1, a1, x1, y1, modulo);
		int[] sd2 = getSD(h2, a2, x2, y2, modulo);
		
		int s1 = sd1[0], d1 = sd1[1];
		int s2 = sd2[0], d2 = sd2[1];
		
		if (s1 == -1 || s2 == -1) {
			return -1;
		}
		
		if (s1 == s2) {
			return s1;
		}
		
		if (d1 == -1 && d2 == -1) {
			return -1;
		}
		
		if (d1 == -1) {
			if (s1 < s2 || (s1 - s2) % d2 != 0) {
				return -1;
			} else {
				return s1;
			}
		}
		
		if (d2 == -1) {
			if (s2 < s1 || (s2 - s1) % d1 != 0) {
				return -1;
			} else {
				return s2;
			}
		}
		
//		if (s1 < s2 && (s2 - s1) % d1 == 0) {
//			return s2;
//		}
//		
//		if (s2 < s1 && (s1 - s2) % d2 == 0) {
//			return s1;
//		}
		
//		if (d1 == d2) {
//			return -1;
//		}
		
		long[] gcd = gcd(d1, d2);
		
		long g = gcd[0];
		long i1 = gcd[1];
		long i2 = gcd[2];
		
		if (i1 <= 0) {
			long tmpLong = i2;
			i2 = i1;
			i1 = tmpLong;
			
			int tmp = d1;
			d1 = d2;
			d2 = tmp;
			
			tmp = s1;
			s1 = s2;
			s2 = tmp;
		}
		
		// i1 * d1 + i2 * d2 = s2 - s1
		int delta = s2 - s1;
		if (delta % g != 0) {
			return -1;
		}
		
		i1 *= delta / g;
		i2 *= delta / g;
		
		// i1 must be has sign of delta, i2 must be another sign of delta
		long i1Delta = d2 / g;
		long i2Delta = d1 / g;
		
		long maxI1Decrement = i1 / i1Delta;
		long maxI2Decrement = i2 / i2Delta;
		
		long count = min(abs(maxI1Decrement), abs(maxI2Decrement));
		i1 -= i1Delta * count * (i1 > 0 ? 1 : -1);
		i2 -= i2Delta * count * (i2 > 0 ? 1 : -1);
		
		if (i1 < 0) {
			i1 -= i1Delta * (i1 > 0 ? 1 : -1);
			i2 -= i2Delta * (i2 > 0 ? 1 : -1);
		}
		
		long time = s1 + i1 * d1;
		return time;
	}
	
	private long[] gcd(int a, int b) {
		if (a == 0) {
			return new long[] { b, 0, 1 };
		}
		
		long[] gcd = gcd(b % a, a);
		long x = gcd[1];
		long y = gcd[2];
		
		gcd[1] = y - b / a * x;
		gcd[2] = x;
		
		return gcd;
	}

	private int[] getSD(int h, int a, int x, int y, int modulo) {
		int s = -1;
		int d = -1;
		
		long cur = h;
		for (int i = 1; i <= 2 * modulo && s == -1; ++i) {
			cur = x * cur + y;
			cur %= modulo;
			
			if (cur == a) {
				s = i;
			}
		}

		if (s != -1) {
			for (int i = s + 1; i <= 4 * modulo && d == -1; ++i) {
				cur = x * cur + y;
				cur %= modulo;
				
				if (cur == a) {
					d = i - s;
				}
			}
		}
		
		return new int[] { s, d };
	}

	final boolean ONLINE_JUDGE = System.getProperty("ONLINE_JUDGE") != null;
	
	BufferedReader in;
	PrintWriter out;
	StringTokenizer tok = new StringTokenizer("");
	
	public static void main(String[] args){
		new Thread(null, new ProblemA(), "", 128 * (1L << 20)).start();
	}
	
	void init() throws FileNotFoundException{
		Locale.setDefault(Locale.US);
		
		if (ONLINE_JUDGE){
			in = new BufferedReader(new InputStreamReader(System.in));
			out = new PrintWriter(System.out);
		}else{
			in = new BufferedReader(new FileReader("input.txt"));
			out = new PrintWriter("output.txt");
		}
	}
	
	/////////////////////////////////////////////////////////////////////
	
	public void run(){
		try{
			long timeBegin = System.currentTimeMillis();
			Locale.setDefault(Locale.US);
			
			init();
			solve();
			
			out.close();
			
			long timeEnd = System.currentTimeMillis();
			System.err.println("Time = " + (timeEnd - timeBegin));
		}catch (Exception e){
			e.printStackTrace(System.err);
			System.exit(-1);
		}
	}
	
	String readString() throws IOException{
		while(!tok.hasMoreTokens()){
			try{
				tok = new StringTokenizer(in.readLine());
			}catch (Exception e){
				return null;
			}
		}
		
		return tok.nextToken();
	}
	
	int readInt() throws IOException {
		return Integer.parseInt(readString());
	}
}

