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.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.Reader;
import java.util.Arrays;
import java.util.StringTokenizer;

public class A {

    static final long MODULO = (long) (1e9 + 7);
    static final int INF = (int) 1e9;

    public static void main(String[] args) {
        BufferedScanner scanner = new BufferedScanner();
        PrintWriter writer = new PrintWriter(new BufferedOutputStream(System.out));

        int t = 1;//scanner.nextInt();
        for (int tc = 0; tc < t; tc++) {
            int m = scanner.nextInt();
            int[] h = new int[2];
            int[] a = new int[2];
            int[] x = new int[2];
            int[] y = new int[2];
            for (int i = 0; i < 2; i++) {
                h[i] = scanner.nextInt();
                a[i] = scanner.nextInt();
                x[i] = scanner.nextInt();
                y[i] = scanner.nextInt();
            }
            writer.println(solve(m, h, a, x, y));
        }

        scanner.close();
        writer.flush();
        writer.close();
    }

    private static long solve(int m, int[] h, int[] a, int[] x, int[] y) {
        int[] timeA = new int[]{-INF, -INF};
        int[] before = new int[]{-INF, -INF};
        int[] after = new int[]{-INF, -INF};
        int[] loop = new int[]{-INF, -INF};

        for (int i = 0; i < 2; i++) {
            int[] time = new int[m];
            Arrays.fill(time, -INF);
            int cur = h[i];
            int timer = 0;
            while (time[cur] == -INF) {
                time[cur] = timer++;
                if (cur == a[i]) {
                    timeA[i] = time[cur];
                }
                cur = (int) (((long) x[i] * cur + y[i]) % m);
            }

            before[i] = time[cur];
            if (timeA[i] == -INF) {
                return -1;
            }
            debug("timeA=" + timeA[i]);
            after[i] = timeA[i] - time[cur];
            loop[i] = timer - time[cur];
            debug("before=" + before[i] + " loop=" + loop[i] + " after=" + after[i]);
        }

        for (int i = 0; i < 2; i++) {
            if (after[i] < 0) {
                int j = 1 - i;
                int cur = h[j];
                for (int timer = 1; timer <= before[i] + after[i]; timer++) {
                    cur = (int) (((long) cur * x[j] + y[j]) % m);
                }
                if (cur != a[j]) {
                    debug("here4");
                    return -1;
                } else {
                    return before[i] + after[i];
                }
            }
        }

        long aa = loop[0];
        long bb = -loop[1];
        long cc = before[1] + after[1] - before[0] - after[0];
        debug("aa:" + aa + " bb:" + bb + " cc:" + cc);
        long[] ans0 = linear(aa, bb, cc);
        if (ans0 == null) {
            debug("here0");
            return -1;
        }
        long gcd = ans0[0];
        long x0 = ans0[1];
        long y0 = ans0[2];
        debug("x0=%d,y0=%d,g=%d", x0, y0, gcd);
        // shift to x>=0 and y>=0
        // x=x0+k*b/g>=0; y=y0-k*a/g>=0;
        long k = Math.min(Math.floorDiv(-x0 * gcd, bb), Math.floorDiv(y0 * gcd, aa));
        debug("k=%d", k);
        long xx = x0 + k * bb / gcd;
        long yy = y0 - k * aa / gcd;
        debug("xx=" + xx + " yy=" + yy);
        if (xx < 0 || yy < 0) {
            debug("here1");
            return -1;
        }
        return before[0] + loop[0] * xx + after[0];
    }

    static void debug(String fmt, Object... args) {
//        System.out.println(String.format(fmt, args));
    }

    /**
     * ax+by=c
     *
     * @return null if no solution; else first=gcd(a,b),second=x,third=y
     */
    private static long[] linear(long a, long b, long c) {
        long[] sol = extgcd(Math.abs(a), Math.abs(b));
        if (c % sol[0] != 0) {
            return null;
        }
        if (a < 0) {
            sol[1] = -sol[1];
        }
        if (b < 0) {
            sol[2] = -sol[2];
        }
        return new long[]{sol[0], sol[1] * c / sol[0], sol[2] * c / sol[0]};
    }

    public static class BufferedScanner {
        BufferedReader br;
        StringTokenizer st;

        public BufferedScanner(Reader reader) {
            br = new BufferedReader(reader);
        }

        public BufferedScanner() {
            this(new InputStreamReader(System.in));
        }

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

        int nextInt() {
            return Integer.parseInt(next());
        }

        long nextLong() {
            return Long.parseLong(next());
        }

        double nextDouble() {
            return Double.parseDouble(next());
        }

        String nextLine() {
            String str = "";
            try {
                str = br.readLine();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return str;
        }

        void close() {
            try {
                br.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }

    static long gcd(long a, long b) {
        if (a < b) {
            return gcd(b, a);
        }
        while (b > 0) {
            long tmp = b;
            b = a % b;
            a = tmp;
        }
        return a;
    }

    static long inverse(long a, long m) {
        long[] ans = extgcd(a, m);
        return ans[0] == 1 ? (ans[1] + m) % m : -1;
    }

    /**
     * ax+by=gcd(a,b)
     *
     * @return first=gcd(a,b),second=x,third=y
     */
    private static long[] extgcd(long a, long b) {
        if (b == 0) {
            return new long[]{a, 1, 0};
        } else {
            long[] ans = extgcd(b, a % b);
            long tmp = ans[1];
            ans[1] = ans[2];
            ans[2] = tmp;
            ans[2] -= ans[1] * (a / b);
            return ans;
        }
    }

    static long add(long a, long b) {
        a += b;
        if (a >= MODULO) {
            a -= MODULO;
        }
        return a;
    }

    static long sub(long a, long b) {
        a -= b;
        if (a < 0) {
            a += MODULO;
        }
        return a;
    }

    static long mul(long a, long b) {
        return a * b % MODULO;
    }

    static long div(long a, long b) {
        return a * inverse(b, MODULO) % MODULO;
    }

    static class Comb {
        final long modulo;
        final long[] fac, fnv;

        Comb(int limit, long modulo) {
            fac = new long[limit + 1];
            fnv = new long[limit + 1];
            fac[0] = 1;
            fnv[0] = 1;
            for (int i = 1; i <= limit; i++) {
                fac[i] = mul(fac[i - 1], i);
                fnv[i] = div(fnv[i - 1], i);
            }
            this.modulo = modulo;
        }

        long c(int total, int choose) {
            if (total < choose) {
                return 0;
            }
            if (total == 0 || total == choose) {
                return 1;
            }
            return mul(mul(fac[total], fnv[choose]), fnv[total - choose]);
        }
    }

}
