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.StringTokenizer;


public class ProblemA {
    public static void main(String[] args) {
        int testMode = 0;
        String testInput = "1023\n" +
                "1 2\n" +
                "1 0\n" +
                "1 2\n" +
                "1 1";
        /*-------------------------------------------------------------------------------------------------------*/
        FastReader in = new FastReader(testMode == 0 ? System.in : new ByteArrayInputStream(testInput.getBytes()));
        PrintWriter out = new PrintWriter(System.out);
        new ProblemSolver().solve(in, out);
        out.close();
    }
}

class ProblemSolver {
    void solve(FastReader in, PrintWriter out) {
        int m = in.nextInt();
        int h1 = in.nextInt();
        int a1 = in.nextInt();
        long x1 = in.nextInt();
        long y1 = in.nextInt();
        int h2 = in.nextInt();
        int a2 = in.nextInt();
        long x2 = in.nextInt();
        long y2 = in.nextInt();

        long t1 = 1;
        long s1 = 0;
        long[] rem1 = new long[m];
        rem1[h1] = t1;
        for (int h = h1; ; ) {
            h = (int) ((x1 * h + y1) % m);
            t1++;
            if (rem1[h] > 0) {
                s1 = rem1[h];
                break;
            }
            rem1[h] = t1;
        }

        long t2 = 1;
        long s2 = 0;
        long[] rem2 = new long[m];
        rem2[h2] = t2;
        for (int h = h2; ; ) {
            h = (int) ((x2 * h + y2) % m);
            t2++;
            if (rem2[h] > 0) {
                s2 = rem2[h];
                break;
            }
            rem2[h] = t2;
        }

        long res = 0;
        long n1 = t1 - s1;
        long n2 = t2 - s2;
        if (rem1[a1] == rem2[a2]) res = rem1[a1];
        if (rem1[a1] >= s2 && rem2[a2] >= s2) if ((rem1[a1] - s2) % n2 == (rem2[a2] - s2)) res = rem1[a1];
        if (rem2[a2] >= s1 && rem1[a1] >= s1) if ((rem2[a2] - s1) % n1 == (rem1[a1] - s1)) res = rem2[a2];

        if (s1 <= rem1[a1] && s2 <= rem2[a2]) {
            if (n1 != n2) {
                long r1;
                long r2;
                if (s1 > s2) {
                    r1 = rem1[a1] - s1;
                    r2 = (rem2[a2] - s1 + n2) % n2;
                } else {
                    r1 = (rem1[a1] - s2 + n1) % n1;
                    r2 = rem2[a2] - s2;
                }

                long[][] params = new long[][]{{r1, r2}, {n1, n2}};
                long solution = ChineseRemainderTheorem.solveNonPairwiseCoprime(params[0], params[1]);
                if (solution != -1) {
                    res = solution + (s1 > s2 ? s1 : s2);
                }
            } else {
                if (n1 == 1) res = Math.max(rem1[a1], rem2[a2]);
            }
        }

        out.println(res - 1);
    }
}


class ChineseRemainderTheorem {
    // Chinese Remainder theorem with non-pairwise coprime moduli
    // return x∈Z satisfying system of equations:
    //        x=a1(mod n1)
    //        x=a2(mod n2)
    //        …
    //        x=ak(mod nk)
    // if and only if ai=aj(mod gcd(ni,nj)) for all i,j=1,...,k
    // return -1 otherwise
    static long solveNonPairwiseCoprime(long[] a, long[] n) {
        long a0 = a[0];
        long n0 = n[0];
        long[][] coprimeProblemIn = new long[2][2];
        for (int i = 1; i < n.length; i++) {
            long d = EuclidAlgorithms.gcd(n0, n[i]);
            if ((a0 - a[i]) % d != 0) return -1;
            long k = (a0 - a[i]) / d;
            coprimeProblemIn[0][1] = -k;
            coprimeProblemIn[1][0] = n0 / d;
            coprimeProblemIn[1][1] = n[i] / d;
            long solution = solvePairwiseCoprime(coprimeProblemIn[0], coprimeProblemIn[1]);
            n0 = n0 * coprimeProblemIn[1][1];
            a0 = (solution * d + a0) % n0;
        }
        return (a0 + n0) % n0;
    }


    // Chinese Remainder theorem with pairwise coprime moduli
    // return x∈Z satisfying system of equations:
    //        x=a1(mod n1)
    //        x=a2(mod n2)
    //        …
    //        x=ak(mod nk)
    static long solvePairwiseCoprime(long[] a, long[] n) {
        long m = 1;
        for (int i = 0; i < n.length; i++) m *= n[i];

        long solution = 0;
        for (int i = 0; i < n.length; i++) {
            long ci = m / n[i];
            solution += ModularMultiplicativeInverse.inverse(ci, n[i]) * ci * a[i];
            solution %= m;
        }
        return (solution + m) % m;
    }
}

class ModularMultiplicativeInverse {
    // return b such that a*b=1 modulo n
    // return 0 if such b doesn't exist
    public static long inverse(long a, long n) {
        return new MultiplicativeGroupZn(n).inverse(a);
    }
}

class MultiplicativeGroupZn {
    long n;

    public MultiplicativeGroupZn(long n) {
        this.n = n;
    }

    // return b such a*b=1 mod n
    // return 0 if such b doesn't exist
    public long inverse(long a) {
        long[] ret = EuclidAlgorithms.extendedGcd(a, n);
        return ret[0] == 1 ? ((ret[1] % n) + n) % n : 0;
    }
}

class EuclidAlgorithms {
    public static int gcd(int a, int b) {
        return b == 0 ? a : gcd(b, a % b);
    }

    public static long gcd(long a, long b) {
        return b == 0 ? a : gcd(b, a % b);
    }

    // return [d,x,y] such that d=gcd(a,b)=xa+yb
    public static int[] extendedGcd(int a, int b) {
        if (b == 0) return new int[]{a, 1, 0};
        int[] ret = extendedGcd(b, a % b);
        int x = ret[1];
        ret[1] = ret[2];
        ret[2] = x - ret[2] * (a / b);
        return ret;
    }

    // return [d,x,y] such that d=gcd(a,b)=xa+yb
    public static long[] extendedGcd(long a, long b) {
        if (b == 0) return new long[]{a, 1, 0};
        long[] ret = extendedGcd(b, a % b);
        long x = ret[1];
        ret[1] = ret[2];
        ret[2] = x - ret[2] * (a / b);
        return ret;
    }
}

class FastReader extends BufferedReader {
    private StringTokenizer st;

    public FastReader(InputStream is) {
        super(new InputStreamReader(is));
    }

    public String nextToken() {
        try {
            while (st == null || !st.hasMoreElements()) {
                st = new StringTokenizer(readLine());
            }
            return st.nextToken();
        } catch (IOException e) {
            return null;
        }
    }

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

    public long nextLong() {
        return Long.parseLong(nextToken());
    }

    public int[] nextArrayInt(int size) {
        int[] ret = new int[size];
        for (int i = 0; i < size; i++) {
            ret[i] = nextInt();
        }
        return ret;
    }
}