#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define maxn 1000000
bool vit1[maxn], vit2[maxn];

ll gcd(ll a, ll b)
{
    ll pr = a, r = b;
    while(r != 0)
    {
        ll q = pr/r, h;
        h = r;
        r = pr - q*r;
        pr = h;
    }
    return pr;
}
ll lcm(ll a, ll b)
{
    return a/gcd(a, b)*b;
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    ll m, h1, a1, x1, y1, h2, a2, x2, y2, k1 = -1, k2 = -1, ret = 0;
    cin >> m >> h1 >> a1 >> x1 >> y1 >> h2 >> a2 >> x2 >> y2;
    memset(vit1, false, sizeof(vit1));
    memset(vit2, false, sizeof(vit2));
    vit1[h1] = vit2[h2] = true;
    while(1)
    {
        ret++;
        h1 = (x1*h1 + y1) % m;
        h2 = (x2*h2 + y2) % m;
        if((vit1[h1] && !vit1[a1])|| (vit2[h2] && !vit2[a2]))
        {
            ret = -1;
            break;
        }
        if(h1 == a1 && k1 == -1) k1 = ret;
        if(h2 == a2 && k2 == -1) k2 = ret;
        vit1[h1] = vit2[h2] = true;
        if(vit1[a1] && vit2[a2])
        {
            ret = lcm(k1, k2);
            break;
        }
    }
    cout << ret;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define maxn 1000000
ll vit1[maxn], vit2[maxn];

ll gcd(ll a, ll b)
{
    ll pr = a, r = b;
    while(r != 0)
    {
        ll q = pr/r, h;
        h = r;
        r = pr - q*r;
        pr = h;
    }
    return pr;
}
ll lcm(ll a, ll b)
{
    return a/gcd(a, b)*b;
}
pair<ll, ll> euclid(ll a, ll b)
{
    ll pr = a, r = b;
    ll ps = 1, s = 0;
    ll pt = 0, t = 1;
    while(r != 0)
    {
        ll q = pr/r, h;
        h = r;
        r = pr - q*r;
        pr = h;
        h = s;
        s = ps - q*s;
        ps = h;
        h = t;
        t = pt - q*t;
        pt = h;
    }
    return make_pair(ps, pt);
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    ll m, h1, a1, x1, y1, h2, a2, x2, y2, k1 = -1, k2 = -1, t1 = -1, t2 = -1, ret = 0;
    cin >> m >> h1 >> a1 >> x1 >> y1 >> h2 >> a2 >> x2 >> y2;
    memset(vit1, 0, sizeof(vit1));
    memset(vit2, 0, sizeof(vit2));
    vit1[h1] = vit2[h2] = 1;
    while(h1 != a1 || h2 != a2)
    {
        ret++;
        h1 = (x1*h1 + y1) % m;
        h2 = (x2*h2 + y2) % m;
        if((vit1[h1] > 0 && vit1[a1] == 0)|| (vit2[h2] > 0 && vit2[a2] == 0))
        {
            ret = -1;
            break;
        }
        if(h1 == a1)
        {
            if(k1 == -1) k1 = ret;
            else
                if(t1 == -1) t1 = ret;
        }
        if(h2 == a2)
        {
            if(k2 == -1) k2 = ret;
            else
                if(t2 == -1) t2 = ret;
        }
        vit1[h1]++;
        vit2[h2]++;
        if(vit1[a1] >= 2 && vit2[a2] >= 2)
        {
            ll a, b, c, d;
            a = t1 - k1;
            b = k1;
            c = t2 - k2;
            d = k2;
            if(b == d)
            {
                ret = b;
                break;
            }
            ll g = gcd(a, -c);
            if((d - b) % g != 0)
            {
                ret = -1;
                break;
            }
            else
            {
                a /= g;
                c /= g;
                b = (d - b)/g;
                pair<ll, ll> p = euclid(a, -c);
                ll x0 = b*p.first, y0 = b*p.second;
                ll a0 = t1 - k1, b0 = k1, c0 = t2 - k2, d0 = k2;
                double up = (d0 - c0*y0)/(1.0*c0*a), dw = (b0 - a0*x0)/(1.0*a0*c);
                if(c0*a > 0 && a0*c > 0)
                {
                    ret = c0*(y0 + a*(max((ll)ceil(up), (ll)ceil(dw)))) + d0;
                }
                if(c0*a > 0 && a0*c < 0)
                {
                    ll ddw = ceil(up), uup = up;
                    if(uup < ddw) ret = -1;
                    else ret = c0*(y0 + a*ddw) + d0;
                }
                if(c0*a < 0 && a0*c > 0)
                {
                    ll ddw = ceil(dw), uup = floor(up);
                    if(uup < ddw) ret = -1;
                    else ret = c0*(y0 + a*uup) + d0;
                }
                if(c0*a < 0 && a0*c < 0)
                {
                    ret = c0*(y0 + a*min((ll)floor(up), (ll)floor(dw))) + d0;
                }
            }
            break;
        }
    }
    cout << ret;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define maxn 1000000
ll vit1[maxn], vit2[maxn];

ll gcd(ll a, ll b)
{
    ll pr = a, r = b;
    while(r != 0)
    {
        ll q = pr/r, h;
        h = r;
        r = pr - q*r;
        pr = h;
    }
    return pr;
}
ll lcm(ll a, ll b)
{
    return a/gcd(a, b)*b;
}
pair<ll, ll> euclid(ll a, ll b)
{
    ll pr = a, r = b;
    ll ps = 1, s = 0;
    ll pt = 0, t = 1;
    while(r != 0)
    {
        ll q = pr/r, h;
        h = r;
        r = pr - q*r;
        pr = h;
        h = s;
        s = ps - q*s;
        ps = h;
        h = t;
        t = pt - q*t;
        pt = h;
    }
    return make_pair(ps, pt);
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    ll m, h1, a1, x1, y1, h2, a2, x2, y2, k1 = -1, k2 = -1, t1 = -1, t2 = -1, ret = 0;
    cin >> m >> h1 >> a1 >> x1 >> y1 >> h2 >> a2 >> x2 >> y2;
    memset(vit1, 0, sizeof(vit1));
    memset(vit2, 0, sizeof(vit2));
    vit1[h1] = vit2[h2] = 1;
    while(h1 != a1 || h2 != a2)
    {
        ret++;
        h1 = (x1*h1 + y1) % m;
        h2 = (x2*h2 + y2) % m;
        if((vit1[h1] > 0 && vit1[a1] == 0)|| (vit2[h2] > 0 && vit2[a2] == 0))
        {
            ret = -1;
            break;
        }
        if(h1 == a1)
        {
            if(k1 == -1) k1 = ret;
            else
                if(t1 == -1) t1 = ret;
        }
        if(h2 == a2)
        {
            if(k2 == -1) k2 = ret;
            else
                if(t2 == -1) t2 = ret;
        }
        vit1[h1]++;
        vit2[h2]++;
        if(vit1[a1] >= 2 && vit2[a2] >= 2)
        {
            ll a, b, c, d;
            a = t1 - k1;
            b = k1;
            c = t2 - k2;
            d = k2;
            if(b == d)
            {
                ret = b;
                break;
            }
            ll g = gcd(a, -c);
            if((d - b) % g != 0)
            {
                ret = -1;
                break;
            }
            else
            {
                a /= g;
                c /= g;
                b = (d - b)/g;
                pair<ll, ll> p = euclid(a, -c);
                ll x0 = b*p.first, y0 = b*p.second;
                ll a0 = t1 - k1, b0 = k1, c0 = t2 - k2, d0 = k2;
                double up = -x0*1.0/c, dw = -y0*1.0/a;
                if(c > 0 && a > 0)
                {
                    ret = c0*(y0 + a*(max((ll)ceil(up), (ll)ceil(dw)))) + d0;
                }
                if(c > 0 && a < 0)
                {
                    ll ddw = ceil(up), uup = floor(up);
                    if(uup < ddw) ret = -1;
                    else ret = c0*(y0 + a*ddw) + d0;
                }
                if(c < 0 && a > 0)
                {
                    ll ddw = ceil(dw), uup = floor(up);
                    if(uup < ddw) ret = -1;
                    else ret = c0*(y0 + a*uup) + d0;
                }
                if(c < 0 && a < 0)
                {
                    ret = c0*(y0 + a*min((ll)floor(up), (ll)floor(dw))) + d0;
                }
            }
            break;
        }
        if((h1 == 0 || h2 == 0) && (h1 != a1 || h2 != a2))
        {
            ret = -1;
            break;
        }
    }
    cout << ret;
    return 0;
}


#include <iostream>
#include <cstdio>
#include <iomanip>
#include <vector>
#include <algorithm>
#include <queue>

using namespace std;
typedef long long ll; typedef long double ld;
typedef pair<ll, ll> pii;

const ll MAXN = 1e6 + 10;

ll m, h1, a1, x1, y1, h2, a2, x2, y2;
ll first1[MAXN], first2[MAXN], his1[MAXN], his2[MAXN], c1, c2, s1, s2;

int main(){
    cin >> m >> h1 >> a1 >> x1 >> y1 >> h2 >> a2 >> x2 >> y2;

    fill (first1, first1 + MAXN, -1);
    fill (first2, first2 + MAXN, -1);

    his1[0] = h1; first1[h1] = 0;
    for (ll i = 1; i <= m; i++){
        his1[i] = (his1[i-1] * x1 + y1) % m;
        if (first1[his1[i]] != -1){
            c1 = i - first1[his1[i]];
        //    cerr << "here " << i << " - " << first1[his1[i]] << endl;
            s1 = first1[his1[i]];
            break;
        }else first1[his1[i]] = i;
    }

    his2[0] = h2; first2[h2] = 0;
    for (ll i = 1; i <= m; i++){
        his2[i] = (his2[i-1] * x2 + y2) % m;
        if (first2[his2[i]] != -1){
         //   cerr << "here " << i << " - " << first2[his2[i]] << endl;
            c2 = i - first2[his2[i]];
            s2 = first2[his2[i]];
            break;
        }else first2[his2[i]] = i;
    }

   /* for (ll i = 0; i < m; i++)
        cerr << his1[i] << " -> ";
    cerr << endl;

    for (ll i = 0; i < m; i++)
        cerr << his2[i] << " -> ";
    cerr << endl;*/

    ll i1 = first1[a1], i2 = first2[a2];
    if (i1 == -1 || i2 == -1){
        cout << -1 << endl;
        return 0;
    }

    if (i1 < s1 && i2 < s2){
        if (i1 == i2) cout << i1; else cout << -1;
        return 0;
    }

    if (i1 < s1){
        if (first2[his2[i2]] < i1 && (i1 - first2[his2[i2]]) % c2 == 0)
            cout << i1;
        else  cout << -1;
        return 0;

    }

    if (i2 < s2){
        if (first1[his1[i1]] < i2 && (i2 - first1[his1[i1]]) % c1 == 0)
            cout << i2;
        else  cout << -1;
        return 0;
    }

    ll x;
    for (x = 1; x < MAXN * 10; x++){
    //    cerr << c2 << ", " << c1<< endl;
        if ((i2 - s1) % c1 == i1 - s1 && i2 >= s1) break;
        i2 += c2;
    }
    if ((i2 - s1) % c1 == i1 - s1 && i2 >= s1) cout << i2 << endl;
    else cout << -1;

}


#include <iostream>
#include <cstdio>
#include <iomanip>
#include <vector>
#include <algorithm>
#include <queue>

using namespace std;
typedef long long ll; typedef long double ld;
typedef pair<ll, ll> pii;

const ll MAXN = 1e6 + 10;

ll m, h1, a1, x1, y1, h2, a2, x2, y2;
ll first1[MAXN], first2[MAXN], his1[MAXN], his2[MAXN], c1, c2, s1, s2;

int main(){
    cin >> m >> h1 >> a1 >> x1 >> y1 >> h2 >> a2 >> x2 >> y2;

    fill (first1, first1 + m, -1);
    fill (first2, first2 + m, -1);

    his1[0] = h1; first1[h1] = 0;
    for (ll i = 1; i <= m; i++){
        his1[i] = (his1[i-1] * x1 + y1) % m;
        if (first1[his1[i]] != -1){
            c1 = i - first1[his1[i]];
        //    cerr << "here " << i << " - " << first1[his1[i]] << endl;
            s1 = first1[his1[i]];
            break;
        }else first1[his1[i]] = i;
    }

    his2[0] = h2; first2[h2] = 0;
    for (ll i = 1; i <= m; i++){
        his2[i] = (his2[i-1] * x2 + y2) % m;
        if (first2[his2[i]] != -1){
         //   cerr << "here " << i << " - " << first2[his2[i]] << endl;
            c2 = i - first2[his2[i]];
            s2 = first2[his2[i]];
            break;
        }else first2[his2[i]] = i;
    }

   /* for (ll i = 0; i < m; i++)
        cerr << his1[i] << " -> ";
    cerr << endl;

    for (ll i = 0; i < m; i++)
        cerr << his2[i] << " -> ";
    cerr << endl;*/

    ll i1 = first1[a1], i2 = first2[a2];
    if (i1 == -1 || i2 == -1){
        cout << -1 << endl;
        return 0;
    }

    if (i1 < s1 && i2 < s2){
        if (i1 == i2) cout << i1; else cout << -1;
        return 0;
    }

    if (i1 < s1){
        if (first2[his2[i2]] < s1 && (i1 - first2[his2[i2]]) % c2 == 0)
            cout << i1;
        else  cout << -1;
        return 0;

    }

    if (i2 < s2){
        if (first1[his1[i1]] < s2 && (i2 - first1[his1[i1]]) % c1 == 0)
            cout << i2;
        else  cout << -1;
        return 0;
    }

    ll x;
    for (x = 1; x < MAXN * 10; x++){
    //    cerr << c2 << ", " << c1<< endl;
        if ((i2 - s1) % c1 == i1 - s1 && i2 > s1) break;
        i2 += c2;
    }
    if ((i2 - s1) % c1 == i1 - s1 && i2 > s1) cout << i2 << endl;
    else cout << -1;

}


#include <iostream>
#include <cstdio>
#include <iomanip>
#include <vector>
#include <algorithm>
#include <queue>

using namespace std;
typedef long long ll; typedef long double ld;
typedef pair<ll, ll> pii;

const ll MAXN = 1e6 + 10;

ll m, h1, a1, x1, y1, h2, a2, x2, y2;
ll first1[MAXN], first2[MAXN], his1[MAXN], his2[MAXN], c1, c2, s1, s2;

int main(){
    cin >> m >> h1 >> a1 >> x1 >> y1 >> h2 >> a2 >> x2 >> y2;

    fill (first1, first1 + m, -1);
    fill (first2, first2 + m, -1);

    his1[0] = h1; first1[h1] = 0;
    for (ll i = 1; i <= m; i++){
        his1[i] = (his1[i-1] * x1 + y1) % m;
        if (first1[his1[i]] != -1){
            c1 = i - first1[his1[i]];
        //    cerr << "here " << i << " - " << first1[his1[i]] << endl;
            s1 = first1[his1[i]];
            break;
        }else first1[his1[i]] = i;
    }

    his2[0] = h2; first2[h2] = 0;
    for (ll i = 1; i <= m; i++){
        his2[i] = (his2[i-1] * x2 + y2) % m;
        if (first2[his2[i]] != -1){
         //   cerr << "here " << i << " - " << first2[his2[i]] << endl;
            c2 = i - first2[his2[i]];
            s2 = first2[his2[i]];
            break;
        }else first2[his2[i]] = i;
    }

   /* for (ll i = 0; i < m; i++)
        cerr << his1[i] << " -> ";
    cerr << endl;

    for (ll i = 0; i < m; i++)
        cerr << his2[i] << " -> ";
    cerr << endl;*/

    ll i1 = first1[a1], i2 = first2[a2];
    if (i1 == -1 || i2 == -1){
        cout << -1 << endl;
        return 0;
    }

    if (i1 < s1 && i2 < s2){
        if (i1 == i2) cout << i1; else cout << -1;
        return 0;
    }

    if (i2 < s2){
        swap(i2, i1);
        swap(s2, s1);
        swap(c2, c1);
    }

    ll x;
    for (x = 1; x < MAXN * 10; x++){
    //    cerr << c2 << ", " << c1<< endl;
        if ((i2 - s1) % c1 == (i1 - s1) % c1 && (i2 >= s1) == (i1 >= s1)) break;
        i2 += c2;
    }
    if ((i2 - s1) % c1 == i1 - s1 && i2 > s1) cout << i2 << endl;
    else cout << -1;

}


#include <iostream>
#include <cstdio>
#include <iomanip>
#include <vector>
#include <algorithm>
#include <queue>

using namespace std;
typedef long long ll; typedef long double ld;
typedef pair<ll, ll> pii;

const ll MAXN = 1e6 + 10;

ll m, h1, a1, x1, y1, h2, a2, x2, y2;
ll first1[MAXN], first2[MAXN], his1[MAXN], his2[MAXN], c1, c2, s1, s2;

int main(){
    cin >> m >> h1 >> a1 >> x1 >> y1 >> h2 >> a2 >> x2 >> y2;

    fill (first1, first1 + MAXN, -1);
    fill (first2, first2 + MAXN, -1);

    his1[0] = h1; first1[h1] = 0;
    for (ll i = 1; i <= m; i++){
        his1[i] = (his1[i-1] * x1 + y1) % m;
        if (first1[his1[i]] != -1){
            c1 = i - first1[his1[i]];
        //    cerr << "here " << i << " - " << first1[his1[i]] << endl;
            s1 = first1[his1[i]];
            break;
        }else first1[his1[i]] = i;
    }

    his2[0] = h2; first2[h2] = 0;
    for (ll i = 1; i <= m; i++){
        his2[i] = (his2[i-1] * x2 + y2) % m;
        if (first2[his2[i]] != -1){
         //   cerr << "here " << i << " - " << first2[his2[i]] << endl;
            c2 = i - first2[his2[i]];
            s2 = first2[his2[i]];
            break;
        }else first2[his2[i]] = i;
    }

   /* for (ll i = 0; i < m; i++)
        cerr << his1[i] << " -> ";
    cerr << endl;

    for (ll i = 0; i < m; i++)
        cerr << his2[i] << " -> ";
    cerr << endl;*/

    ll i1 = first1[a1], i2 = first2[a2];
    if (i1 == -1 || i2 == -1){
        cout << -1 << endl;
        return 0;
    }

    if (i1 < s1 && i2 < s2){
        if (i1 == i2) cout << i1; else cout << -1;
        return 0;
    }

    if (i1 < s1){
        if (first2[his2[i2]] < i1 && (i1 - first2[his2[i2]]) % c2 == 0)
            cout << i1;
        else  cout << -1;
        return 0;

    }

    if (i2 < s2){
        if (first1[his1[i1]] < i2 && (i2 - first1[his1[i1]]) % c1 == 0)
            cout << i2;
        else  cout << -1;
        return 0;
    }

    ll x;
    for (x = 1; x < MAXN * 10; x++){
    //    cerr << c2 << ", " << c1<< endl;
        if ((i2 - s1) % c1 == i1 - s1 && i2 > s1) break;
        i2 += c2;
    }
    if ((i2 - s1) % c1 == i1 - s1 && i2 > s1) cout << i2 << endl;
    else cout << -1;

}


#include <iostream>
#include <cstdio>
#include <iomanip>
#include <vector>
#include <algorithm>
#include <queue>

using namespace std;
typedef long long ll; typedef long double ld;
typedef pair<ll, ll> pii;

const ll MAXN = 1e6 + 10;

ll m, h1, a1, x1, y1, h2, a2, x2, y2;
ll first1[MAXN], first2[MAXN], his1[MAXN], his2[MAXN], c1, c2, s1, s2;

int main(){
    cin >> m >> h1 >> a1 >> x1 >> y1 >> h2 >> a2 >> x2 >> y2;

    fill (first1, first1 + m, -1);
    fill (first2, first2 + m, -1);

    his1[0] = h1; first1[h1] = 0;
    for (ll i = 1; i <= m; i++){
        his1[i] = (his1[i-1] * x1 + y1) % m;
        if (first1[his1[i]] != -1){
            c1 = i - first1[his1[i]];
        //    cerr << "here " << i << " - " << first1[his1[i]] << endl;
            s1 = first1[his1[i]];
            break;
        }else first1[his1[i]] = i;
    }

    his2[0] = h2; first2[h2] = 0;
    for (ll i = 1; i <= m; i++){
        his2[i] = (his2[i-1] * x2 + y2) % m;
        if (first2[his2[i]] != -1){
         //   cerr << "here " << i << " - " << first2[his2[i]] << endl;
            c2 = i - first2[his2[i]];
            s2 = first2[his2[i]];
            break;
        }else first2[his2[i]] = i;
    }

   /* for (ll i = 0; i < m; i++)
        cerr << his1[i] << " -> ";
    cerr << endl;

    for (ll i = 0; i < m; i++)
        cerr << his2[i] << " -> ";
    cerr << endl;*/

    ll i1 = first1[a1], i2 = first2[a2];
    if (i1 == -1 || i2 == -1){
        cout << -1 << endl;
        return 0;
    }

    if (i1 < s1 && i2 < s2){
        if (i1 == i2) cout << i1; else cout << -1;
        return 0;
    }

    if (i2 < s2){
        swap(i2, i1);
        swap(s2, s1);
        swap(c2, c1);
    }

    ll x;
    for (x = 1; x < MAXN * 10; x++){
    //    cerr << c2 << ", " << c1<< endl;
        if ((i2 - s1) % c1 == i1 - s1 && i2 > s1) break;
        i2 += c2;
    }
    if ((i2 - s1) % c1 == i1 - s1 && i2 > s1) cout << i2 << endl;
    else cout << -1;

}


#include <iostream>
#include <cstdio>
#include <iomanip>
#include <vector>
#include <algorithm>
#include <queue>

using namespace std;
typedef long long ll; typedef long double ld;
typedef pair<ll, ll> pii;

const ll MAXN = 1e6 + 10;

ll m, h1, a1, x1, y1, h2, a2, x2, y2;
ll first1[MAXN], first2[MAXN], his1[MAXN], his2[MAXN], c1, c2, s1, s2;

int main(){
    cin >> m >> h1 >> a1 >> x1 >> y1 >> h2 >> a2 >> x2 >> y2;

    fill (first1, first1 + MAXN, -1);
    fill (first2, first2 + MAXN, -1);

    his1[0] = h1; first1[h1] = 0;
    for (ll i = 1; i <= m; i++){
        his1[i] = (his1[i-1] * x1 + y1) % m;
        if (first1[his1[i]] != -1){
            c1 = i - first1[his1[i]];
        //    cerr << "here " << i << " - " << first1[his1[i]] << endl;
            s1 = first1[his1[i]];
            break;
        }else first1[his1[i]] = i;
    }

    his2[0] = h2; first2[h2] = 0;
    for (ll i = 1; i <= m; i++){
        his2[i] = (his2[i-1] * x2 + y2) % m;
        if (first2[his2[i]] != -1){
         //   cerr << "here " << i << " - " << first2[his2[i]] << endl;
            c2 = i - first2[his2[i]];
            s2 = first2[his2[i]];
            break;
        }else first2[his2[i]] = i;
    }

    for (ll i = 0; i < m; i++)
        cerr << his1[i] << " -> ";
    cerr << endl;

    for (ll i = 0; i < m; i++)
        cerr << his2[i] << " -> ";
    cerr << endl;

    ll i1 = first1[a1], i2 = first2[a2];
    if (i1 == -1 || i2 == -1){
        cout << -1 << endl;
        return 0;
    }

    if (i1 < s1 && i2 < s2){
        if (i1 == i2) cout << i1; else cout << -1;
        return 0;
    }

    if (i1 < s1){
        if (first2[his2[i2]] <= i1 && (i1 - first2[his2[i2]]) % c2 == 0)
            cout << i1;
        else  cout << -1;
        return 0;

    }

    if (i2 < s2){
        cerr << s1 << ", " << s2;
        if (first1[his1[i1]] <= i2 && (i2 - first1[his1[i1]]) % c1 == 0)
            cout << i2;
        else  cout << -1;
        return 0;
    }

    ll x;
    for (x = 1; x < MAXN * 10; x++){
    //    cerr << c2 << ", " << c1<< endl;
        if ((i2 - s1) % c1 == i1 - s1 && i2 >= s1) break;
        i2 += c2;
    }
    if ((i2 - s1) % c1 == i1 - s1 && i2 >= s1) cout << i2 << endl;
    else cout << -1;

}


#include <iostream>
#include <cstdio>
#include <iomanip>
#include <vector>
#include <algorithm>
#include <queue>

using namespace std;
typedef long long ll; typedef long double ld;
typedef pair<ll, ll> pii;

const ll MAXN = 1e6 + 10;

ll m, h1, a1, x1, y1, h2, a2, x2, y2;
ll first1[MAXN], first2[MAXN], his1[MAXN], his2[MAXN], c1, c2, s1, s2;

int main(){
    cin >> m >> h1 >> a1 >> x1 >> y1 >> h2 >> a2 >> x2 >> y2;

    fill (first1, first1 + m, -1);
    fill (first2, first2 + m, -1);

    his1[0] = h1; first1[h1] = 0;
    for (ll i = 1; i <= m; i++){
        his1[i] = (his1[i-1] * x1 + y1) % m;
        if (first1[his1[i]] != -1){
            c1 = i - first1[his1[i]];
        //    cerr << "here " << i << " - " << first1[his1[i]] << endl;
            s1 = first1[his1[i]];
            break;
        }else first1[his1[i]] = i;
    }

    his2[0] = h2; first2[h2] = 0;
    for (ll i = 1; i <= m; i++){
        his2[i] = (his2[i-1] * x2 + y2) % m;
        if (first2[his2[i]] != -1){
         //   cerr << "here " << i << " - " << first2[his2[i]] << endl;
            c2 = i - first2[his2[i]];
            s2 = first2[his2[i]];
            break;
        }else first2[his2[i]] = i;
    }

   /* for (ll i = 0; i < m; i++)
        cerr << his1[i] << " -> ";
    cerr << endl;

    for (ll i = 0; i < m; i++)
        cerr << his2[i] << " -> ";
    cerr << endl;*/

    ll i1 = first1[a1], i2 = first2[a2];
    if (i1 == -1 || i2 == -1){
        cout << -1 << endl;
        return 0;
    }

    if (i1 < s1 && i2 < s2){
        if (i1 == i2) cout << i1; else cout << -1;
        return 0;
    }

    if (i1 < s1){
        if ((i1 - s2) % c2 == i2 -s2 && i1 >= s2) cout << i1; else cout << -1;
        return 0;
    }

    if (i2 < s2){
        if ((i2 - s1) % c1 == i1 -s1 && i2 >= s1) cout << i2; else cout << -1;
        return 0;
    }

    ll x;
    for (x = 1; x < MAXN; x++){
    //    cerr << c2 << ", " << c1<< endl;
        if ((i2 - s1) % c1 == i1 - s1 && i2 > s1) break;
        i2 += c2;
    }
    if ((i2 - s1) % c1 == i1 - s1 && i2 > s1) cout << i2 << endl;
    else cout << -1;

}


#include <iostream>
#include <cstdio>
#include <iomanip>
#include <vector>
#include <algorithm>
#include <queue>

using namespace std;
typedef long long ll; typedef long double ld;
typedef pair<ll, ll> pii;

const ll MAXN = 1e6 + 10;

ll m, h1, a1, x1, y1, h2, a2, x2, y2;
ll first1[MAXN], first2[MAXN], his1[MAXN], his2[MAXN], c1, c2, s1, s2;

int main(){
    cin >> m >> h1 >> a1 >> x1 >> y1 >> h2 >> a2 >> x2 >> y2;

    fill (first1, first1 + MAXN, -1);
    fill (first2, first2 + MAXN, -1);

    his1[0] = h1; first1[h1] = 0;
    for (ll i = 1; i <= m; i++){
        his1[i] = (his1[i-1] * x1 + y1) % m;
        if (first1[his1[i]] != -1){
            c1 = i - first1[his1[i]];
        //    cerr << "here " << i << " - " << first1[his1[i]] << endl;
            s1 = first1[his1[i]];
            break;
        }else first1[his1[i]] = i;
    }

    his2[0] = h2; first2[h2] = 0;
    for (ll i = 1; i <= m; i++){
        his2[i] = (his2[i-1] * x2 + y2) % m;
        if (first2[his2[i]] != -1){
         //   cerr << "here " << i << " - " << first2[his2[i]] << endl;
            c2 = i - first2[his2[i]];
            s2 = first2[his2[i]];
            break;
        }else first2[his2[i]] = i;
    }

   /* for (ll i = 0; i < m; i++)
        cerr << his1[i] << " -> ";
    cerr << endl;

    for (ll i = 0; i < m; i++)
        cerr << his2[i] << " -> ";
    cerr << endl;*/

    ll i1 = first1[a1], i2 = first2[a2];
    if (i1 == -1 || i2 == -1){
        cout << -1 << endl;
        return 0;
    }

    if (i1 < s1 && i2 < s2){
        if (i1 == i2) cout << i1; else cout << -1;
        return 0;
    }

    if (i1 < s1){
        if (first2[his2[i2]] <= i1 && (i1 - first2[his2[i2]]) % c2 == 0)
            cout << i1;
        else  cout << -1;
        return 0;

    }

    if (i2 < s2){
        cerr << s1 << ", " << s2;
        if (first1[his1[i1]] <= i2 && (i2 - first1[his1[i1]]) % c1 == 0)
            cout << i2;
        else  cout << -1;
        return 0;
    }

    ll x;
    for (x = 1; x < MAXN * 10; x++){
    //    cerr << c2 << ", " << c1<< endl;
        if ((i2 - s1) % c1 == i1 - s1 && i2 >= s1) break;
        i2 += c2;
    }
    if ((i2 - s1) % c1 == i1 - s1 && i2 >= s1) cout << i2 << endl;
    else cout << -1;

}


#include <iostream>
#include <cstdio>
#include <iomanip>
#include <vector>
#include <algorithm>
#include <queue>

using namespace std;
typedef long long ll; typedef long double ld;
typedef pair<ll, ll> pii;

const ll MAXN = 1e6 + 10;

ll m, h1, a1, x1, y1, h2, a2, x2, y2;
ll first1[MAXN], first2[MAXN], his1[MAXN], his2[MAXN], c1, c2, s1, s2;

int main(){
    cin >> m >> h1 >> a1 >> x1 >> y1 >> h2 >> a2 >> x2 >> y2;

    fill (first1, first1 + m, -1);
    fill (first2, first2 + m, -1);

    his1[0] = h1; first1[h1] = 0;
    for (ll i = 1; i <= m; i++){
        his1[i] = (his1[i-1] * x1 + y1) % m;
        if (first1[his1[i]] != -1){
            c1 = i - first1[his1[i]];
        //    cerr << "here " << i << " - " << first1[his1[i]] << endl;
            s1 = first1[his1[i]];
            break;
        }else first1[his1[i]] = i;
    }

    his2[0] = h2; first2[h2] = 0;
    for (ll i = 1; i <= m; i++){
        his2[i] = (his2[i-1] * x2 + y2) % m;
        if (first2[his2[i]] != -1){
         //   cerr << "here " << i << " - " << first2[his2[i]] << endl;
            c2 = i - first2[his2[i]];
            s2 = first2[his2[i]];
            break;
        }else first2[his2[i]] = i;
    }

   /* for (ll i = 0; i < m; i++)
        cerr << his1[i] << " -> ";
    cerr << endl;

    for (ll i = 0; i < m; i++)
        cerr << his2[i] << " -> ";
    cerr << endl;*/

    ll i1 = first1[a1], i2 = first2[a2];
    if (i1 == -1 || i2 == -1){
        cout << -1 << endl;
        return 0;
    }

    if (i1 < s1 && i2 < s2){
        if (i1 == i2) cout << i1; else cout << -1;
        return 0;
    }

    if (i1 < s1){
        if (first2[his2[i2]] < i1 && (i1 - first2[his2[i2]]) % c2 == 0)
            cout << i1;
        else  cout << -1;
        return 0;

    }

    if (i2 < s2){
        if (first1[his1[i1]] < i2 && (i2 - first1[his1[i1]]) % c1 == 0)
            cout << i2;
        else  cout << -1;
        return 0;
    }

    ll x;
    for (x = 1; x < MAXN * 10; x++){
    //    cerr << c2 << ", " << c1<< endl;
        if ((i2 - s1) % c1 == i1 - s1 && i2 > s1) break;
        i2 += c2;
    }
    if ((i2 - s1) % c1 == i1 - s1 && i2 > s1) cout << i2 << endl;
    else cout << -1;

}


#include <iostream>
#include <cstdio>
#include <iomanip>
#include <vector>
#include <algorithm>
#include <queue>

using namespace std;
typedef long long ll; typedef long double ld;
typedef pair<ll, ll> pii;

const ll MAXN = 1e6 + 10;

ll m, h1, a1, x1, y1, h2, a2, x2, y2;
ll first1[MAXN], first2[MAXN], his1[MAXN], his2[MAXN], c1, c2, s1, s2;

int main(){
    cin >> m >> h1 >> a1 >> x1 >> y1 >> h2 >> a2 >> x2 >> y2;

    fill (first1, first1 + m, -1);
    fill (first2, first2 + m, -1);

    his1[0] = h1; first1[h1] = 0;
    for (ll i = 1; i <= m; i++){
        his1[i] = (his1[i-1] * x1 + y1) % m;
        if (first1[his1[i]] != -1){
            c1 = i - first1[his1[i]];
        //    cerr << "here " << i << " - " << first1[his1[i]] << endl;
            s1 = first1[his1[i]];
            break;
        }else first1[his1[i]] = i;
    }

    his2[0] = h2; first2[h2] = 0;
    for (ll i = 1; i <= m; i++){
        his2[i] = (his2[i-1] * x2 + y2) % m;
        if (first2[his2[i]] != -1){
         //   cerr << "here " << i << " - " << first2[his2[i]] << endl;
            c2 = i - first2[his2[i]];
            s2 = first2[his2[i]];
            break;
        }else first2[his2[i]] = i;
    }

   /* for (ll i = 0; i < m; i++)
        cerr << his1[i] << " -> ";
    cerr << endl;

    for (ll i = 0; i < m; i++)
        cerr << his2[i] << " -> ";
    cerr << endl;*/

    ll i1 = first1[a1], i2 = first2[a2];
    if (i1 == -1 || i2 == -1){
        cout << -1 << endl;
        return 0;
    }

    if (i1 < s1 && i2 < s2){
        if (i1 == i2) cout << i1; else cout << -1;
        return 0;
    }

    if (i1 < s1){
        ll x;
        for (x = 1; x < MAXN * 10; x++){
        //    cerr << c2 << ", " << c1<< endl;
            if ((i2 - s1) % c1 == i1 - s1 && i2 > s1) break;
                i2 += c2;
            }
            if ((i2 - s1) % c1 == i1 - s1 && i2 > s1) cout << i2 << endl;
                else cout << -1;
            return 0;
    }

    if (i2 < s2){
        ll x;
        for (x = 1; x < MAXN * 10; x++){
    //    cerr << c2 << ", " << c1<< endl;
            if ((i1 - s2) % c2 == i2 - s2 && i1 > s2) break;
            i1 += c1;
        }
        if ((i1 - s2) % c2 == i2 - s2 && i1 > s2) cout << i1 << endl;
            else cout << -1;
        return 0;
    }

    ll x;
    for (x = 1; x < MAXN * 10; x++){
    //    cerr << c2 << ", " << c1<< endl;
        if ((i2 - s1) % c1 == i1 - s1 && i2 > s1) break;
        i2 += c2;
    }
    if ((i2 - s1) % c1 == i1 - s1 && i2 > s1) cout << i2 << endl;
    else cout << -1;

}


#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <ctime>
#include <cmath>
#include <algorithm>
#include <vector>
#include <queue>
#include <deque>

#define INF (1<<30)

using namespace std;
typedef long long LL;
LL mod, h1, r1, h2, r2, s, x, y, xx, yy, a1, b1, a2, b2, d, r, i;
LL v1[1000005], v2[1000005];
LL vv1[1000005], vv2[1000005];

LL gcd(LL a, LL b)
{
    if(a == 0)
        return b;
    return gcd(b % a, a);
}

int main()
{
    //freopen("1.in", "r", stdin);
    //freopen("1.out", "w", stdout);

    scanf("%I64d", &mod);
    scanf("%I64d%I64d", &h1, &r1);
    scanf("%I64d%I64d", &a1, &b1);
    scanf("%I64d%I64d", &h2, &r2);
    scanf("%I64d%I64d", &a2, &b2);

    x = h1;
    i = 0;
    while(!v1[x] || !vv1[x])
    {
        if(!v1[x])
            v1[x] = i;
        else
            vv1[x] = i;
        i++;
        x = (a1 * x + b1) % mod;
    }

    y = h2;
    i = 0;
    while(!v2[y] || !vv2[y])
    {
        if(!v2[y])
            v2[y] = i;
        else
            vv2[y] = i;
        i++;
        y = (a2 * y + b2) % mod;
    }

    x = v1[r1]; y = v2[r2];
    xx = vv1[r1] - x; yy = vv2[r2] - y;
    if(x && y)
    {
        if(x == y)
            printf("%I64d", x);
        else
        {
            for(i = 0; i <= 50000000; i++)
                if((x + xx * i - y) % yy == 0)
                {
                    printf("%I64d", x + xx * i);
                    return 0;
                }
            printf("-1");
            return 0;
        }
    }
    else
        printf("-1");
    return 0;
}


#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <ctime>
#include <cmath>
#include <algorithm>
#include <vector>
#include <queue>
#include <deque>

#define INF (1<<30)

using namespace std;
typedef long long LL;
LL mod, h1, r1, h2, r2, s, x, y, xx, yy, a1, b1, a2, b2, d, r, i, mnx, mny;
LL v1[1000005], v2[1000005];
LL vv1[1000005], vv2[1000005];

LL gcd(LL a, LL b)
{
    if(a == 0)
        return b;
    return gcd(b % a, a);
}

int main()
{
    //freopen("1.in", "r", stdin);
    //freopen("1.out", "w", stdout);

    scanf("%I64d", &mod);
    scanf("%I64d%I64d", &h1, &r1);
    scanf("%I64d%I64d", &a1, &b1);
    scanf("%I64d%I64d", &h2, &r2);
    scanf("%I64d%I64d", &a2, &b2);

    x = h1;
    i = 0;
    while(!v1[x] || !vv1[x])
    {
        if(!v1[x])
            v1[x] = i;
        else
            vv1[x] = i;
        i++;
        x = (a1 * x + b1) % mod;
    }

    y = h2;
    i = 0;
    while(!v2[y] || !vv2[y])
    {
        if(!v2[y])
            v2[y] = i;
        else
            vv2[y] = i;
        i++;
        y = (a2 * y + b2) % mod;
    }

    x = v1[r1]; y = v2[r2];
    xx = vv1[r1] - x; yy = vv2[r2] - y;
    if(x && y)
    {
        if(x == y)
            printf("%I64d", x);
        else
        {
            mnx = INF;
            mny = INF;
            for(i = 1; i <= 5000000; i++)
                if((x + xx * i - y) % yy == 0)
                {
                    mnx = i;
                    break;
                }
            for(i = 1; i <= 5000000; i++)
                if((y + yy * i - x) % xx == 0)
                {
                    mny = i;
                    break;
                }
            printf("%I64d", min(x + xx * mnx, y + yy * mny));
            return 0;
        }
    }
    else
        printf("-1");
    return 0;
}


#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <ctime>
#include <cmath>
#include <algorithm>
#include <vector>
#include <queue>
#include <deque>

#define INF (1<<30)

using namespace std;
typedef long long LL;
LL mod, h1, r1, h2, r2, s, x, y, xx, yy, a1, b1, a2, b2, d, r, i;
LL v1[1000005], v2[1000005];
LL vv1[1000005], vv2[1000005];

LL gcd(LL a, LL b)
{
    if(a == 0)
        return b;
    return gcd(b % a, a);
}

int main()
{
    //freopen("1.in", "r", stdin);
    //freopen("1.out", "w", stdout);

    scanf("%I64d", &mod);
    scanf("%I64d%I64d", &h1, &r1);
    scanf("%I64d%I64d", &a1, &b1);
    scanf("%I64d%I64d", &h2, &r2);
    scanf("%I64d%I64d", &a2, &b2);

    x = h1;
    i = 0;
    while(!v1[x] || !vv1[x])
    {
        if(!v1[x])
            v1[x] = i;
        else
            vv1[x] = i;
        i++;
        x = (a1 * x + b1) % mod;
    }

    y = h2;
    i = 0;
    while(!v2[y] || !vv2[y])
    {
        if(!v2[y])
            v2[y] = i;
        else
            vv2[y] = i;
        i++;
        y = (a2 * y + b2) % mod;
    }

    x = v1[r1]; y = v2[r2];
    xx = vv1[r1] - x; yy = vv2[r2] - y;
    if(x && y)
    {
        if(x == y)
            printf("%I64d", x);
        else
        {
            for(i = 0; i <= 1000000000; i++)
                if((x + xx * i - y) % yy == 0)
                {
                    printf("%I64d", x + xx * i);
                    return 0;
                }
            printf("-1");
            return 0;
        }
    }
    else
        printf("-1");
    return 0;
}


#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <ctime>
#include <cmath>
#include <algorithm>
#include <vector>
#include <queue>
#include <deque>

#define INF (1<<30)

using namespace std;
typedef long long LL;
LL mod, h1, r1, h2, r2, s, x, y, xx, yy, a1, b1, a2, b2, d, r, mnx, mny, i;
LL v1[1000005], v2[1000005];

LL gcd(LL a, LL b)
{
    if(a == 0)
        return b;
    return gcd(b % a, a);
}

int main()
{
    //freopen("1.in", "r", stdin);
    //freopen("1.out", "w", stdout);

    scanf("%I64d", &mod);
    scanf("%I64d%I64d", &h1, &r1);
    scanf("%I64d%I64d", &a1, &b1);
    scanf("%I64d%I64d", &h2, &r2);
    scanf("%I64d%I64d", &a2, &b2);

    x = h1;
    i = 0;
    while(!v1[x])
    {
        v1[x] = i;
        i++;
        x = (a1 * x + b1) % mod;
    }

    y = h2;
    i = 0;
    while(!v2[y])
    {

        v2[y] = i;
        i++;
        y = (a2 * y + b2) % mod;
    }

    x = v1[r1]; y = v2[r2];
    xx = v1[h1]; yy = v2[r2];
    if(x && y)
    {
        for(i = 0; i <= 5000000; i++)
            if((xx * i + x - y) % yy == 0)
            {
                mnx = i;
                break;
            }
        for(i = 0; i <= 5000000; i++)
            if((yy * i + y - x) % xx == 0)
            {
                mny = i;
                break;
            }
        printf("%I64d", min(xx * mnx + x, yy * mny + y));
    }
    else
        printf("-1");
    return 0;
}


#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <ctime>
#include <cmath>
#include <algorithm>
#include <vector>
#include <queue>
#include <deque>

#define INF (1<<30)

using namespace std;
typedef long long LL;
LL mod, h1, r1, h2, r2, s, x, y, xx, yy, a1, b1, a2, b2, d, r, i;
LL v1[1000005], v2[1000005];
LL vv1[1000005], vv2[1000005];

LL gcd(LL a, LL b)
{
    if(a == 0)
        return b;
    return gcd(b % a, a);
}

int main()
{
    //freopen("1.in", "r", stdin);
    //freopen("1.out", "w", stdout);

    scanf("%I64d", &mod);
    scanf("%I64d%I64d", &h1, &r1);
    scanf("%I64d%I64d", &a1, &b1);
    scanf("%I64d%I64d", &h2, &r2);
    scanf("%I64d%I64d", &a2, &b2);

    x = h1;
    i = 0;
    while(!v1[x] || !vv1[x])
    {
        if(!v1[x])
            v1[x] = i;
        else
            vv1[x] = i;
        i++;
        x = (a1 * x + b1) % mod;
    }

    y = h2;
    i = 0;
    while(!v2[y] || !vv2[y])
    {
        if(!v2[y])
            v2[y] = i;
        else
            vv2[y] = i;
        i++;
        y = (a2 * y + b2) % mod;
    }

    x = v1[r1]; y = v2[r2];
    xx = vv1[r1] - x; yy = vv2[r2] - y;
    if(x && y)
    {
        if(x == y)
            printf("%I64d", x);
        else
        {
            for(i = 0; i <= 5000000; i++)
                if((x + xx * i - y) % yy == 0)
                {
                    printf("%I64d", x + xx * i);
                    return 0;
                }
            printf("-1");
            return 0;
        }
    }
    else
        printf("-1");
    return 0;
}


#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <ctime>
#include <cmath>
#include <algorithm>
#include <vector>
#include <queue>
#include <deque>

#define INF (1<<30)

using namespace std;
typedef long long LL;
LL mod, h1, r1, h2, r2, s, x, y, xx, yy, a1, b1, a2, b2, d, r, i, a, b, c;
LL v1[1000005], v2[1000005];
LL vv1[1000005], vv2[1000005];

LL gcd( LL A, LL B, LL &X, LL &Y )
{
    if (B == 0)
    {
        X = 1;
        Y = 0;
        return A;
    }

    LL X0, Y0, D;
    D = gcd( B, A % B, X0, Y0 );

    X = Y0;
    Y = X0 - (A / B) * Y0;
    return D;
}

int main()
{
    //freopen("1.in", "r", stdin);
    //freopen("1.out", "w", stdout);

    scanf("%I64d", &mod);
    scanf("%I64d%I64d", &h1, &r1);
    scanf("%I64d%I64d", &a1, &b1);
    scanf("%I64d%I64d", &h2, &r2);
    scanf("%I64d%I64d", &a2, &b2);

    x = h1;
    i = 0;
    while(!v1[x] || !vv1[x])
    {
        if(!v1[x])
            v1[x] = i;
        else
            vv1[x] = i;
        i++;
        x = (a1 * x + b1) % mod;
    }

    y = h2;
    i = 0;
    while(!v2[y] || !vv2[y])
    {
        if(!v2[y])
            v2[y] = i;
        else
            vv2[y] = i;
        i++;
        y = (a2 * y + b2) % mod;
    }

    x = v1[r1]; y = v2[r2];
    xx = vv1[r1] - x; yy = vv2[r2] - y;
    if(x && y)
    {
        if(x == y)
            printf("%I64d", x);
        else
        {
            a = b = 0;
            c = y - x;
            d = gcd(xx, -yy, a, b);
            if(c % d != 0)
            {
                printf("-1");
                return 0;
            }
            LL aa, bb, aaa;
            if(a < 0)
            {
                aa = a;
                bb = b;
                aaa = abs(bb / (-yy));
                if(bb % (-yy) != 0)
                    aaa++;
                a = aa - xx * aaa;
                b = bb + (-yy) * aaa;
            }
            if(b < 0)
            {
                aa = a;
                bb = b;
                aaa = abs(aa / xx);
                if(aa % xx != 0)
                    aaa++;
                a = aa + xx * aaa;
                b = bb - (-yy) * aaa;
            }
            a = a * (c / d);
            b = b * (c / d);
            printf("%I64d", xx * a + x);

            /*for(i = 0; i <= 5000000; i++)
                if((x + xx * i - y) % yy == 0)
                {
                    printf("%I64d", x + xx * i);
                    return 0;
                }
            printf("-1");
            return 0;*/
        }
    }
    else
        printf("-1");
    return 0;
}


#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <ctime>
#include <cmath>
#include <algorithm>
#include <vector>
#include <queue>
#include <deque>

#define INF (1<<30)

using namespace std;
typedef long long LL;
LL mod, h1, r1, h2, r2, s, x, y, xx, yy, a1, b1, a2, b2, d, r, i;
LL v1[1000005], v2[1000005];
LL vv1[1000005], vv2[1000005];

LL gcd(LL a, LL b)
{
    if(a == 0)
        return b;
    return gcd(b % a, a);
}

int main()
{
    //freopen("1.in", "r", stdin);
    //freopen("1.out", "w", stdout);

    scanf("%I64d", &mod);
    scanf("%I64d%I64d", &h1, &r1);
    scanf("%I64d%I64d", &a1, &b1);
    scanf("%I64d%I64d", &h2, &r2);
    scanf("%I64d%I64d", &a2, &b2);

    x = h1;
    i = 0;
    while(!v1[x] || !vv1[x])
    {
        if(!v1[x])
            v1[x] = i;
        else
            vv1[x] = i;
        i++;
        x = (a1 * x + b1) % mod;
    }

    y = h2;
    i = 0;
    while(!v2[y] || !vv2[y])
    {
        if(!v2[y])
            v2[y] = i;
        else
            vv2[y] = i;
        i++;
        y = (a2 * y + b2) % mod;
    }

    x = v1[r1]; y = v2[r2];
    xx = vv1[r1] - x; yy = vv2[r2] - y;
    if(x && y)
    {
        if(x == y)
        {
            printf("%I64d", x);
            return 0;
        }
        if(!xx && !yy)
        {
            printf("-1");
            return 0;
        }
        else if(!xx)
        {
            if(x > y && (x - y) % yy == 0)
            {
                printf("%I64d", x);
                return 0;
            }
            printf("-1");
            return 0;
        }
        else if(!yy)
        {
            if(x < y && (y - x) % xx == 0)
            {
                printf("%I64d", y);
                return 0;
            }
            printf("-1");
            return 0;
        }
        else
        {
            for(i = 0; i <= 10000000000; i++)
                if((x + xx * i - y) % yy == 0)
                {
                    printf("%I64d", x + xx * i);
                    return 0;
                }
            printf("-1");
            return 0;
        }
    }
    else
        printf("-1");
    return 0;
}


#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <ctime>
#include <cmath>
#include <algorithm>
#include <vector>
#include <queue>
#include <deque>

#define INF (1<<30)

using namespace std;
typedef long long LL;
LL mod, h1, r1, h2, r2, s, x, y, a1, b1, a2, b2, d, i;
LL v1[1000005], v2[1000005];

LL gcd(LL a, LL b)
{
    if(a == 0)
        return b;
    return gcd(b % a, a);
}

int main()
{
    //freopen("1.in", "r", stdin);
    //freopen("1.out", "w", stdout);
    scanf("%I64d", &mod);
    scanf("%I64d%I64d", &h1, &r1);
    scanf("%I64d%I64d", &a1, &b1);
    scanf("%I64d%I64d", &h2, &r2);
    scanf("%I64d%I64d", &a2, &b2);

    x = h1;
    i = 0;
    while(!v1[x])
    {
        v1[x] = i;
        i++;
        x = (a1 * x + b1) % mod;
    }

    y = h2;
    i = 0;
    while(!v2[x])
    {
        v2[y] = i;
        i++;
        y = (a2 * y + b2) % mod;
    }

    x = v1[r1]; y = v2[r2];
    if(x && y)
    {
        d = gcd(x, y);
        printf("%I64d", x * y / d);
    }
    else
        printf("-1");
    return 0;
}


#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <ctime>
#include <cmath>
#include <algorithm>
#include <vector>
#include <queue>
#include <deque>

#define INF (1<<30)

using namespace std;
typedef long long LL;
LL mod, h1, r1, h2, r2, s, x, y, xx, yy, a1, b1, a2, b2, d, r, mnx, mny, i;
LL v1[1000005], v2[1000005];

LL gcd(LL a, LL b)
{
    if(a == 0)
        return b;
    return gcd(b % a, a);
}

int main()
{
    //freopen("1.in", "r", stdin);
    //freopen("1.out", "w", stdout);

    scanf("%I64d", &mod);
    scanf("%I64d%I64d", &h1, &r1);
    scanf("%I64d%I64d", &a1, &b1);
    scanf("%I64d%I64d", &h2, &r2);
    scanf("%I64d%I64d", &a2, &b2);

    x = h1;
    i = 0;
    while(!v1[x])
    {
        v1[x] = i;
        i++;
        x = (a1 * x + b1) % mod;
    }

    y = h2;
    i = 0;
    while(!v2[y])
    {

        v2[y] = i;
        i++;
        y = (a2 * y + b2) % mod;
    }

    x = v1[r1]; y = v2[r2];
    xx = v1[h1] - 1; yy = v2[r2] - 1;
    if(x && y)
    {
        for(i = 0; i <= 5000000; i++)
            if((xx * i + x - y) % yy == 0)
            {
                mnx = i;
                break;
            }
        for(i = 0; i <= 5000000; i++)
            if((yy * i + y - x) % xx == 0)
            {
                mny = i;
                break;
            }
        printf("%I64d", min(xx * mnx + x, yy * mny + y));
    }
    else
        printf("-1");
    return 0;
}


#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <ctime>
#include <cmath>
#include <algorithm>
#include <vector>
#include <queue>
#include <deque>

#define INF (1<<30)

using namespace std;
typedef long long LL;
LL mod, h1, r1, h2, r2, s, x, y, xx, yy, a1, b1, a2, b2, d, r, i;
LL v1[1000005], v2[1000005];
LL vv1[1000005], vv2[1000005];

LL gcd(LL a, LL b)
{
    if(a == 0)
        return b;
    return gcd(b % a, a);
}

int main()
{
    //freopen("1.in", "r", stdin);
    //freopen("1.out", "w", stdout);

    scanf("%I64d", &mod);
    scanf("%I64d%I64d", &h1, &r1);
    scanf("%I64d%I64d", &a1, &b1);
    scanf("%I64d%I64d", &h2, &r2);
    scanf("%I64d%I64d", &a2, &b2);

    x = h1;
    i = 0;
    while(!v1[x] || !vv1[x])
    {
        if(!v1[x])
            v1[x] = i;
        else
            vv1[x] = i;
        i++;
        x = (a1 * x + b1) % mod;
    }

    y = h2;
    i = 0;
    while(!v2[y] || !vv2[y])
    {
        if(!v2[y])
            v2[y] = i;
        else
            vv2[y] = i;
        i++;
        y = (a2 * y + b2) % mod;
    }

    x = v1[r1]; y = v2[r2];
    xx = vv1[r1] - x; yy = vv2[r2] - y;
    if(x && y)
    {
        if(x == y)
            printf("%I64d", x);
        else
        {
            for(i = 0; i <= 1000000; i++)
                if((x + xx * i - y) % yy == 0)
                {
                    printf("%I64d", x + xx * i);
                    return 0;
                }
            printf("-1");
            return 0;
        }
    }
    else
        printf("-1");
    return 0;
}


#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <ctime>
#include <cmath>
#include <algorithm>
#include <vector>
#include <queue>
#include <deque>

#define INF (1<<30)

using namespace std;
typedef long long LL;
LL mod, h1, r1, h2, r2, s, x, y, xx, yy, a1, b1, a2, b2, d, r, i, mnx, mny;
LL v1[1000005], v2[1000005];
LL vv1[1000005], vv2[1000005];

LL gcd(LL a, LL b)
{
    if(a == 0)
        return b;
    return gcd(b % a, a);
}

int main()
{
    //freopen("1.in", "r", stdin);
    //freopen("1.out", "w", stdout);

    scanf("%I64d", &mod);
    scanf("%I64d%I64d", &h1, &r1);
    scanf("%I64d%I64d", &a1, &b1);
    scanf("%I64d%I64d", &h2, &r2);
    scanf("%I64d%I64d", &a2, &b2);

    x = h1;
    i = 0;
    while(!v1[x] || !vv1[x])
    {
        if(!v1[x])
            v1[x] = i;
        else
            vv1[x] = i;
        i++;
        x = (a1 * x + b1) % mod;
    }

    y = h2;
    i = 0;
    while(!v2[y] || !vv2[y])
    {
        if(!v2[y])
            v2[y] = i;
        else
            vv2[y] = i;
        i++;
        y = (a2 * y + b2) % mod;
    }

    x = v1[r1]; y = v2[r2];
    xx = vv1[r1] - x; yy = vv2[r2] - y;
    if(x && y)
    {
        if(x == y)
            printf("%I64d", x);
        else
        {
            mnx = INF;
            mny = INF;
            for(i = 1; i <= 5000000; i++)
                if((x + xx * i - y) % yy == 0 && x + xx * i - y >= 0)
                {
                    mnx = i;
                    break;
                }
            for(i = 1; i <= 5000000; i++)
                if((y + yy * i - x) % xx == 0 && y + yy * i - x >= 0)
                {
                    mny = i;
                    break;
                }
            printf("%I64d", min(x + xx * mnx, y + yy * mny));
            return 0;
        }
    }
    else
        printf("-1");
    return 0;
}


#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <ctime>
#include <cmath>
#include <algorithm>
#include <vector>
#include <queue>
#include <deque>

#define INF (1<<30)

using namespace std;
typedef long long LL;
LL mod, h1, r1, h2, r2, s, x, y, xx, yy, a1, b1, a2, b2, d, r, i, mnx, mny;
LL v1[1000005], v2[1000005];
LL vv1[1000005], vv2[1000005];

LL gcd(LL a, LL b)
{
    if(a == 0)
        return b;
    return gcd(b % a, a);
}

int main()
{
    //freopen("1.in", "r", stdin);
    //freopen("1.out", "w", stdout);

    scanf("%I64d", &mod);
    scanf("%I64d%I64d", &h1, &r1);
    scanf("%I64d%I64d", &a1, &b1);
    scanf("%I64d%I64d", &h2, &r2);
    scanf("%I64d%I64d", &a2, &b2);

    x = h1;
    i = 0;
    while(!v1[x] || !vv1[x])
    {
        if(!v1[x])
            v1[x] = i;
        else
            vv1[x] = i;
        i++;
        x = (a1 * x + b1) % mod;
    }

    y = h2;
    i = 0;
    while(!v2[y] || !vv2[y])
    {
        if(!v2[y])
            v2[y] = i;
        else
            vv2[y] = i;
        i++;
        y = (a2 * y + b2) % mod;
    }

    x = v1[r1]; y = v2[r2];
    xx = vv1[r1] - x; yy = vv2[r2] - y;
    if(x && y)
    {
        if(x == y)
            printf("%I64d", x);
        else
        {
            for(i = 0; i <= 5000000; i++)
                if((x + xx * i - y) % yy == 0)
                {
                    mnx = i;
                    break;
                }
            for(i = 0; i <= 5000000; i++)
                if((y + yy * i - x) % xx == 0)
                {
                    mny = i;
                    break;
                }
            printf("%I64d", min(x + xx * mnx, y + yy * mny));
            return 0;
        }
    }
    else
        printf("-1");
    return 0;
}


#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <ctime>
#include <cmath>
#include <algorithm>
#include <vector>
#include <queue>
#include <deque>

#define INF (1<<30)

using namespace std;
typedef long long LL;
LL mod, h1, r1, h2, r2, s, x, y, xx, yy, a1, b1, a2, b2, d, r, i, mnx, mny;
LL v1[1000005], v2[1000005];
LL vv1[1000005], vv2[1000005];

LL gcd(LL a, LL b)
{
    if(a == 0)
        return b;
    return gcd(b % a, a);
}

int main()
{
    //freopen("1.in", "r", stdin);
    //freopen("1.out", "w", stdout);

    scanf("%I64d", &mod);
    scanf("%I64d%I64d", &h1, &r1);
    scanf("%I64d%I64d", &a1, &b1);
    scanf("%I64d%I64d", &h2, &r2);
    scanf("%I64d%I64d", &a2, &b2);

    x = h1;
    i = 0;
    while(!v1[x] || !vv1[x])
    {
        if(!v1[x])
            v1[x] = i;
        else
            vv1[x] = i;
        i++;
        x = (a1 * x + b1) % mod;
    }

    y = h2;
    i = 0;
    while(!v2[y] || !vv2[y])
    {
        if(!v2[y])
            v2[y] = i;
        else
            vv2[y] = i;
        i++;
        y = (a2 * y + b2) % mod;
    }

    x = v1[r1]; y = v2[r2];
    xx = vv1[r1] - x; yy = vv2[r2] - y;
    if(x && y)
    {
        if(x == y)
            printf("%I64d", x);
        else
        {
            mnx = INF;
            mny = INF;
            for(i = 1; i <= 5000000; i++)
                if((x + xx * i - y) % yy == 0)
                {
                    mnx = i;
                    break;
                }
            for(i = 1; i <= 5000000; i++)
                if((y + yy * i - x) % xx == 0)
                {
                    mny = i;
                    break;
                }
            if(mnx == INF && mny == INF)
            {
                printf("-1");
                return 0;
            }
            printf("%I64d", min(x + xx * mnx, y + yy * mny));
            return 0;
        }
    }
    else
        printf("-1");
    return 0;
}


#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <ctime>
#include <cmath>
#include <algorithm>
#include <vector>
#include <queue>
#include <deque>

#define INF (1<<30)

using namespace std;
typedef long long LL;
LL mod, h1, r1, h2, r2, s, x, y, xx, yy, a1, b1, a2, b2, d, r, i, a, b, c;
LL v1[1000005], v2[1000005];
LL vv1[1000005], vv2[1000005];

LL gcd( LL A, LL B, LL &X, LL &Y )
{
    if (B == 0)
    {
        X = 1;
        Y = 0;
        return A;
    }

    LL X0, Y0, D;
    D = gcd( B, A % B, X0, Y0 );

    X = Y0;
    Y = X0 - (A / B) * Y0;
    return D;
}

int main()
{
    //freopen("1.in", "r", stdin);
    //freopen("1.out", "w", stdout);

    scanf("%I64d", &mod);
    scanf("%I64d%I64d", &h1, &r1);
    scanf("%I64d%I64d", &a1, &b1);
    scanf("%I64d%I64d", &h2, &r2);
    scanf("%I64d%I64d", &a2, &b2);

    x = h1;
    i = 0;
    while(!v1[x] || !vv1[x])
    {
        if(!v1[x])
            v1[x] = i;
        else
            vv1[x] = i;
        i++;
        x = (a1 * x + b1) % mod;
    }

    y = h2;
    i = 0;
    while(!v2[y] || !vv2[y])
    {
        if(!v2[y])
            v2[y] = i;
        else
            vv2[y] = i;
        i++;
        y = (a2 * y + b2) % mod;
    }

    x = v1[r1]; y = v2[r2];
    xx = vv1[r1] - x; yy = vv2[r2] - y;
    if(x && y)
    {
        if(x == y)
            printf("%I64d", x);
        else
        {
            a = b = 0;
            c = y - x;
            d = gcd(xx, -yy, a, b);
            if(c % d != 0)
            {
                printf("-1");
                return 0;
            }
            a = a * (c / d);
            b = b * (c / d);
            printf("%I64d", xx * a + x);

            /*for(i = 0; i <= 5000000; i++)
                if((x + xx * i - y) % yy == 0)
                {
                    printf("%I64d", x + xx * i);
                    return 0;
                }
            printf("-1");
            return 0;*/
        }
    }
    else
        printf("-1");
    return 0;
}


#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <ctime>
#include <cmath>
#include <algorithm>
#include <vector>
#include <queue>
#include <deque>

#define INF (1<<30)

using namespace std;
typedef long long LL;
LL mod, h1, r1, h2, r2, s, x, y, xx, yy, a1, b1, a2, b2, d, r, i;
LL v1[1000005], v2[1000005];
LL vv1[1000005], vv2[1000005];

LL gcd(LL a, LL b)
{
    if(a == 0)
        return b;
    return gcd(b % a, a);
}

int main()
{
    //freopen("1.in", "r", stdin);
    //freopen("1.out", "w", stdout);

    scanf("%I64d", &mod);
    scanf("%I64d%I64d", &h1, &r1);
    scanf("%I64d%I64d", &a1, &b1);
    scanf("%I64d%I64d", &h2, &r2);
    scanf("%I64d%I64d", &a2, &b2);

    x = h1;
    i = 0;
    while(!v1[x] || !vv1[x])
    {
        if(!v1[x])
            v1[x] = i;
        else
            vv1[x] = i;
        i++;
        x = (a1 * x + b1) % mod;
    }

    y = h2;
    i = 0;
    while(!v2[y] || !vv2[y])
    {
        if(!v2[y])
            v2[y] = i;
        else
            vv2[y] = i;
        i++;
        y = (a2 * y + b2) % mod;
    }

    x = v1[r1]; y = v2[r2];
    xx = vv1[r1] - x; yy = vv2[r2] - y;
    if(x && y)
    {
        if(x == y)
        {
            printf("%I64d", x);
            return 0;
        }
        if(!xx && !yy)
        {
            printf("-1");
            return 0;
        }
        else if(!xx)
        {
            if(x > y && (x - y) % yy == 0)
            {
                printf("%I64d", x);
                return 0;
            }
            printf("-1");
            return 0;
        }
        else if(!yy)
        {
            if(x < y && (y - x) % xx == 0)
            {
                printf("%I64d", y);
                return 0;
            }
            printf("-1");
            return 0;
        }
        else
        {
            for(i = 0; i <= 1000000000; i++)
                if((x + xx * i - y) % yy == 0)
                {
                    printf("%I64d", x + xx * i);
                    return 0;
                }
            printf("-1");
            return 0;
        }
    }
    else
        printf("-1");
    return 0;
}


#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <ctime>
#include <cmath>
#include <algorithm>
#include <vector>
#include <queue>
#include <deque>

#define INF (1<<30)

using namespace std;
typedef long long LL;
LL mod, h1, r1, h2, r2, s, x, y, xx, yy, a1, b1, a2, b2, d, r, i, mnx, mny;
LL v1[1000005], v2[1000005];
LL vv1[1000005], vv2[1000005];

LL gcd(LL a, LL b)
{
    if(a == 0)
        return b;
    return gcd(b % a, a);
}

int main()
{
    //freopen("1.in", "r", stdin);
    //freopen("1.out", "w", stdout);

    scanf("%I64d", &mod);
    scanf("%I64d%I64d", &h1, &r1);
    scanf("%I64d%I64d", &a1, &b1);
    scanf("%I64d%I64d", &h2, &r2);
    scanf("%I64d%I64d", &a2, &b2);

    x = h1;
    i = 0;
    while(!v1[x] || !vv1[x])
    {
        if(!v1[x])
            v1[x] = i;
        else
            vv1[x] = i;
        i++;
        x = (a1 * x + b1) % mod;
    }

    y = h2;
    i = 0;
    while(!v2[y] || !vv2[y])
    {
        if(!v2[y])
            v2[y] = i;
        else
            vv2[y] = i;
        i++;
        y = (a2 * y + b2) % mod;
    }

    x = v1[r1]; y = v2[r2];
    xx = vv1[r1] - x; yy = vv2[r2] - y;
    if(x && y)
    {
        if(x == y)
            printf("%I64d", x);
        else
        {
            mnx = INF;
            mny = INF;
            for(i = 1; i <= 5000000; i++)
                if((x + xx * i - y) % yy == 0 && x + xx * i - y >= 0)
                {
                    mnx = i;
                    break;
                }
            for(i = 1; i <= 5000000; i++)
                if((y + yy * i - x) % xx == 0 && y + yy * i - x >= 0)
                {
                    mny = i;
                    break;
                }
            if(mnx == INF && mny == INF)
            {
                printf("-1");
                return 0;
            }
            printf("%I64d", min(x + xx * mnx, y + yy * mny));
            return 0;
        }
    }
    else
        printf("-1");
    return 0;
}


#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <ctime>
#include <cmath>
#include <algorithm>
#include <vector>
#include <queue>
#include <deque>

#define INF (1<<30)

using namespace std;
typedef long long LL;
LL mod, h1, r1, h2, r2, s, x, y, xx, yy, a1, b1, a2, b2, d, r, i, mnx, mny;
LL v1[1000005], v2[1000005];
LL vv1[1000005], vv2[1000005];

LL gcd(LL a, LL b)
{
    if(a == 0)
        return b;
    return gcd(b % a, a);
}

int main()
{
    //freopen("1.in", "r", stdin);
    //freopen("1.out", "w", stdout);

    scanf("%I64d", &mod);
    scanf("%I64d%I64d", &h1, &r1);
    scanf("%I64d%I64d", &a1, &b1);
    scanf("%I64d%I64d", &h2, &r2);
    scanf("%I64d%I64d", &a2, &b2);

    x = h1;
    i = 0;
    while(!v1[x] || !vv1[x])
    {
        if(!v1[x])
            v1[x] = i;
        else
            vv1[x] = i;
        i++;
        x = (a1 * x + b1) % mod;
    }

    y = h2;
    i = 0;
    while(!v2[y] || !vv2[y])
    {
        if(!v2[y])
            v2[y] = i;
        else
            vv2[y] = i;
        i++;
        y = (a2 * y + b2) % mod;
    }

    x = v1[r1]; y = v2[r2];
    xx = vv1[r1] - x; yy = vv2[r2] - y;
    if(x && y)
    {
        if(x == y)
            printf("%I64d", x);
        if(xx <= 0 || yy <= 0)
        {
            printf("-1");
            return 0;
        }
        else
        {
            mnx = INF;
            mny = INF;
            for(i = 1; i <= 5000000; i++)
                if((x + xx * i - y) % yy == 0 && x + xx * i - y >= 0)
                {
                    mnx = i;
                    break;
                }
            for(i = 1; i <= 5000000; i++)
                if((y + yy * i - x) % xx == 0 && y + yy * i - x >= 0)
                {
                    mny = i;
                    break;
                }
            if(mnx == INF && mny == INF)
            {
                printf("-1");
                return 0;
            }
            printf("%I64d", min(x + xx * mnx, y + yy * mny));
            return 0;
        }
    }
    else
        printf("-1");
    return 0;
}


#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <ctime>
#include <cmath>
#include <algorithm>
#include <vector>
#include <queue>
#include <deque>

#define INF (1<<30)

using namespace std;
typedef long long LL;
LL mod, h1, r1, h2, r2, s, x, y, xx, yy, a1, b1, a2, b2, d, r, mnx, mny, i;
LL v1[1000005], v2[1000005];

LL gcd(LL a, LL b)
{
    if(a == 0)
        return b;
    return gcd(b % a, a);
}

int main()
{
    //freopen("1.in", "r", stdin);
    //freopen("1.out", "w", stdout);

    scanf("%I64d", &mod);
    scanf("%I64d%I64d", &h1, &r1);
    scanf("%I64d%I64d", &a1, &b1);
    scanf("%I64d%I64d", &h2, &r2);
    scanf("%I64d%I64d", &a2, &b2);

    x = h1;
    i = 0;
    while(!v1[x])
    {
        v1[x] = i;
        i++;
        x = (a1 * x + b1) % mod;
    }

    y = h2;
    i = 0;
    while(!v2[y])
    {

        v2[y] = i;
        i++;
        y = (a2 * y + b2) % mod;
    }

    x = v1[r1]; y = v2[r2];
    xx = v1[h1]; yy = v2[h2];
    if(x && y)
    {
        for(i = 0; i <= 5000000; i++)
            if((xx * i + x - y) % yy == 0)
            {
                mnx = i;
                break;
            }
        for(i = 0; i <= 5000000; i++)
            if((yy * i + y - x) % xx == 0)
            {
                mny = i;
                break;
            }
        printf("%I64d", min(xx * mnx + x, yy * mny + y));
    }
    else
        printf("-1");
    return 0;
}


#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <ctime>
#include <cmath>
#include <algorithm>
#include <vector>
#include <queue>
#include <deque>

#define INF (1<<30)

using namespace std;
typedef long long LL;
LL mod, h1, r1, h2, r2, s, x, y, xx, yy, a1, b1, a2, b2, d, r, i, mnx, mny;
LL v1[1000005], v2[1000005];
LL vv1[1000005], vv2[1000005];

LL gcd(LL a, LL b)
{
    if(a == 0)
        return b;
    return gcd(b % a, a);
}

int main()
{
    //freopen("1.in", "r", stdin);
    //freopen("1.out", "w", stdout);

    scanf("%I64d", &mod);
    scanf("%I64d%I64d", &h1, &r1);
    scanf("%I64d%I64d", &a1, &b1);
    scanf("%I64d%I64d", &h2, &r2);
    scanf("%I64d%I64d", &a2, &b2);

    x = h1;
    i = 0;
    while(!v1[x] || !vv1[x])
    {
        if(!v1[x])
            v1[x] = i;
        else
            vv1[x] = i;
        i++;
        x = (a1 * x + b1) % mod;
    }

    y = h2;
    i = 0;
    while(!v2[y] || !vv2[y])
    {
        if(!v2[y])
            v2[y] = i;
        else
            vv2[y] = i;
        i++;
        y = (a2 * y + b2) % mod;
    }

    x = v1[r1]; y = v2[r2];
    xx = vv1[r1] - x; yy = vv2[r2] - y;
    if(x && y)
    {
        if(x == y)
            printf("%I64d", x);
        else if(xx <= 0 || yy <= 0)
        {
            printf("-1");
            return 0;
        }
        else
        {
            mnx = INF;
            mny = INF;
            for(i = 1; i <= 5000000; i++)
                if((x + xx * i - y) % yy == 0 && x + xx * i - y >= 0)
                {
                    mnx = i;
                    break;
                }
            for(i = 1; i <= 5000000; i++)
                if((y + yy * i - x) % xx == 0 && y + yy * i - x >= 0)
                {
                    mny = i;
                    break;
                }
            if(mnx == INF && mny == INF)
            {
                printf("-1");
                return 0;
            }
            printf("%I64d", min(x + xx * mnx, y + yy * mny));
            return 0;
        }
    }
    else
        printf("-1");
    return 0;
}


#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <ctime>
#include <cmath>
#include <algorithm>
#include <vector>
#include <queue>
#include <deque>

#define INF (1<<30)

using namespace std;
typedef long long LL;
LL mod, h1, r1, h2, r2, s, x, y, xx, yy, a1, b1, a2, b2, d, r, i;
LL v1[1000005], v2[1000005];
LL vv1[1000005], vv2[1000005];

LL gcd(LL a, LL b)
{
    if(a == 0)
        return b;
    return gcd(b % a, a);
}

int main()
{
    //freopen("1.in", "r", stdin);
    //freopen("1.out", "w", stdout);

    scanf("%I64d", &mod);
    scanf("%I64d%I64d", &h1, &r1);
    scanf("%I64d%I64d", &a1, &b1);
    scanf("%I64d%I64d", &h2, &r2);
    scanf("%I64d%I64d", &a2, &b2);

    x = h1;
    i = 0;
    while(!v1[x] || !vv1[x])
    {
        if(!v1[x])
            v1[x] = i;
        else
            vv1[x] = i;
        i++;
        x = (a1 * x + b1) % mod;
    }

    y = h2;
    i = 0;
    while(!v2[y] || !vv2[y])
    {
        if(!v2[y])
            v2[y] = i;
        else
            vv2[y] = i;
        i++;
        y = (a2 * y + b2) % mod;
    }

    x = v1[r1]; y = v2[r2];
    xx = vv1[r1] - x; yy = vv2[r2] - y;
    if(x && y)
    {
        if(x == y)
            printf("%I64d", x);
        else
        {
            for(i = 0; i <= 10000000; i++)
                if((x + xx * i - y) % yy == 0)
                {
                    printf("%I64d", x + xx * i);
                    return 0;
                }
            printf("-1");
            return 0;
        }
    }
    else
        printf("-1");
    return 0;
}


#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <ctime>
#include <cmath>
#include <algorithm>
#include <vector>
#include <queue>
#include <deque>

#define INF (1<<30)

using namespace std;
typedef long long LL;
LL mod, h1, r1, h2, r2, s, x, y, a1, b1, a2, b2, d, i;
LL v1[1000005], v2[1000005];

LL gcd(LL a, LL b)
{
    if(a == 0)
        return b;
    return gcd(b % a, a);
}

int main()
{
    //freopen("1.in", "r", stdin);
    //freopen("1.out", "w", stdout);
    scanf("%I64d", &mod);
    scanf("%I64d%I64d", &h1, &r1);
    scanf("%I64d%I64d", &a1, &b1);
    scanf("%I64d%I64d", &h2, &r2);
    scanf("%I64d%I64d", &a2, &b2);

    x = h1;
    i = 0;
    while(!v1[x])
    {
        v1[x] = i;
        i++;
        x = (a1 * x + b1) % mod;
    }

    y = h2;
    i = 0;
    while(!v2[y])
    {
        v2[y] = i;
        i++;
        y = (a2 * y + b2) % mod;
    }

    x = v1[r1]; y = v2[r2];
    if(x && y)
    {
        d = gcd(x, y);
        printf("%I64d", x * y / d);
    }
    else
        printf("-1");
    return 0;
}


#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <ctime>
#include <cmath>
#include <algorithm>
#include <vector>
#include <queue>
#include <deque>

#define INF (1<<30)

using namespace std;
typedef long long LL;
LL mod, h1, r1, h2, r2, s, x, y, xx, yy, a1, b1, a2, b2, d, r, i, mnx, mny;
LL v1[1000005], v2[1000005];
LL vv1[1000005], vv2[1000005];

LL gcd(LL a, LL b)
{
    if(a == 0)
        return b;
    return gcd(b % a, a);
}

int main()
{
    //freopen("1.in", "r", stdin);
    //freopen("1.out", "w", stdout);

    scanf("%I64d", &mod);
    scanf("%I64d%I64d", &h1, &r1);
    scanf("%I64d%I64d", &a1, &b1);
    scanf("%I64d%I64d", &h2, &r2);
    scanf("%I64d%I64d", &a2, &b2);

    x = h1;
    i = 0;
    while(!v1[x] || !vv1[x])
    {
        if(!v1[x])
            v1[x] = i;
        else
            vv1[x] = i;
        i++;
        x = (a1 * x + b1) % mod;
    }

    y = h2;
    i = 0;
    while(!v2[y] || !vv2[y])
    {
        if(!v2[y])
            v2[y] = i;
        else
            vv2[y] = i;
        i++;
        y = (a2 * y + b2) % mod;
    }

    x = v1[r1]; y = v2[r2];
    xx = vv1[r1] - x; yy = vv2[r2] - y;
    if(x && y)
    {
        if(x == y)
            printf("%I64d", x);
        else if(xx < 0 && yy < 0)
        {
            printf("-1");
            return 0;
        }
        else if(xx < 0)
        {
            if(x > y && (x - y) % yy == 0)
                printf("%I64d", x);
            else
                printf("-1");
        }
        else if(yy < 0)
        {
            if(x < y && (y - x) % xx == 0)
                printf("%I64d", y);
            else
                printf("-1");
        }
        else
        {
            mnx = INF;
            mny = INF;
            for(i = 1; i <= 5000000; i++)
                if((x + xx * i - y) % yy == 0 && x + xx * i - y >= 0)
                {
                    mnx = i;
                    break;
                }
            for(i = 1; i <= 5000000; i++)
                if((y + yy * i - x) % xx == 0 && y + yy * i - x >= 0)
                {
                    mny = i;
                    break;
                }
            if(mnx == INF && mny == INF)
            {
                printf("-1");
                return 0;
            }
            printf("%I64d", min(x + xx * mnx, y + yy * mny));
            return 0;
        }
    }
    else
        printf("-1");
    return 0;
}


#include <bits/stdc++.h>
using namespace std;

#define ll long long
const int INF = 1e9;

ll gcd(ll a,ll b){
	if(b==0)return a;
	return gcd(b,a%b);
}

ll MIN(ll a,ll b){
	if(a>b)return b;
	return a;
}

set<ll> s1;
set<ll> s2;

int main(){

	ll m,h1,a1,x1,y1;
	ll   h2,a2,x2,y2;
	cin>>m;
	cin>>h1>>a1;
	cin>>x1>>y1;
	cin>>h2>>a2;
	cin>>x2>>y2;

	//
	ll ans1=1;
	for(;ans1<=m+1;ans1++){
		h1=x1*h1+y1;
		h1%=m;
		if(h1==a1){
			break;
		}
	}

	ll ans2=1;
	for(;ans2<=m+1;ans2++){
		h2=x2*h2+y2;
		h2%=m;
		if(h2==a2){
			break;
		}
	}

	ll cyc1=1;
	for(;cyc1<=m+1;cyc1++){
		h1=x1*h1+y1;
		h1%=m;
		if(h1==a1){
			break;
		}
	}

	ll cyc2=1;
	for(;cyc2<=m+1;cyc2++){
		h2=x2*h2+y2;
		h2%=m;
		if(h2==a2){
			break;
		}
	}

	bool ok=1;
	if(ans1>m+1||ans2>m+1)ok=0;

	if(ok){
		int end1=100;
		int end2=100;
		if(cyc1>m+1)end1=0;
		if(cyc2>m+1)end2=0;

		ll ans=10000000000000LL;
		for(int i=0;i<=end1;i++){
			for(int j=0;j<=end2;j++){
				ll tmp1=ans1+i*cyc1;
				ll tmp2=ans2+j*cyc2;
				ans=MIN(ans, tmp1*tmp2/gcd(tmp1,tmp2) );
			}
		}
		cout<<ans;
	}else{
		cout<<"-1";
	}

	return 0;
}

/*
#define ll long long
const int INF=1e9;

int mp[110][110];
int lv[110];
bool dp[110][110];
int n;
vector<int> s0[110];
vector<int> s1[110];
int groupcnt;
int pre[110][110];

int re;
int tmp;
void dfs(int u){
//	cout<<"dfsing "<<u<<endl;
	if(lv[u]){
		s0[groupcnt].push_back(u);
		tmp--;
	}else{
		s1[groupcnt].push_back(u);
		tmp++;
	}
	for(int v=1;v<=n;v++){
		if(v==u)continue;
		if(!(mp[u][v]&&mp[v][u])){
			if(lv[v]!=-1){
				if((lv[v]&1)==(lv[u]&1)){
					return;
				}else{
					continue;
				}
			}
			lv[v]=!lv[u];
			dfs(v);
		}
	}
}

void init(){
	memset(mp,0,sizeof(mp));
	memset(lv,-1,sizeof(lv));
	memset(dp,0,sizeof(dp));
	memset(s0,0,sizeof(s0));
	memset(s1,0,sizeof(s1));
}

int main(){
	//int t;
	//cin>>t;
	while(cin>>n){

		init();

		//cin>>n;
		for(int i=1;i<=n;i++){
			int num;
			while(scanf("%d",&num)){
				if(!num)break;
				mp[i][num]=1;
			}
		}

		dp[0][0]=1;
		groupcnt=0;
		for(int i=1;i<=n;i++){
			if(lv[i]==-1){
				lv[i]=0;
				groupcnt++;
				tmp=0;
				dfs(i);
				//cout<<i<<" "<<tmp<<endl;

				for(int j=0;j<=n/2;j++){
					if(dp[groupcnt-1][j]){
						if(j+tmp>=0){
							dp[groupcnt][j+tmp]=1;
							pre[groupcnt][j+tmp]=j;
						}
						if(j-tmp>=0){
							dp[groupcnt][j-tmp]=1;
							pre[groupcnt][j-tmp]=j;
						}
					}
				}
			}
		}

		int ans=0;
		for(int i=0;i<=n;i++){
			if(dp[groupcnt][i]){
				ans=i;break;
			}
		}

		bool ok=1;
		for(int i=1;i<=n;i++){
			for(int j=1;j<=n;j++){
				if(i==j)continue;
				if( !(mp[i][j]&&mp[j][i]) ){
					if(lv[i]==lv[j]){
						ok=0;
					}
				}
			}
		}

		if(ok){
			//cout<<ans<<endl;
			int cur=ans;
			vector<int> ans0;
			vector<int> ans1;
			for(int i=groupcnt;i>=1;i--){
				int p=pre[i][cur];

				vector<int> &big=s0[i];
				vector<int> &small=s1[i];
				if(big.size()<small.size()){
					swap(big,small);
				}

				//cout<<"????size"<<big.size()<<" ��????size"<<small.size()<<endl;

				if(p<cur)swap(big,small);

				for(int j=0;j<big.size();j++){
					ans0.push_back(big[j]);
				}
				for(int j=0;j<small.size();j++){
					ans1.push_back(small[j]);
				}

				cur=p;
			}
			cout<<ans0.size()<<" ";
			for(int i=0;i<ans0.size();i++){
				cout<<ans0[i]<<" ";
			}cout<<endl;
			cout<<ans1.size()<<" ";
			for(int i=0;i<ans1.size();i++){
				cout<<ans1[i]<<" ";
			}cout<<endl;
		}else{
			cout<<"No solution"<<endl;
		}
		//if(t)cout<<endl;
	}
	return 0;
}*/


#include <bits/stdc++.h>
using namespace std;

#define ll long long
const int INF = 1e9;

ll gcd(ll a,ll b){
	return b==0?a:gcd(b,a%b);
}

ll MIN(ll a,ll b){
	if(a>b)return b;
	return a;
}

set<ll> s1;
set<ll> s2;

int main(){

	ll m,h1,a1,x1,y1;
	ll   h2,a2,x2,y2;
	cin>>m;
	cin>>h1>>a1;
	cin>>x1>>y1;
	cin>>h2>>a2;
	cin>>x2>>y2;

	//
	ll ans1=1;
	for(;ans1<=m+10;ans1++){
		h1=x1*h1+y1;
		h1%=m;
		if(h1==a1){
			s1.insert(ans1);
		}
	}

	ll ans2=1;
	for(;ans2<=m+10;ans2++){
		h2=x2*h2+y2;
		h2%=m;
		if(h2==a2){
			s2.insert(ans2);
		}
	}

	bool ok=1;
	if(s1.empty()||s2.empty())ok=0;

	if(ok){
		ll ans=11000000000000LL;

		for(set<ll>::iterator it1=s1.begin();it1!=s1.end();it1++){
			if(*it1>ans)break;
			for(set<ll>::iterator it2=s2.begin();it2!=s2.end();it2++){
				if(*it2>ans)break;
				ll t1=*it1,	t2=*it2;
				ans=MIN(ans, t1*t2/gcd(t1,t2) );
			}
		}

		//ans1*ans2/gcd(ans1,ans2);
		cout<<ans;

	}else{
		cout<<"-1";
	}

	return 0;
}

/*
#define ll long long
const int INF=1e9;

int mp[110][110];
int lv[110];
bool dp[110][110];
int n;
vector<int> s0[110];
vector<int> s1[110];
int groupcnt;
int pre[110][110];

int re;
int tmp;
void dfs(int u){
//	cout<<"dfsing "<<u<<endl;
	if(lv[u]){
		s0[groupcnt].push_back(u);
		tmp--;
	}else{
		s1[groupcnt].push_back(u);
		tmp++;
	}
	for(int v=1;v<=n;v++){
		if(v==u)continue;
		if(!(mp[u][v]&&mp[v][u])){
			if(lv[v]!=-1){
				if((lv[v]&1)==(lv[u]&1)){
					return;
				}else{
					continue;
				}
			}
			lv[v]=!lv[u];
			dfs(v);
		}
	}
}

void init(){
	memset(mp,0,sizeof(mp));
	memset(lv,-1,sizeof(lv));
	memset(dp,0,sizeof(dp));
	memset(s0,0,sizeof(s0));
	memset(s1,0,sizeof(s1));
}

int main(){
	//int t;
	//cin>>t;
	while(cin>>n){

		init();

		//cin>>n;
		for(int i=1;i<=n;i++){
			int num;
			while(scanf("%d",&num)){
				if(!num)break;
				mp[i][num]=1;
			}
		}

		dp[0][0]=1;
		groupcnt=0;
		for(int i=1;i<=n;i++){
			if(lv[i]==-1){
				lv[i]=0;
				groupcnt++;
				tmp=0;
				dfs(i);
				//cout<<i<<" "<<tmp<<endl;

				for(int j=0;j<=n/2;j++){
					if(dp[groupcnt-1][j]){
						if(j+tmp>=0){
							dp[groupcnt][j+tmp]=1;
							pre[groupcnt][j+tmp]=j;
						}
						if(j-tmp>=0){
							dp[groupcnt][j-tmp]=1;
							pre[groupcnt][j-tmp]=j;
						}
					}
				}
			}
		}

		int ans=0;
		for(int i=0;i<=n;i++){
			if(dp[groupcnt][i]){
				ans=i;break;
			}
		}

		bool ok=1;
		for(int i=1;i<=n;i++){
			for(int j=1;j<=n;j++){
				if(i==j)continue;
				if( !(mp[i][j]&&mp[j][i]) ){
					if(lv[i]==lv[j]){
						ok=0;
					}
				}
			}
		}

		if(ok){
			//cout<<ans<<endl;
			int cur=ans;
			vector<int> ans0;
			vector<int> ans1;
			for(int i=groupcnt;i>=1;i--){
				int p=pre[i][cur];

				vector<int> &big=s0[i];
				vector<int> &small=s1[i];
				if(big.size()<small.size()){
					swap(big,small);
				}

				//cout<<"�󼯺�size"<<big.size()<<" С����size"<<small.size()<<endl;

				if(p<cur)swap(big,small);

				for(int j=0;j<big.size();j++){
					ans0.push_back(big[j]);
				}
				for(int j=0;j<small.size();j++){
					ans1.push_back(small[j]);
				}

				cur=p;
			}
			cout<<ans0.size()<<" ";
			for(int i=0;i<ans0.size();i++){
				cout<<ans0[i]<<" ";
			}cout<<endl;
			cout<<ans1.size()<<" ";
			for(int i=0;i<ans1.size();i++){
				cout<<ans1[i]<<" ";
			}cout<<endl;
		}else{
			cout<<"No solution"<<endl;
		}
		//if(t)cout<<endl;
	}
	return 0;
}*/


#include <bits/stdc++.h>
using namespace std;

#define ll long long
const int INF = 1e9;

ll gcd(ll a,ll b){
	return b==0?a:gcd(b,a%b);
}

ll MIN(ll a,ll b){
	if(a>b)return b;
	return a;
}

set<ll> s1;
set<ll> s2;

int main(){

	ll m,h1,a1,x1,y1;
	ll   h2,a2,x2,y2;
	cin>>m;
	cin>>h1>>a1;
	cin>>x1>>y1;
	cin>>h2>>a2;
	cin>>x2>>y2;

	//
	ll ans1=1;
	for(;ans1<=1e7;ans1++){
		h1=x1*h1+y1;
		h1%=m;
		h2=x2*h2+y2;
		h2%=m;
		if(h1==a1&&h2==a2){
			cout<<ans1;
			return 0;
			//s1.insert(ans1);
		}
	}
	cout<<-1;
	/*ll ans2=1;
	for(;ans2<=m+10;ans2++){
		h2=x2*h2+y2;
		h2%=m;
		if(h2==a2){
			s2.insert(ans2);
		}
	}

	bool ok=1;
	if(s1.empty()||s2.empty())ok=0;

	if(ok){
		ll ans=11000000000000LL;

		for(set<ll>::iterator it1=s1.begin();it1!=s1.end();it1++){
			if(*it1>ans)break;
			for(set<ll>::iterator it2=s2.begin();it2!=s2.end();it2++){
				if(*it2>ans)break;
				ll t1=*it1,	t2=*it2;
				ans=MIN(ans, t1*t2/gcd(t1,t2) );
			}
		}

		//ans1*ans2/gcd(ans1,ans2);
		cout<<ans;

	}else{
		cout<<"-1";
	}
	*/
	return 0;
}

/*
#define ll long long
const int INF=1e9;

int mp[110][110];
int lv[110];
bool dp[110][110];
int n;
vector<int> s0[110];
vector<int> s1[110];
int groupcnt;
int pre[110][110];

int re;
int tmp;
void dfs(int u){
//	cout<<"dfsing "<<u<<endl;
	if(lv[u]){
		s0[groupcnt].push_back(u);
		tmp--;
	}else{
		s1[groupcnt].push_back(u);
		tmp++;
	}
	for(int v=1;v<=n;v++){
		if(v==u)continue;
		if(!(mp[u][v]&&mp[v][u])){
			if(lv[v]!=-1){
				if((lv[v]&1)==(lv[u]&1)){
					return;
				}else{
					continue;
				}
			}
			lv[v]=!lv[u];
			dfs(v);
		}
	}
}

void init(){
	memset(mp,0,sizeof(mp));
	memset(lv,-1,sizeof(lv));
	memset(dp,0,sizeof(dp));
	memset(s0,0,sizeof(s0));
	memset(s1,0,sizeof(s1));
}

int main(){
	//int t;
	//cin>>t;
	while(cin>>n){

		init();

		//cin>>n;
		for(int i=1;i<=n;i++){
			int num;
			while(scanf("%d",&num)){
				if(!num)break;
				mp[i][num]=1;
			}
		}

		dp[0][0]=1;
		groupcnt=0;
		for(int i=1;i<=n;i++){
			if(lv[i]==-1){
				lv[i]=0;
				groupcnt++;
				tmp=0;
				dfs(i);
				//cout<<i<<" "<<tmp<<endl;

				for(int j=0;j<=n/2;j++){
					if(dp[groupcnt-1][j]){
						if(j+tmp>=0){
							dp[groupcnt][j+tmp]=1;
							pre[groupcnt][j+tmp]=j;
						}
						if(j-tmp>=0){
							dp[groupcnt][j-tmp]=1;
							pre[groupcnt][j-tmp]=j;
						}
					}
				}
			}
		}

		int ans=0;
		for(int i=0;i<=n;i++){
			if(dp[groupcnt][i]){
				ans=i;break;
			}
		}

		bool ok=1;
		for(int i=1;i<=n;i++){
			for(int j=1;j<=n;j++){
				if(i==j)continue;
				if( !(mp[i][j]&&mp[j][i]) ){
					if(lv[i]==lv[j]){
						ok=0;
					}
				}
			}
		}

		if(ok){
			//cout<<ans<<endl;
			int cur=ans;
			vector<int> ans0;
			vector<int> ans1;
			for(int i=groupcnt;i>=1;i--){
				int p=pre[i][cur];

				vector<int> &big=s0[i];
				vector<int> &small=s1[i];
				if(big.size()<small.size()){
					swap(big,small);
				}

				//cout<<"�󼯺�size"<<big.size()<<" С����size"<<small.size()<<endl;

				if(p<cur)swap(big,small);

				for(int j=0;j<big.size();j++){
					ans0.push_back(big[j]);
				}
				for(int j=0;j<small.size();j++){
					ans1.push_back(small[j]);
				}

				cur=p;
			}
			cout<<ans0.size()<<" ";
			for(int i=0;i<ans0.size();i++){
				cout<<ans0[i]<<" ";
			}cout<<endl;
			cout<<ans1.size()<<" ";
			for(int i=0;i<ans1.size();i++){
				cout<<ans1[i]<<" ";
			}cout<<endl;
		}else{
			cout<<"No solution"<<endl;
		}
		//if(t)cout<<endl;
	}
	return 0;
}*/


#include <bits/stdc++.h>
using namespace std;

#define ll long long
const int INF = 1e9;

ll gcd(ll a,ll b){
	return b==0?a:gcd(b,a%b);
}

int main(){

	ll m,h1,a1,x1,y1;
	ll   h2,a2,x2,y2;
	cin>>m;
	cin>>h1>>a1;
	cin>>x1>>y1;
	cin>>h2>>a2;
	cin>>x2>>y2;

	//
	ll ans1=1;
	for(;ans1<=m*2;ans1++){
		h1=x1*h1+y1;
		h1%=m;
		if(h1==a1)break;
	}

	ll ans2=1;
	for(;ans2<=m*2;ans2++){
		h2=x2*h2+y2;
		h2%=m;
		if(h2==a2)break;
	}

	bool ok=1;
	if(ans1>m*2||ans2>m*2)ok=0;

	if(ok){
		ll ans=ans1*ans2/gcd(ans1,ans2);
		cout<<ans;
	}else{
		cout<<"-1";
	}

	return 0;
}

/*
#define ll long long
const int INF=1e9;

int mp[110][110];
int lv[110];
bool dp[110][110];
int n;
vector<int> s0[110];
vector<int> s1[110];
int groupcnt;
int pre[110][110];

int re;
int tmp;
void dfs(int u){
//	cout<<"dfsing "<<u<<endl;
	if(lv[u]){
		s0[groupcnt].push_back(u);
		tmp--;
	}else{
		s1[groupcnt].push_back(u);
		tmp++;
	}
	for(int v=1;v<=n;v++){
		if(v==u)continue;
		if(!(mp[u][v]&&mp[v][u])){
			if(lv[v]!=-1){
				if((lv[v]&1)==(lv[u]&1)){
					return;
				}else{
					continue;
				}
			}
			lv[v]=!lv[u];
			dfs(v);
		}
	}
}

void init(){
	memset(mp,0,sizeof(mp));
	memset(lv,-1,sizeof(lv));
	memset(dp,0,sizeof(dp));
	memset(s0,0,sizeof(s0));
	memset(s1,0,sizeof(s1));
}

int main(){
	//int t;
	//cin>>t;
	while(cin>>n){

		init();

		//cin>>n;
		for(int i=1;i<=n;i++){
			int num;
			while(scanf("%d",&num)){
				if(!num)break;
				mp[i][num]=1;
			}
		}

		dp[0][0]=1;
		groupcnt=0;
		for(int i=1;i<=n;i++){
			if(lv[i]==-1){
				lv[i]=0;
				groupcnt++;
				tmp=0;
				dfs(i);
				//cout<<i<<" "<<tmp<<endl;

				for(int j=0;j<=n/2;j++){
					if(dp[groupcnt-1][j]){
						if(j+tmp>=0){
							dp[groupcnt][j+tmp]=1;
							pre[groupcnt][j+tmp]=j;
						}
						if(j-tmp>=0){
							dp[groupcnt][j-tmp]=1;
							pre[groupcnt][j-tmp]=j;
						}
					}
				}
			}
		}

		int ans=0;
		for(int i=0;i<=n;i++){
			if(dp[groupcnt][i]){
				ans=i;break;
			}
		}

		bool ok=1;
		for(int i=1;i<=n;i++){
			for(int j=1;j<=n;j++){
				if(i==j)continue;
				if( !(mp[i][j]&&mp[j][i]) ){
					if(lv[i]==lv[j]){
						ok=0;
					}
				}
			}
		}

		if(ok){
			//cout<<ans<<endl;
			int cur=ans;
			vector<int> ans0;
			vector<int> ans1;
			for(int i=groupcnt;i>=1;i--){
				int p=pre[i][cur];

				vector<int> &big=s0[i];
				vector<int> &small=s1[i];
				if(big.size()<small.size()){
					swap(big,small);
				}

				//cout<<"�󼯺�size"<<big.size()<<" С����size"<<small.size()<<endl;

				if(p<cur)swap(big,small);

				for(int j=0;j<big.size();j++){
					ans0.push_back(big[j]);
				}
				for(int j=0;j<small.size();j++){
					ans1.push_back(small[j]);
				}

				cur=p;
			}
			cout<<ans0.size()<<" ";
			for(int i=0;i<ans0.size();i++){
				cout<<ans0[i]<<" ";
			}cout<<endl;
			cout<<ans1.size()<<" ";
			for(int i=0;i<ans1.size();i++){
				cout<<ans1[i]<<" ";
			}cout<<endl;
		}else{
			cout<<"No solution"<<endl;
		}
		//if(t)cout<<endl;
	}
	return 0;
}*/


#include <bits/stdc++.h>
using namespace std;

#define ll long long
const int INF = 1e9;

ll gcd(ll a,ll b){
	return b==0?a:gcd(b,a%b);
}

set<ll> s1;
set<ll> s2;

int main(){

	ll m,h1,a1,x1,y1;
	ll   h2,a2,x2,y2;
	cin>>m;
	cin>>h1>>a1;
	cin>>x1>>y1;
	cin>>h2>>a2;
	cin>>x2>>y2;

	//
	ll ans1=1;
	for(;ans1<=m+1;ans1++){
		h1=x1*h1+y1;
		h1%=m;
		if(h1==a1){
			s1.insert(ans1);
		}
	}

	ll ans2=1;
	for(;ans2<=m+1;ans2++){
		h2=x2*h2+y2;
		h2%=m;
		if(h2==a2){
			s2.insert(ans2);
		}
	}

	bool ok=1;
	if(s1.empty()||s2.empty())ok=0;

	if(ok){
		ll ans=1100000000000LL;

		for(set<ll>::iterator it1=s1.begin();it1!=s1.end();it1++){
			if(*it1>ans)break;
			for(set<ll>::iterator it2=s2.begin();it2!=s2.end();it2++){
				if(*it2>ans)break;
				ll t1=*it1,	t2=*it2;
				ans=min(ans, t1*t2/gcd(t1,t2) );
			}
		}

		//ans1*ans2/gcd(ans1,ans2);
		cout<<ans;

	}else{
		cout<<"-1";
	}

	return 0;
}

/*
#define ll long long
const int INF=1e9;

int mp[110][110];
int lv[110];
bool dp[110][110];
int n;
vector<int> s0[110];
vector<int> s1[110];
int groupcnt;
int pre[110][110];

int re;
int tmp;
void dfs(int u){
//	cout<<"dfsing "<<u<<endl;
	if(lv[u]){
		s0[groupcnt].push_back(u);
		tmp--;
	}else{
		s1[groupcnt].push_back(u);
		tmp++;
	}
	for(int v=1;v<=n;v++){
		if(v==u)continue;
		if(!(mp[u][v]&&mp[v][u])){
			if(lv[v]!=-1){
				if((lv[v]&1)==(lv[u]&1)){
					return;
				}else{
					continue;
				}
			}
			lv[v]=!lv[u];
			dfs(v);
		}
	}
}

void init(){
	memset(mp,0,sizeof(mp));
	memset(lv,-1,sizeof(lv));
	memset(dp,0,sizeof(dp));
	memset(s0,0,sizeof(s0));
	memset(s1,0,sizeof(s1));
}

int main(){
	//int t;
	//cin>>t;
	while(cin>>n){

		init();

		//cin>>n;
		for(int i=1;i<=n;i++){
			int num;
			while(scanf("%d",&num)){
				if(!num)break;
				mp[i][num]=1;
			}
		}

		dp[0][0]=1;
		groupcnt=0;
		for(int i=1;i<=n;i++){
			if(lv[i]==-1){
				lv[i]=0;
				groupcnt++;
				tmp=0;
				dfs(i);
				//cout<<i<<" "<<tmp<<endl;

				for(int j=0;j<=n/2;j++){
					if(dp[groupcnt-1][j]){
						if(j+tmp>=0){
							dp[groupcnt][j+tmp]=1;
							pre[groupcnt][j+tmp]=j;
						}
						if(j-tmp>=0){
							dp[groupcnt][j-tmp]=1;
							pre[groupcnt][j-tmp]=j;
						}
					}
				}
			}
		}

		int ans=0;
		for(int i=0;i<=n;i++){
			if(dp[groupcnt][i]){
				ans=i;break;
			}
		}

		bool ok=1;
		for(int i=1;i<=n;i++){
			for(int j=1;j<=n;j++){
				if(i==j)continue;
				if( !(mp[i][j]&&mp[j][i]) ){
					if(lv[i]==lv[j]){
						ok=0;
					}
				}
			}
		}

		if(ok){
			//cout<<ans<<endl;
			int cur=ans;
			vector<int> ans0;
			vector<int> ans1;
			for(int i=groupcnt;i>=1;i--){
				int p=pre[i][cur];

				vector<int> &big=s0[i];
				vector<int> &small=s1[i];
				if(big.size()<small.size()){
					swap(big,small);
				}

				//cout<<"�󼯺�size"<<big.size()<<" С����size"<<small.size()<<endl;

				if(p<cur)swap(big,small);

				for(int j=0;j<big.size();j++){
					ans0.push_back(big[j]);
				}
				for(int j=0;j<small.size();j++){
					ans1.push_back(small[j]);
				}

				cur=p;
			}
			cout<<ans0.size()<<" ";
			for(int i=0;i<ans0.size();i++){
				cout<<ans0[i]<<" ";
			}cout<<endl;
			cout<<ans1.size()<<" ";
			for(int i=0;i<ans1.size();i++){
				cout<<ans1[i]<<" ";
			}cout<<endl;
		}else{
			cout<<"No solution"<<endl;
		}
		//if(t)cout<<endl;
	}
	return 0;
}*/


#include <bits/stdc++.h>
using namespace std;

#define ll long long
const int INF = 1e9;

ll gcd(ll a,ll b){
	if(b==0)return a;
	return gcd(b,a%b);
}

ll MIN(ll a,ll b){
	if(a>b)return b;
	return a;
}

set<ll> s1;
set<ll> s2;

int main(){

	ll m,h1,a1,x1,y1;
	ll   h2,a2,x2,y2;
	cin>>m;
	cin>>h1>>a1;
	cin>>x1>>y1;
	cin>>h2>>a2;
	cin>>x2>>y2;

	//
	ll ans1=1;
	for(;ans1<=m+1;ans1++){
		h1=x1*h1+y1;
		h1%=m;
		if(h1==a1){
			break;
		}
	}

	ll ans2=1;
	for(;ans2<=m+1;ans2++){
		h2=x2*h2+y2;
		h2%=m;
		if(h2==a2){
			break;
		}
	}

	ll cyc1=1;
	for(;cyc1<=m+1;cyc1++){
		h1=x1*h1+y1;
		h1%=m;
		if(h1==a1){
			break;
		}
	}

	ll cyc2=1;
	for(;cyc2<=m+1;cyc2++){
		h2=x2*h2+y2;
		h2%=m;
		if(h2==a2){
			break;
		}
	}

	bool ok=1;
	if(ans1>m+1||ans2>m+1)ok=0;

	if(ok){
		int end1=1000;
		int end2=1000;
		if(cyc1>m+1)end1=0;
		if(cyc2>m+1)end2=0;

		ll ans=10000000000000LL;
		for(int i=0;i<=end1;i++){
			for(int j=0;j<=end2;j++){
				ll tmp1=ans1+i*cyc1;
				ll tmp2=ans2+j*cyc2;
				ans=MIN(ans, tmp1*tmp2/gcd(tmp1,tmp2) );
			}
		}
		cout<<ans;
	}else{
		cout<<"-1";
	}

	return 0;
}

/*
#define ll long long
const int INF=1e9;

int mp[110][110];
int lv[110];
bool dp[110][110];
int n;
vector<int> s0[110];
vector<int> s1[110];
int groupcnt;
int pre[110][110];

int re;
int tmp;
void dfs(int u){
//	cout<<"dfsing "<<u<<endl;
	if(lv[u]){
		s0[groupcnt].push_back(u);
		tmp--;
	}else{
		s1[groupcnt].push_back(u);
		tmp++;
	}
	for(int v=1;v<=n;v++){
		if(v==u)continue;
		if(!(mp[u][v]&&mp[v][u])){
			if(lv[v]!=-1){
				if((lv[v]&1)==(lv[u]&1)){
					return;
				}else{
					continue;
				}
			}
			lv[v]=!lv[u];
			dfs(v);
		}
	}
}

void init(){
	memset(mp,0,sizeof(mp));
	memset(lv,-1,sizeof(lv));
	memset(dp,0,sizeof(dp));
	memset(s0,0,sizeof(s0));
	memset(s1,0,sizeof(s1));
}

int main(){
	//int t;
	//cin>>t;
	while(cin>>n){

		init();

		//cin>>n;
		for(int i=1;i<=n;i++){
			int num;
			while(scanf("%d",&num)){
				if(!num)break;
				mp[i][num]=1;
			}
		}

		dp[0][0]=1;
		groupcnt=0;
		for(int i=1;i<=n;i++){
			if(lv[i]==-1){
				lv[i]=0;
				groupcnt++;
				tmp=0;
				dfs(i);
				//cout<<i<<" "<<tmp<<endl;

				for(int j=0;j<=n/2;j++){
					if(dp[groupcnt-1][j]){
						if(j+tmp>=0){
							dp[groupcnt][j+tmp]=1;
							pre[groupcnt][j+tmp]=j;
						}
						if(j-tmp>=0){
							dp[groupcnt][j-tmp]=1;
							pre[groupcnt][j-tmp]=j;
						}
					}
				}
			}
		}

		int ans=0;
		for(int i=0;i<=n;i++){
			if(dp[groupcnt][i]){
				ans=i;break;
			}
		}

		bool ok=1;
		for(int i=1;i<=n;i++){
			for(int j=1;j<=n;j++){
				if(i==j)continue;
				if( !(mp[i][j]&&mp[j][i]) ){
					if(lv[i]==lv[j]){
						ok=0;
					}
				}
			}
		}

		if(ok){
			//cout<<ans<<endl;
			int cur=ans;
			vector<int> ans0;
			vector<int> ans1;
			for(int i=groupcnt;i>=1;i--){
				int p=pre[i][cur];

				vector<int> &big=s0[i];
				vector<int> &small=s1[i];
				if(big.size()<small.size()){
					swap(big,small);
				}

				//cout<<"????size"<<big.size()<<" ��????size"<<small.size()<<endl;

				if(p<cur)swap(big,small);

				for(int j=0;j<big.size();j++){
					ans0.push_back(big[j]);
				}
				for(int j=0;j<small.size();j++){
					ans1.push_back(small[j]);
				}

				cur=p;
			}
			cout<<ans0.size()<<" ";
			for(int i=0;i<ans0.size();i++){
				cout<<ans0[i]<<" ";
			}cout<<endl;
			cout<<ans1.size()<<" ";
			for(int i=0;i<ans1.size();i++){
				cout<<ans1[i]<<" ";
			}cout<<endl;
		}else{
			cout<<"No solution"<<endl;
		}
		//if(t)cout<<endl;
	}
	return 0;
}*/


#include <bits/stdc++.h>
using namespace std;

#define ll long long
const int INF = 1e9;

ll gcd(ll a,ll b){
	if(b==0)return a;
	return gcd(b,a%b);
}

ll MIN(ll a,ll b){
	if(a>b)return b;
	return a;
}

int main(){
	ll H1,H2;
	ll m,h1,a1,x1,y1;
	ll   h2,a2,x2,y2;
	cin>>m;
	cin>>h1>>a1;
	cin>>x1>>y1;
	cin>>h2>>a2;
	cin>>x2>>y2;
	H1=h1;	H2=h2;
	//
	ll ans1=1;
	for(;ans1<=m+1;ans1++){
		h1=x1*h1+y1;
		h1%=m;
		if(h1==a1){
			break;
		}
	}

	ll ans2=1;
	for(;ans2<=m+1;ans2++){
		h2=x2*h2+y2;
		h2%=m;
		if(h2==a2){
			break;
		}
	}

	ll cyc1=1;
	for(;cyc1<=m+1;cyc1++){
		h1=x1*h1+y1;
		h1%=m;
		if(h1==a1){
			break;
		}
	}

	ll cyc2=1;
	for(;cyc2<=m+1;cyc2++){
		h2=x2*h2+y2;
		h2%=m;
		if(h2==a2){
			break;
		}
	}

	bool ok=1;
	if(ans1>m+1||ans2>m+1)ok=0;

	if(ok){
		if(cyc1>m+1)cyc1=0;
		if(cyc2>m+1)cyc2=0;

	/*	cout<<"ans1 = "<<ans1<<endl;
		cout<<"ans2 = "<<ans2<<endl;
		cout<<"cyc1 = "<<cyc1<<endl;
		cout<<"cyc2 = "<<cyc2<<endl;
	*/
		set<ll> s;
		for(int i=0;i<=m;i++){
			s.insert(ans1+cyc1*i);
		}
		for(int i=0;i<=m;i++){
			if(s.count(ans2+cyc2*i)){
				cout<<ans2+cyc2*i<<endl;
				return 0;
			}
		}

		cout<<-1;
	}else{
		cout<<-1;
	}

	return 0;
}

/*
#define ll long long
const int INF=1e9;

int mp[110][110];
int lv[110];
bool dp[110][110];
int n;
vector<int> s0[110];
vector<int> s1[110];
int groupcnt;
int pre[110][110];

int re;
int tmp;
void dfs(int u){
//	cout<<"dfsing "<<u<<endl;
	if(lv[u]){
		s0[groupcnt].push_back(u);
		tmp--;
	}else{
		s1[groupcnt].push_back(u);
		tmp++;
	}
	for(int v=1;v<=n;v++){
		if(v==u)continue;
		if(!(mp[u][v]&&mp[v][u])){
			if(lv[v]!=-1){
				if((lv[v]&1)==(lv[u]&1)){
					return;
				}else{
					continue;
				}
			}
			lv[v]=!lv[u];
			dfs(v);
		}
	}
}

void init(){
	memset(mp,0,sizeof(mp));
	memset(lv,-1,sizeof(lv));
	memset(dp,0,sizeof(dp));
	memset(s0,0,sizeof(s0));
	memset(s1,0,sizeof(s1));
}

int main(){
	//int t;
	//cin>>t;
	while(cin>>n){

		init();

		//cin>>n;
		for(int i=1;i<=n;i++){
			int num;
			while(scanf("%d",&num)){
				if(!num)break;
				mp[i][num]=1;
			}
		}

		dp[0][0]=1;
		groupcnt=0;
		for(int i=1;i<=n;i++){
			if(lv[i]==-1){
				lv[i]=0;
				groupcnt++;
				tmp=0;
				dfs(i);
				//cout<<i<<" "<<tmp<<endl;

				for(int j=0;j<=n/2;j++){
					if(dp[groupcnt-1][j]){
						if(j+tmp>=0){
							dp[groupcnt][j+tmp]=1;
							pre[groupcnt][j+tmp]=j;
						}
						if(j-tmp>=0){
							dp[groupcnt][j-tmp]=1;
							pre[groupcnt][j-tmp]=j;
						}
					}
				}
			}
		}

		int ans=0;
		for(int i=0;i<=n;i++){
			if(dp[groupcnt][i]){
				ans=i;break;
			}
		}

		bool ok=1;
		for(int i=1;i<=n;i++){
			for(int j=1;j<=n;j++){
				if(i==j)continue;
				if( !(mp[i][j]&&mp[j][i]) ){
					if(lv[i]==lv[j]){
						ok=0;
					}
				}
			}
		}

		if(ok){
			//cout<<ans<<endl;
			int cur=ans;
			vector<int> ans0;
			vector<int> ans1;
			for(int i=groupcnt;i>=1;i--){
				int p=pre[i][cur];

				vector<int> &big=s0[i];
				vector<int> &small=s1[i];
				if(big.size()<small.size()){
					swap(big,small);
				}

				//cout<<"????size"<<big.size()<<" ��????size"<<small.size()<<endl;

				if(p<cur)swap(big,small);

				for(int j=0;j<big.size();j++){
					ans0.push_back(big[j]);
				}
				for(int j=0;j<small.size();j++){
					ans1.push_back(small[j]);
				}

				cur=p;
			}
			cout<<ans0.size()<<" ";
			for(int i=0;i<ans0.size();i++){
				cout<<ans0[i]<<" ";
			}cout<<endl;
			cout<<ans1.size()<<" ";
			for(int i=0;i<ans1.size();i++){
				cout<<ans1[i]<<" ";
			}cout<<endl;
		}else{
			cout<<"No solution"<<endl;
		}
		//if(t)cout<<endl;
	}
	return 0;
}*/


#include <bits/stdc++.h>
using namespace std;

#define ll long long
const int INF = 1e9;

ll gcd(ll a,ll b){
	if(b==0)return a;
	return gcd(b,a%b);
}

ll MIN(ll a,ll b){
	if(a>b)return b;
	return a;
}

set<ll> s1;
set<ll> s2;

int main(){

	ll m,h1,a1,x1,y1;
	ll   h2,a2,x2,y2;
	cin>>m;
	cin>>h1>>a1;
	cin>>x1>>y1;
	cin>>h2>>a2;
	cin>>x2>>y2;

	//
	ll ans1=1;
	for(;ans1<=m+10;ans1++){
		h1=x1*h1+y1;
		h1%=m;
		if(h1==a1){
			s1.insert(ans1);
		}
	}

	ll ans2=1;
	for(;ans2<=m+10;ans2++){
		h2=x2*h2+y2;
		h2%=m;
		if(h2==a2){
			s2.insert(ans2);
		}
	}

	bool ok=1;
	if(s1.empty()||s2.empty())ok=0;

	if(ok){
		ll ans=110000000000000LL;

		for(set<ll>::iterator it1=s1.begin();it1!=s1.end();it1++){
			if(*it1>ans)break;
			for(set<ll>::iterator it2=s2.begin();it2!=s2.end();it2++){
				if(*it2>ans)break;
				ll t1=*it1,	t2=*it2;
				ans=MIN(ans, t1*t2/gcd(t1,t2) );
			}
		}

		//ans1*ans2/gcd(ans1,ans2);
		cout<<ans;

	}else{
		cout<<"-1";
	}

	return 0;
}

/*
#define ll long long
const int INF=1e9;

int mp[110][110];
int lv[110];
bool dp[110][110];
int n;
vector<int> s0[110];
vector<int> s1[110];
int groupcnt;
int pre[110][110];

int re;
int tmp;
void dfs(int u){
//	cout<<"dfsing "<<u<<endl;
	if(lv[u]){
		s0[groupcnt].push_back(u);
		tmp--;
	}else{
		s1[groupcnt].push_back(u);
		tmp++;
	}
	for(int v=1;v<=n;v++){
		if(v==u)continue;
		if(!(mp[u][v]&&mp[v][u])){
			if(lv[v]!=-1){
				if((lv[v]&1)==(lv[u]&1)){
					return;
				}else{
					continue;
				}
			}
			lv[v]=!lv[u];
			dfs(v);
		}
	}
}

void init(){
	memset(mp,0,sizeof(mp));
	memset(lv,-1,sizeof(lv));
	memset(dp,0,sizeof(dp));
	memset(s0,0,sizeof(s0));
	memset(s1,0,sizeof(s1));
}

int main(){
	//int t;
	//cin>>t;
	while(cin>>n){

		init();

		//cin>>n;
		for(int i=1;i<=n;i++){
			int num;
			while(scanf("%d",&num)){
				if(!num)break;
				mp[i][num]=1;
			}
		}

		dp[0][0]=1;
		groupcnt=0;
		for(int i=1;i<=n;i++){
			if(lv[i]==-1){
				lv[i]=0;
				groupcnt++;
				tmp=0;
				dfs(i);
				//cout<<i<<" "<<tmp<<endl;

				for(int j=0;j<=n/2;j++){
					if(dp[groupcnt-1][j]){
						if(j+tmp>=0){
							dp[groupcnt][j+tmp]=1;
							pre[groupcnt][j+tmp]=j;
						}
						if(j-tmp>=0){
							dp[groupcnt][j-tmp]=1;
							pre[groupcnt][j-tmp]=j;
						}
					}
				}
			}
		}

		int ans=0;
		for(int i=0;i<=n;i++){
			if(dp[groupcnt][i]){
				ans=i;break;
			}
		}

		bool ok=1;
		for(int i=1;i<=n;i++){
			for(int j=1;j<=n;j++){
				if(i==j)continue;
				if( !(mp[i][j]&&mp[j][i]) ){
					if(lv[i]==lv[j]){
						ok=0;
					}
				}
			}
		}

		if(ok){
			//cout<<ans<<endl;
			int cur=ans;
			vector<int> ans0;
			vector<int> ans1;
			for(int i=groupcnt;i>=1;i--){
				int p=pre[i][cur];

				vector<int> &big=s0[i];
				vector<int> &small=s1[i];
				if(big.size()<small.size()){
					swap(big,small);
				}

				//cout<<"????size"<<big.size()<<" ��????size"<<small.size()<<endl;

				if(p<cur)swap(big,small);

				for(int j=0;j<big.size();j++){
					ans0.push_back(big[j]);
				}
				for(int j=0;j<small.size();j++){
					ans1.push_back(small[j]);
				}

				cur=p;
			}
			cout<<ans0.size()<<" ";
			for(int i=0;i<ans0.size();i++){
				cout<<ans0[i]<<" ";
			}cout<<endl;
			cout<<ans1.size()<<" ";
			for(int i=0;i<ans1.size();i++){
				cout<<ans1[i]<<" ";
			}cout<<endl;
		}else{
			cout<<"No solution"<<endl;
		}
		//if(t)cout<<endl;
	}
	return 0;
}*/


#include <bits/stdc++.h>
using namespace std;

#define ll long long
const int INF = 1e9;

ll gcd(ll a,ll b){
	if(b==0)return a;
	return gcd(b,a%b);
}

ll MIN(ll a,ll b){
	if(a>b)return b;
	return a;
}

bool flag1[1000010];
bool flag2[1000010];

int main(){
	ll H1,H2;
	ll m,h1,a1,x1,y1;
	ll   h2,a2,x2,y2;
	cin>>m;
	cin>>h1>>a1;
	cin>>x1>>y1;
	cin>>h2>>a2;
	cin>>x2>>y2;
	H1=h1;	H2=h2;
	//
	ll ans1=1;
	for(;ans1<=m+1;ans1++){
		h1=x1*h1+y1;
		h1%=m;
		if(h1==a1){
			break;
		}
	}

	ll ans2=1;
	for(;ans2<=m+1;ans2++){
		h2=x2*h2+y2;
		h2%=m;
		if(h2==a2){
			break;
		}
	}

	ll cyc1=1;
	for(;cyc1<=m+1;cyc1++){
		h1=x1*h1+y1;
		h1%=m;
		if(h1==a1){
			break;
		}
	}

	ll cyc2=1;
	for(;cyc2<=m+1;cyc2++){
		h2=x2*h2+y2;
		h2%=m;
		if(h2==a2){
			break;
		}
	}

	bool ok=1;
	if(ans1>m+1||ans2>m+1)ok=0;

	if(ok){
		if(cyc1>m+1)cyc1=0;
		if(cyc2>m+1)cyc2=0;

	/*	cout<<"ans1 = "<<ans1<<endl;
		cout<<"ans2 = "<<ans2<<endl;
		cout<<"cyc1 = "<<cyc1<<endl;
		cout<<"cyc2 = "<<cyc2<<endl;
	*/

		for(int i=0;i<=m;i++){
			flag1[ans1+cyc1*i]=1;
		}
		for(int i=0;i<=m;i++){
			flag2[ans2+cyc2*i]=1;
		}

		for(int i=0;i<=1000000;i++){
			if(flag1[i]&&flag2[i]){
				cout<<i;
				return 0;
			}
		}

		cout<<-1;
	}else{
		cout<<-1;
	}

	return 0;
}

/*
#define ll long long
const int INF=1e9;

int mp[110][110];
int lv[110];
bool dp[110][110];
int n;
vector<int> s0[110];
vector<int> s1[110];
int groupcnt;
int pre[110][110];

int re;
int tmp;
void dfs(int u){
//	cout<<"dfsing "<<u<<endl;
	if(lv[u]){
		s0[groupcnt].push_back(u);
		tmp--;
	}else{
		s1[groupcnt].push_back(u);
		tmp++;
	}
	for(int v=1;v<=n;v++){
		if(v==u)continue;
		if(!(mp[u][v]&&mp[v][u])){
			if(lv[v]!=-1){
				if((lv[v]&1)==(lv[u]&1)){
					return;
				}else{
					continue;
				}
			}
			lv[v]=!lv[u];
			dfs(v);
		}
	}
}

void init(){
	memset(mp,0,sizeof(mp));
	memset(lv,-1,sizeof(lv));
	memset(dp,0,sizeof(dp));
	memset(s0,0,sizeof(s0));
	memset(s1,0,sizeof(s1));
}

int main(){
	//int t;
	//cin>>t;
	while(cin>>n){

		init();

		//cin>>n;
		for(int i=1;i<=n;i++){
			int num;
			while(scanf("%d",&num)){
				if(!num)break;
				mp[i][num]=1;
			}
		}

		dp[0][0]=1;
		groupcnt=0;
		for(int i=1;i<=n;i++){
			if(lv[i]==-1){
				lv[i]=0;
				groupcnt++;
				tmp=0;
				dfs(i);
				//cout<<i<<" "<<tmp<<endl;

				for(int j=0;j<=n/2;j++){
					if(dp[groupcnt-1][j]){
						if(j+tmp>=0){
							dp[groupcnt][j+tmp]=1;
							pre[groupcnt][j+tmp]=j;
						}
						if(j-tmp>=0){
							dp[groupcnt][j-tmp]=1;
							pre[groupcnt][j-tmp]=j;
						}
					}
				}
			}
		}

		int ans=0;
		for(int i=0;i<=n;i++){
			if(dp[groupcnt][i]){
				ans=i;break;
			}
		}

		bool ok=1;
		for(int i=1;i<=n;i++){
			for(int j=1;j<=n;j++){
				if(i==j)continue;
				if( !(mp[i][j]&&mp[j][i]) ){
					if(lv[i]==lv[j]){
						ok=0;
					}
				}
			}
		}

		if(ok){
			//cout<<ans<<endl;
			int cur=ans;
			vector<int> ans0;
			vector<int> ans1;
			for(int i=groupcnt;i>=1;i--){
				int p=pre[i][cur];

				vector<int> &big=s0[i];
				vector<int> &small=s1[i];
				if(big.size()<small.size()){
					swap(big,small);
				}

				//cout<<"????size"<<big.size()<<" ��????size"<<small.size()<<endl;

				if(p<cur)swap(big,small);

				for(int j=0;j<big.size();j++){
					ans0.push_back(big[j]);
				}
				for(int j=0;j<small.size();j++){
					ans1.push_back(small[j]);
				}

				cur=p;
			}
			cout<<ans0.size()<<" ";
			for(int i=0;i<ans0.size();i++){
				cout<<ans0[i]<<" ";
			}cout<<endl;
			cout<<ans1.size()<<" ";
			for(int i=0;i<ans1.size();i++){
				cout<<ans1[i]<<" ";
			}cout<<endl;
		}else{
			cout<<"No solution"<<endl;
		}
		//if(t)cout<<endl;
	}
	return 0;
}*/