#include <bits/stdc++.h>
using namespace std;
const double pi = acos(-1.0);
const int inf = 0x3f3f3f3f;
const double eps = 1e-15;
int vis[1001010];
void ex_gcd(int a, int b, int &x, int &y) {
  if (!b) {
    x = 1;
    y = 0;
  } else {
    ex_gcd(b, a % b, y, x);
    y -= x * (a / b);
  }
}
int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }
int main() {
  int m;
  while (~scanf("%d", &m)) {
    int h1, a1, x1, y1;
    int h2, a2, x2, y2;
    scanf("%d%d%d%d", &h1, &a1, &x1, &y1);
    scanf("%d%d%d%d", &h2, &a2, &x2, &y2);
    for (int i = 0; i <= m; ++i) {
      vis[i] = -1;
    }
    int s1 = -1;
    vis[h1] = 0;
    long long cur = h1;
    int len1 = 0;
    for (int i = 1; i <= m; ++i) {
      cur = (cur * x1 + y1) % m;
      if (vis[cur] == -1) {
        vis[cur] = i;
        if (cur == a1) {
          s1 = i;
        }
      } else {
        len1 = i - vis[cur];
        break;
      }
    }
    if (s1 == -1) {
      printf("-1\n");
      continue;
    }
    cur = a1;
    bool flag1 = 0;
    for (int i = s1 + 1; i <= len1 + s1; ++i) {
      cur = (cur * x1 + y1) % m;
    }
    if (cur == a1) {
      flag1 = 1;
    }
    cur = h2;
    int len2 = 0;
    int s2 = -1;
    for (int i = 0; i <= m; ++i) {
      vis[i] = -1;
    }
    vis[h2] = 0;
    for (int i = 1; i <= m; ++i) {
      cur = (cur * x2 + y2) % m;
      if (vis[cur] == -1) {
        vis[cur] = i;
        if (cur == a2) {
          s2 = i;
        }
      } else {
        len2 = i - vis[cur];
        break;
      }
    }
    if (s2 == -1) {
      printf("-1\n");
      continue;
    }
    bool flag2 = 0;
    cur = a2;
    for (int i = s2 + 1; i <= s2 + len2; ++i) {
      cur = (cur * x2 + y2) % m;
    }
    if (cur == a2) {
      flag2 = 1;
    }
    if (!flag1 && !flag2) {
      if (s1 == s2) {
        printf("%d\n", s1);
      } else {
        printf("-1\n");
      }
      continue;
    }
    if (!flag1 && flag2) {
      if (s1 < s2) {
        printf("-1\n");
      } else {
        if ((s1 - s2) % len2) {
          printf("-1\n");
        } else {
          printf("%d\n", s1);
        }
      }
      continue;
    }
    if (flag1 && !flag2) {
      if (s1 > s2) {
        printf("-1\n");
      } else {
        if ((s2 - s1) % len1) {
          printf("-1\n");
        } else {
          printf("%d\n", s2);
        }
      }
      continue;
    }
    int GCD = gcd(len1, len2);
    if ((s2 - s1) % GCD) {
      printf("-1\n");
      continue;
    }
    int x0, y0;
    ex_gcd(len1, len2, x0, y0);
    x0 = x0 * (s2 - s1) / GCD;
    y0 *= -1;
    y0 = y0 * (s2 - s1) / GCD;
    int b = len2 / GCD;
    int a = len1 / GCD;
    if (x0 < 0) {
      long long k = ceil(-1.0 * x0 / b);
      x0 += k * b;
      y0 += k * a;
    }
    if (y0 < 0) {
      long long k = ceil(-1.0 * y0 / a);
      x0 += k * b;
      y0 += k * a;
    }
    long long ans = (long long)y0 * len2 + s2;
    while (1) {
      if (y0 - a >= 0) {
        y0 -= a;
        x0 -= b;
        ans = (long long)y0 * len2 + s2;
      } else {
        break;
      }
    }
    cout << ans << endl;
  }
  return 0;
}
