Mike has a frog and a flower. His frog is named Xaniar and his flower is named Abol. Initially(at time 0), height of Xaniar is h1 and height of Abol is h2. Each second, Mike waters Abol and Xaniar.  So, if height of Xaniar is h1 and height of Abol is h2, after one second height of Xaniar will become  and height of Abol will become  where x1, y1, x2 and y2 are some integer numbers and  denotes the remainder of a modulo b.Mike is a competitive programmer fan. He wants to know the minimum time it takes until height of Xania is a1 and height of Abol is a2.Mike has asked you for your help. Calculate the minimum time or say it will never happen.

The first line of input contains integer m (2 ≤ m ≤ 106). The second line of input contains integers h1 and a1 (0 ≤ h1, a1 < m). The third line of input contains integers x1 and y1 (0 ≤ x1, y1 < m). The fourth line of input contains integers h2 and a2 (0 ≤ h2, a2 < m). The fifth line of input contains integers x2 and y2 (0 ≤ x2, y2 < m). It is guaranteed that h1 ≠ a1 and h2 ≠ a2.

Print the minimum number of seconds until Xaniar reaches height a1 and Abol reaches height a2 or print -1 otherwise.

NoteIn the first sample, heights sequences are following:Xaniar: Abol:

#include<stdio.h>

long long a[1000006],b[1000006],ans,count1,count2,lc,count3,count4,count;

long long lcm(long long a,long long b)
{
	if(a>b)
	{
		if(a%b==0)
			return b;

		return lcm(b,a%b);
	}
	else
	{
		if(b%a==0)
			return a;

		return lcm(a,b%a);
	}
}

int main()
{
	long long m,h1,h2,x1,x2,y1,y2,a1,a2,p,i,n;

	scanf("%lld",&m);

	scanf("%lld%lld",&h1,&a1);

	scanf("%lld%lld",&x1,&y1);

	scanf("%lld%lld",&h2,&a2);

	scanf("%lld%lld",&x2,&y2);

	p=1;

	for(i=0;i<m+2;i++)
		a[i]=b[i]=0;

	count1=count2=0;

	for(i=0;i<m+2;i++)
	{
		if(h1==a1)
		{
			p=0;
			break;
		}

		if(!a[h1])
			a[h1]=1;
		else
		{
			break;
		}

		count1++;
		h1=(h1*x1+y1)%m;
	}

	if(p)
	{
		printf("-1");
		return 0;
	}

	p=1;

	for(i=0;i<m+2;i++)
	{
		if(h2==a2)
		{
			p=0;
			break;
		}

		if(!b[h2])
			b[h2]=1;
		else
		{
			break;
		}

		count2++;
		h2=(h2*x2+y2)%m;
	}

	if(p)
	{
		printf("-1");
		return 0;
	}

	if(count1==count2)
	{
		printf("%lld",count1);
		return 0;
	}

	for(i=0;i<m+2;i++)
		a[i]=b[i]=0;

	h2=(h2*x2+y2)%m;
	h1=(h1*x1+y1)%m;

	//printf("a");

	count3=count4=1;

	for(i=0;i<m+2;i++)
	{
		if(h1==a1)
		{
			break;
		}

		if(!a[h1])
			a[h1]=1;
		else
		{
			count3=-1;
			break;
		}

		count3++;
		h1=(h1*x1+y1)%m;
	}

	for(i=0;i<m+2;i++)
	{
		if(h2==a2)
		{
			break;
		}

		if(!b[h2])
			b[h2]=1;
		else
		{
			count4=-1;
			break;
		}

		count4++;
		h2=(h2*x2+y2)%m;
	}

	count=(count1>count2)?count1:count2;

//	printf("%lld %lld ",count3,count4);
//	printf("%lld %lld ",count1,count2);

	if(count3==-1 && count4==-1)
	{
		printf("-1");
		return 0;
	}
	else if(count3==-1)
	{
		if(count1<count2)
		{
			printf("-1");
			return 0;
		}

		if((count1-count2)%count4==0)
		{
			printf("%lld",count1);
			return 0;
		}
		else
		{
			printf("-1");
			return 0;
		}
	}
	else if(count4==-1)
	{
		if(count2<count1)
		{
			printf("-1");
			return 0;
		}

		if((count2-count1)%count3==0)
		{
			printf("%lld",count2);
			return 0;
		}
		else
		{
			printf("-1");
			return 0;
		}
	}

	i=0;

	while(1)
	{
		if(i>10000000)
		{
			printf("-1");
			return 0;
		}
		i++;
		if(((count-count1)%count3==0) && ((count-count2)%count4==0))
			break;
		count=count3*i+count1;
	}

	printf("%lld",count);

	return 0;
}

#include <stdio.h>

int main()
{
    long long m,i;

    scanf("%lld",&m);

    long long h1,a1;
    scanf("%lld %lld",&h1,&a1);

    long long x1,y1;
    scanf("%lld %lld",&x1,&y1);

    long long h2,a2;
    scanf("%lld %lld",&h2,&a2);

    long long x2,y2;
    scanf("%lld %lld",&x2,&y2);

    long long count=0;

    long long initial1 = h1;
    long long initial2 = h2;

        long long flag=0;
        long long temp=0;

        while (h1!=a1)
        {

            h1 = (((x1%m)*(h1%m))%m + y1%m)%m;
            h2 = (((x2%m)*(h2%m))%m + y2%m)%m;
            //printf("h1 is %lld\n",h1);
            //printf("h2 is %lld\n",h2);
            count = count+1;
            temp = temp+1;
            if (temp> (m+10))
            {
                flag=1;
                printf("-1\n");
                break;
            }

        }
    //printf("count is %lld\n",count);

        if (flag==0)
        {
            long long temp1=0;

            if (h2==a2)
            {
                printf("%lld\n",count);
                flag=1;

            }
            else
            {
                long long z1=1, z2=0;
                while (h1!=a1 || temp1==0)
                {
                    h1 = (((x1%m)*(h1%m))%m + y1%m)%m;
                    //h2 = (((x2%m)*(h2%m))%m + y2%m)%m;
                    z1 = ((z1%m) * (x2%m))%m;
                    z2 = (((z2%m)*(x2%m))%m + y2%m)%m;
                    temp1++;
                    //count++;
                    if (temp1>(m+10))
                    {
                        flag=1;
                        //prlong longf("here\n");
                        printf("-1\n");
                        break;
                    }
                }

                if (flag==0)
                {

                    long long temp2=0;

                    while (h2!=a2)
                    {
                        h2 = (((z1%m)*(h2%m))%m + z2%m)%m;
                        temp2++;
                        if (temp2 > (m+10))
                        {
                            flag=1;
                            //prlong longf("second\n");
                            printf("-1\n");
                            break;
                        }
                    }

                    if (flag==0)
                    {
                        printf("%lld\n",count+temp1*temp2);
                    }

                }

            }

        }

    return 0;
}

#include<stdio.h>
long long int p,h1,a1,x1,y,h2,a2,x2,y2;
long long int hone(long long int h)
{
	return (x1*h+y)%p;
}
long long int htwo(long long int h)
{
	return (x2*h+y2)%p;
}
int main()
{
	scanf("%lld %lld %lld %lld %lld %lld %lld %lld %lld",&p,&a1,&h1,&x1,&y,&a2,&h2,&x2,&y2);
	long long int i,hi=a1,c=0,d=1,h=hone(h1),o=0,a,b;
	while(hi!=h1 && c<=p)
	{
		hi=hone(hi);
		c++;
	}
	while(h!=h1 && d<=p)
	{
		h=hone(h);
		d++;
	}
	if(c>p)
		printf("-1\n");
	else
	{
		hi=a2;
		for(i=0;i<c;i++)
			hi=htwo(hi);
		if(hi==h2)
			printf("%lld\n",c);
		else if(d>p)
			printf("-1\n");
		else
		{
			a=1;b=0;
			for(i=0;i<d;i++)
			{
				a=(a*x2)%p;
				b=(b*x2+y2)%p;
			}
			while(hi!=h2 && o<=p)
			{
				hi=(a*hi+b)%p;
				o++;
			}
			if(o>p)
				printf("-1\n");
			else
				printf("%lld\n",c+o*d);
		}
	}
	return 0;
}

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void no_solution() {
    puts("-1");
    exit(0);
}

long long
count_step(unsigned long long h, unsigned long long a, unsigned long long x, unsigned long long y, unsigned int m) {
    if (h == a) return 0;

    char mark[m];
    memset(mark, 0, m);

    unsigned long long c = 0;
    while (h != a) {
        if (mark[h])
            return -1;
        mark[h] = 1;
        h = (x * h + y) % m;
        c++;
    }
    return c;
}

long long gcd(long long a, long long b) {
    long long t;
    while (b != 0) {
        t = b;
        b = a % b;
        a = t;
    }
    return a;
}

/**
 * v1 == 0
 */
void zero(long long u1, long long u2, long long v2) {
    if (v2 == 0 && u1 != u2 || (u1 < u2 || (u1 - u2) % v2 != 0))
        no_solution();
    printf("%I64d", u1);
    exit(0);
}

int main() {
    unsigned int m;
    unsigned long long h1, a1, x1, y1;
    unsigned long long h2, a2, x2, y2;

    scanf("%d %I64d %I64d %I64d %I64d %I64d %I64d %I64d %I64d", &m, &h1, &a1, &x1, &y1, &h2, &a2, &x2, &y2);

    long long u1, v1;
    u1 = count_step(h1, a1, x1, y1, m);
    if (u1 == -1)
        no_solution();
    v1 = count_step((a1 * x1 + y1) % m, a1, x1, y1, m) + 1;

    long long u2, v2;
    u2 = count_step(h2, a2, x2, y2, m);
    if (u2 == -1)
        no_solution();
    v2 = count_step((a2 * x2 + y2) % m, a2, x2, y2, m) + 1;

    // solve u1 + v1 * t1 = u2 + v2 * t2
    if (v1 == 0)
        zero(u1, u2, v2);

    if (v2 == 0)
        zero(u2, u1, v1);

    // t1 = (u2 - u1 + v2 * t2) / v1
    if (labs(u1 - u2) % gcd(v1, v2) != 0)
        no_solution();
    else {
        long long t2 = 0;
        long long s = u2 - u1 + v2 * t2;
        while (s < 0 || s % v1 != 0) {
            t2++;
            s = u2 - u1 + v2 * t2;
        }
        printf("%I64d", u2 + v2 * t2);
    }

    return 0;
}

#include <stdio.h>
#include <string.h>

#define INF 1000000000

int flag[1000000];

int main(void) {
    int tmp;
    long long m, h1, a1, x1, y1, h2, a2, x2, y2;
    long long cur, sum, l1, r1, l2, r2, pos1, pos2;

    while(~scanf("%lld %lld %lld %lld %lld %lld %lld %lld %lld", &m, &h1, &a1, &x1, &y1, &h2, &a2, &x2, &y2)){

    memset(flag, 0, sizeof flag);
    sum = 0;
    cur = h1;
    pos1 = INF;
    do {
        flag[cur] = 1;
        if (cur == a1)
            pos1 = sum;
        cur = (x1 * cur + y1) % m;
        sum++;
    } while (!flag[cur]);
    tmp = cur;
    l1 = 0;
    cur = h1;
    while (cur != tmp) {
        cur = (x1 * cur + y1) % m;
        l1++;
    }
    r1 = sum - l1;
    if (pos1 == INF) {
        puts("-1");
        goto contin;
    }

    memset(flag, 0, sizeof flag);
    sum = 0;
    cur = h2;
    pos2 = INF;
    do {
        flag[cur] = 1;
        if (cur == a2)
            pos2 = sum;
        cur = (x2 * cur + y2) % m;
        sum++;
    } while (!flag[cur]);
    tmp = cur;
    l2 = 0;
    cur = h2;
    while (cur != tmp) {
        cur = (x2 * cur + y2) % m;
        l2++;
    }
    r2 = sum - l2;
    if (pos2 == INF) {
        puts("-1");
        goto contin;
    }

    if (pos1 < l1 && pos2 < l2) {
        if (pos1 != pos2) {
            puts("-1");
            goto contin;
        }
        printf("%lld\n", pos1);
    } else if (pos1 < l1) {
        for (cur = pos2; cur <= pos1; cur += r2)
            if (cur == pos1) {
                printf("%lld\n", pos1);
                goto contin;
            }
        puts("-1");
    } else if (pos2 < l2) {
        for (cur = pos1; cur <= pos2; cur += r1)
            if (cur == pos2) {
                printf("%lld\n", pos2);
                goto contin;
            }
        puts("-1");
    } else
        if (pos1 > pos2) {
            for (cur = pos1; cur < pos1 + r1 * r2; cur += r1)
                if ((cur - l2) % r2 + l2 == pos2) {
                    printf("%lld\n", cur);
                    goto contin;
                }
            puts("-1");
        } else {
            for (cur = pos2; cur < pos2 + r2 * r1; cur += r2)
                if ((cur - l1) % r1 + l1 == pos1) {
                    printf("%lld\n", cur);
                    goto contin;
                }
            puts("-1");
        }
    contin:;
}
    return 0;
}

#include <stdio.h>
#include <string.h>

int main(int argc, char const *argv[])
{
    int i,m,h1,h2,a1,a2,x1,x2,y1,y2;
    __int64 cnt1,cnt2,tmp,tmp1,tmp2;
    while(scanf("%d",&m)!=EOF)
    {
        scanf("%d %d %d %d %d %d %d %d",&h1,&a1,&x1,&y1,&h2,&a2,&x2,&y2);

        for(i=0,tmp1=0,tmp=h1,cnt1=0;i<m;i++)
        {
            tmp=(tmp*x1+y1)%m;
            if(tmp==a1)
            {
                if(!tmp1) {tmp1=i+1;i=-1;}
                else {cnt1=i+1;break;}
            }
        }
        for(i=0,tmp2=0,tmp=h2,cnt2=0;i<m;i++)
        {
            tmp=(tmp*x2+y2)%m;
            if(tmp==a2)
            {
                if(!tmp2) {tmp2=i+1;i=-1;}
                else {cnt2=i+1;break;}
            }
        }

        if (!tmp1||!tmp2)
        {
            printf("-1\n"); continue;
        }

        if(tmp1==tmp2) {printf("%I64d\n",tmp1);continue;}
        for(i=0,tmp=tmp1;i<=m;tmp+=cnt1,i++)
        {
            if(!cnt2&&tmp==tmp2) {printf("%I64d\n", tmp);break;}
            if(cnt2&&tmp>=tmp2&&(tmp-tmp2)%cnt2==0)
            {
                printf("%I64d\n", tmp);
                break;
            }
        }
        if(i==m+1) printf("-1\n");
    }
    return 0;
}

#include <stdio.h>
int main()
{
	long long int m=1;
	long long int h1=0,a1=0,x1=0,y1=0;
	long long int h2=0,a2=0,x2=0,y2=0;
	scanf("%lld",&m);
	scanf("%lld%lld",&h1,&a1);
	scanf("%lld%lld",&x1,&y1);
	scanf("%lld%lld",&h2,&a2);
	scanf("%lld%lld",&x2,&y2);
	long long int initial1=h1;
	long long int initial2=h2;
	long long int count=0;
	long long int q=0;
	while(++count){
	h1=(x1*h1+y1)%m;
	h2=(x2*h2+y2)%m;
	if(count>m+1){
		printf("-1\n");
		return 0;}

		 if(h1==a1) {
		q=count;
		break;}
	}
	if(h2==a2){
		printf("%lld\n",q );
		return 0;
	}
	long long int c=0;
	while(++c){
		h1=(x1*h1+y1)%m;
		if(h1==a1) break;
		if(c>m+1){
		printf("-1\n");
		return 0;
	}
	if(h1==a1) break;
	}

	long long int i; long long a=1, b=0;
	for(i=0;i<c;i++){
		a=(a*x2)%m;
		b=(b*x2+y2)%m;
	}
	//printf("%lld %lld\n",a,b );
	int count1=0;
	while(h2!=a2){
		count1++;
		h2=(a*h2+b)%m;
		if(h2==a2){
			printf("%lld\n",q+count1*c );
			return 0;
		}
		if(count1>m+1){printf("-1\n");return 0;}
		//printf("%lld\n",h2 );
	}

	return 0;
}

#include<stdio.h>
#define MAXN 1000002

long long number1[MAXN],number2[MAXN];

void gcd(long long a,long long b,long long * d,long long * x,long long * y)
{
    if(!b){*d=a;*x=1;*y=0;}
    else {gcd(b,a%b,d,y,x);(*y)-=(*x)*(a/b);}
}

int main()
{
    long long m,h1,a1,x1,y1,h2,a2,x2,y2;
    long long x,y,d,i,len1,len2,b1,b2;
    long long aa,bb,flag1,flag2,flag11,flag22;

    scanf("%I64d%I64d%I64d%I64d%I64d%I64d%I64d%I64d%I64d",&m,&h1,&a1,&x1,&y1,&h2,&a2,&x2,&y2);
    number1[h1]=1;
    len1=1;
    flag1=1;
    flag11=flag22=0;
    for(x=h1,i=0;i<=m;i++)
    {
        x=(x1*x+y1)%m;
        if(!number1[x])number1[x]=++len1;
        else
        {
            len1=len1+1-number1[x];
            if(number1[a1]>0&&number1[a1]<number1[x])flag11=1;
            break;
        }
    }
    if(number1[a1]>0)b1=number1[a1]-number1[h1];
    else flag1=0;

    flag2=1;
    number2[h2]=1;
    len2=1;
    for(x=h2,i=0;i<=m;i++)
    {
        x=(x2*x+y2)%m;
        if(!number2[x])number2[x]=++len2;
        else
        {
            len2=len2+1-number2[x];
            if(number2[a2]>0&&number2[a2]<number2[x])flag22=1;
            break;
        }
    }
    if(number2[a2]>0)b2=number2[a2]-number2[h2];
    else flag2=0;

    if(b1==b2)
    {
        printf("%I64d\n",b1);
        return 0;
    }
    if(flag1==0|flag2==0)
    {
        printf("-1\n");
        return 0;
    }
    if(flag11&&flag22)
    {
        if(b1==b2)printf("%I64d\n",b1);
        else printf("-1\n");
        return 0;
    }
    else if(flag11)
    {
        if(b1>b2&&(b1-b2)%len2==0)printf("%I64d\n",b1);
        else printf("-1\n");
        return 0;
    }
    else if(flag22)
    {
        if(b2>b1&&(b2-b1)%len1==0)printf("%I64d\n",b2);
        else printf("-1\n");
        return 0;
    }
    gcd(len1,len2,&d,&x,&y);
    if((b2-b1)%d!=0)printf("-1\n");
    else
    {
        if((b2-b1)!=d)
        {
            x*=(b2-b1)/d;
            y*=(b2-b1)/d;
        }
        aa=len1/d;
        bb=len2/d;
        while(x<0||y>0)
        {
            x+=bb;
            y-=aa;
        }
        while(x-bb>=0&&y+aa<=0)
        {
            x-=bb;
            y+=aa;
        }
        printf("%I64d\n",b1+x*len1);
    }

    return 0;
}

/* practice with Dukkha */
#include <stdio.h>
#include <string.h>

#define M	1000000

int used[M];
int i_, m_;

int solve(int h, int a, int x, int y, int m) {
	int i;

	memset(used, -1, sizeof used);
	i_ = -1;
	for (i = 0; used[h] < 0; h = ((long long) x * h + y) % m, i++) {
		used[h] = i;
		if (h == a && i_ == -1)
			i_ = i;
	}
	if (i_ == -1)
		return 0;
	if (i_ < used[h])
		return 1;
	m_ = i - used[h];
	return 2;
}

int solve1(int i, int j, int m) {
	return i >= j && (i - j) % m == 0 ? i : -1;
}

int d_, x_, y_;

void gcd_(int a, int b) {
	if (b == 0) {
		d_ = a;
		x_ = 1, y_ = 0;
	} else {
		int tmp;

		gcd_(b, a % b);
		tmp = x_ - a / b * y_; x_ = y_, y_ = tmp;
	}
}

long long solve2(int i, int n, int j, int m) {
	int a, b, c;
	long long x;

	if (i < j)
		return solve2(j, m, i, n);
	/* i + x * n == j + y * m */
	/* x * n - y * m == j - i */
	/* x_ * n + y_ * m == d_ */
	gcd_(n, m);
	if ((j - i) % d_)
		return -1;
	a = n / d_;
	b = m / d_;
	c = (j - i) / d_;
	/* x * a - y * b == c */
	/* x_ * a + y_ * b == 1 */
	x = (long long) x_ * c % b;
	if (x < 0)
		x += b;
	return i + x * n;
}

int main() {
	int m, h1, a1, x1, y1, h2, a2, x2, y2, i1, m1, c1, i2, m2, c2;
	long long ans;

	scanf("%d%d%d%d%d%d%d%d%d", &m, &h1, &a1, &x1, &y1, &h2, &a2, &x2, &y2);
	c1 = solve(h1, a1, x1, y1, m);
	if (c1 == 0) {
		printf("-1\n");
		return 0;
	}
	i1 = i_, m1 = m_;
	c2 = solve(h2, a2, x2, y2, m);
	if (c2 == 0) {
		printf("-1\n");
		return 0;
	}
	i2 = i_, m2 = m_;
	if (c1 == 1 && c2 == 1)
		ans = i1 == i2 ? i1 : -1;
	else if (c1 == 1 && c2 == 2)
		ans = solve1(i1, i2, m2);
	else if (c1 == 2 && c2 == 1)
		ans = solve1(i2, i1, m1);
	else
		ans = solve2(i1, m1, i2, m2);
	printf("%lld\n", ans < 0 ? -1 : ans);
	return 0;
}

#include<stdio.h>

long long a[1000006],b[1000006],ans,count1,count2,lc,count3,count4,count;

long long lcm(long long a,long long b)
{
	if(a>b)
	{
		if(a%b==0)
			return b;

		return lcm(b,a%b);
	}
	else
	{
		if(b%a==0)
			return a;

		return lcm(a,b%a);
	}
}

int main()
{
	long long m,h1,h2,x1,x2,y1,y2,a1,a2,p,i,n;

	scanf("%lld",&m);

	scanf("%lld%lld",&h1,&a1);

	scanf("%lld%lld",&x1,&y1);

	scanf("%lld%lld",&h2,&a2);

	scanf("%lld%lld",&x2,&y2);

	p=1;

	for(i=0;i<m+2;i++)
		a[i]=b[i]=0;

	count1=count2=0;

	for(i=0;i<m+2;i++)
	{
		if(h1==a1)
		{
			p=0;
			break;
		}

		if(!a[h1])
			a[h1]=1;
		else
		{
			break;
		}

		count1++;
		h1=(h1*x1+y1)%m;
	}

	if(p)
	{
		printf("-1");
		return 0;
	}

	p=1;

	for(i=0;i<m+2;i++)
	{
		if(h2==a2)
		{
			p=0;
			break;
		}

		if(!b[h2])
			b[h2]=1;
		else
		{
			break;
		}

		count2++;
		h2=(h2*x2+y2)%m;
	}

	if(p)
	{
		printf("-1");
		return 0;
	}

	if(count1==count2)
	{
		printf("%lld",count1);
		return 0;
	}

	for(i=0;i<m+2;i++)
		a[i]=b[i]=0;

	h2=(h2*x2+y2)%m;
	h1=(h1*x1+y1)%m;

	//printf("a");

	count3=count4=1;

	for(i=0;i<m+2;i++)
	{
		if(h1==a1)
		{
			count3=-1;
			break;
		}

		if(!a[h1])
			a[h1]=1;
		else
		{
			break;
		}

		count3++;
		h1=(h1*x1+y1)%m;
	}

	for(i=0;i<m+2;i++)
	{
		if(h2==a2)
		{
			count4=-1;
			break;
		}

		if(!b[h2])
			b[h2]=1;
		else
		{
			break;
		}

		count4++;
		h2=(h2*x2+y2)%m;
	}

	count=(count1>count2)?count1:count2;

	if(((count-count1)%count3==0) && ((count-count2)%count4==0))
	{
		printf("%lld",count);
		return 0;
	}

	if(count3==-1 || count4==-1)
	{
		printf("-1");
		return 0;
	}
	i=0;

	//printf("%lld %lld ",count3,count4);

	while(1)
	{
		if(i>10000000)
		{
			printf("-1");
			return 0;
		}
		i++;
		if(((count-count1)%count3==0) && ((count-count2)%count4==0))
			break;
		count=count3*i+count1;
	}

	printf("%lld",count);

	return 0;
}

#include<stdio.h>

long long a[1000006],b[1000006],ans,count1,count2,lc,count3,count4,count;

long long lcm(long long a,long long b)
{
	if(a>b)
	{
		if(a%b==0)
			return b;

		return lcm(b,a%b);
	}
	else
	{
		if(b%a==0)
			return a;

		return lcm(a,b%a);
	}
}

int main()
{
	long long m,h1,h2,x1,x2,y1,y2,a1,a2,p,i,n;

	scanf("%lld",&m);

	scanf("%lld%lld",&h1,&a1);

	scanf("%lld%lld",&x1,&y1);

	scanf("%lld%lld",&h2,&a2);

	scanf("%lld%lld",&x2,&y2);

	p=1;

	for(i=0;i<m+2;i++)
		a[i]=b[i]=0;

	count1=count2=0;

	for(i=0;i<m+2;i++)
	{
		if(h1==a1)
		{
			p=0;
			break;
		}

		if(!a[h1])
			a[h1]=1;
		else
		{
			break;
		}

		count1++;
		h1=(h1*x1+y1)%m;
	}

	if(p)
	{
		printf("-1");
		return 0;
	}

	p=1;

	for(i=0;i<m+2;i++)
	{
		if(h2==a2)
		{
			p=0;
			break;
		}

		if(!b[h2])
			b[h2]=1;
		else
		{
			break;
		}

		count2++;
		h2=(h2*x2+y2)%m;
	}

	if(p)
	{
		printf("-1");
		return 0;
	}

	if(count1==count2)
	{
		printf("%lld",count1);
		return 0;
	}

	for(i=0;i<m+2;i++)
		a[i]=b[i]=0;

	h2=(h2*x2+y2)%m;
	h1=(h1*x1+y1)%m;

	//printf("a");

	count3=count4=1;

	for(i=0;i<m+2;i++)
	{
		if(h1==a1)
		{
			break;
		}

		if(!a[h1])
			a[h1]=1;
		else
		{
			count3=-1;
			break;
		}

		count3++;
		h1=(h1*x1+y1)%m;
	}

	for(i=0;i<m+2;i++)
	{
		if(h2==a2)
		{
			break;
		}

		if(!b[h2])
			b[h2]=1;
		else
		{
			count4=-1;
			break;
		}

		count4++;
		h2=(h2*x2+y2)%m;
	}

	count=(count1>count2)?count1:count2;

	//printf("%lld %lld ",count3,count4);
	//printf("%lld %lld ",count1,count2);

	if(((count-count1)%count3==0) && ((count-count2)%count4==0))
	{
		printf("%lld",count);
		return 0;
	}

	if(count3==-1 && count4==-1)
	{
		printf("-1");
		return 0;
	}
	else if(count3==-1)
	{
		if((count1-count2)%count4==0)
		{
			printf("%lld",count1);
			return 0;
		}
	}
	else if(count4==-1)
	{
		if((count2-count1)%count3==0)
		{
			printf("%lld",count2);
			return 0;
		}
	}

	i=0;

	while(1)
	{
		if(i>10000000)
		{
			printf("-1");
			return 0;
		}
		i++;
		if(((count-count1)%count3==0) && ((count-count2)%count4==0))
			break;
		count=count3*i+count1;
	}

	printf("%lld",count);

	return 0;
}

#include<stdio.h>

long long a[1000006],b[1000006],ans,count1,count2,lc;

long long lcm(long long a,long long b)
{
	if(a>b)
	{
		if(a%b==0)
			return b;

		return lcm(b,a%b);
	}
	else
	{
		if(b%a==0)
			return a;

		return lcm(a,b%a);
	}
}

int main()
{
	long m,h1,h2,x1,x2,y1,y2,a1,a2,p,i;

	scanf("%ld",&m);

	scanf("%ld%ld",&h1,&a1);

	scanf("%ld%ld",&x1,&y1);

	scanf("%ld%ld",&h2,&a2);

	scanf("%ld%ld",&x2,&y2);

	p=1;

	for(i=0;i<m+1;i++)
		a[i]=b[i]=0;

	count1=count2=0;

	if(h1==a1 && h2==a2)
	{
		printf("0");
		return 0;
	}

	if(h1==a1)
	{
		p=0;
		h1=(h1*x1+y1)%m;
		count1++;
	}

	for(i=0;i<m+2;i++)
	{
		if(h1==a1)
		{
			p=0;
			break;
		}

		if(!a[h1])
			a[h1]=1;
		else
		{
			break;
		}

		count1++;
		h1=(h1*x1+y1)%m;
	}

	if(p)
	{
		printf("-1");
		return 0;
	}

	p=1;

	if(h2==a2)
	{
		p=0;
		h2=(h2*x2+y2)%m;
		count2++;
	}

	for(i=0;i<m+2;i++)
	{
		if(h2==a2)
		{
			p=0;
			break;
		}

		if(!b[h2])
			b[h2]=1;
		else
		{
			break;
		}

		count2++;
		h2=(h2*x2+y2)%m;
	}

	if(p)
	{
		printf("-1");
		return 0;
	}

	lc=lcm(count1,count2);

	ans=(count1*count2)/lc;

	printf("%lld",ans);

	return 0;
}

#include<stdio.h>

long long a[1000006],b[1000006],ans,count1,count2,lc;

long long lcm(long long a,long long b)
{
	if(a>b)
	{
		if(a%b==0)
			return b;

		return lcm(b,a%b);
	}
	else
	{
		if(b%a==0)
			return a;

		return lcm(a,b%a);
	}
}

int main()
{
	long m,h1,h2,x1,x2,y1,y2,a1,a2,p,i;

	scanf("%ld",&m);

	scanf("%ld%ld",&h1,&a1);

	scanf("%ld%ld",&x1,&y1);

	scanf("%ld%ld",&h2,&a2);

	scanf("%ld%ld",&x2,&y2);

	p=1;

	for(i=0;i<m+1;i++)
		a[i]=b[i]=0;

	count1=count2=0;

	if(h1==a1 && h2==a2)
	{
		printf("0");
		return 0;
	}

	if(h1==a1)
	{
		h1=(h1*x1+y1)%m;
		count1++;
	}

	for(i=0;i<m+2;i++)
	{
		if(h1==a1)
		{
			p=0;
			break;
		}

		if(!a[h1])
			a[h1]=1;
		else
		{
			break;
		}

		count1++;
		h1=(h1*x1+y1)%m;
	}

	if(p)
	{
		printf("-1");
		return 0;
	}

	p=1;

	if(h2==a2)
	{
		h2=(h2*x2+y2)%m;
		count2++;
	}

	for(i=0;i<m+2;i++)
	{
		if(h2==a2)
		{
			p=0;
			break;
		}

		if(!b[h2])
			b[h2]=1;
		else
		{
			break;
		}

		count2++;
		h2=(h2*x2+y2)%m;
	}

	if(p)
	{
		printf("-1");
		return 0;
	}

	lc=lcm(count1,count2);

	ans=(count1*count2)/lc;

	printf("%lld",ans);

	return 0;
}

#include<stdio.h>

long long a[1000006],b[1000006],ans,count1,count2,lc,count3,count4,count;

long long lcm(long long a,long long b)
{
	if(a>b)
	{
		if(a%b==0)
			return b;

		return lcm(b,a%b);
	}
	else
	{
		if(b%a==0)
			return a;

		return lcm(a,b%a);
	}
}

int main()
{
	long long m,h1,h2,x1,x2,y1,y2,a1,a2,p,i,n;

	scanf("%lld",&m);

	scanf("%lld%lld",&h1,&a1);

	scanf("%lld%lld",&x1,&y1);

	scanf("%lld%lld",&h2,&a2);

	scanf("%lld%lld",&x2,&y2);

	p=1;

	for(i=0;i<m+2;i++)
		a[i]=b[i]=0;

	count1=count2=0;

	for(i=0;i<m+2;i++)
	{
		if(h1==a1)
		{
			p=0;
			break;
		}

		if(!a[h1])
			a[h1]=1;
		else
		{
			break;
		}

		count1++;
		h1=(h1*x1+y1)%m;
	}

	if(p)
	{
		printf("-1");
		return 0;
	}

	p=1;

	for(i=0;i<m+2;i++)
	{
		if(h2==a2)
		{
			p=0;
			break;
		}

		if(!b[h2])
			b[h2]=1;
		else
		{
			break;
		}

		count2++;
		h2=(h2*x2+y2)%m;
	}

	if(p)
	{
		printf("-1");
		return 0;
	}

	if(count1==count2)
	{
		printf("%lld",count1);
		return 0;
	}

	for(i=0;i<m+2;i++)
		a[i]=b[i]=0;

	h2=(h2*x2+y2)%m;
	h1=(h1*x1+y1)%m;

	//printf("a");

	count3=count4=1;

	for(i=0;i<m+2;i++)
	{
		if(h1==a1)
		{
			break;
		}

		if(!a[h1])
			a[h1]=1;
		else
		{
			count3=-1;
			break;
		}

		count3++;
		h1=(h1*x1+y1)%m;
	}

	for(i=0;i<m+2;i++)
	{
		if(h2==a2)
		{
			break;
		}

		if(!b[h2])
			b[h2]=1;
		else
		{
			count4=-1;
			break;
		}

		count4++;
		h2=(h2*x2+y2)%m;
	}

	count=(count1>count2)?count1:count2;

	//printf("%lld %lld ",count3,count4);
	//printf("%lld %lld ",count1,count2);

	if(((count-count1)%count3==0) && ((count-count2)%count4==0))
	{
		printf("%lld",count);
		return 0;
	}

	if(count3==-1 || count4==-1)
	{
		printf("-1");
		return 0;
	}
	i=0;

	while(1)
	{
		if(i>10000000)
		{
			printf("-1");
			return 0;
		}
		i++;
		if(((count-count1)%count3==0) && ((count-count2)%count4==0))
			break;
		count=count3*i+count1;
	}

	printf("%lld",count);

	return 0;
}

#include<stdio.h>

long long a[1000006],b[1000006],ans,count1,count2,lc;

long long lcm(long long a,long long b)
{
	if(a>b)
	{
		if(a%b==0)
			return b;

		return lcm(b,a%b);
	}
	else
	{
		if(b%a==0)
			return a;

		return lcm(a,b%a);
	}
}

int main()
{
	long m,h1,h2,x1,x2,y1,y2,a1,a2,p,i;

	scanf("%ld",&m);

	scanf("%ld%ld",&h1,&a1);

	scanf("%ld%ld",&x1,&y1);

	scanf("%ld%ld",&h2,&a2);

	scanf("%ld%ld",&x2,&y2);

	p=1;

	for(i=0;i<m+1;i++)
		a[i]=b[i]=0;

	count1=count2=0;

	for(i=0;i<m+2;i++)
	{
		if(h1==a1)
		{
			p=0;
			break;
		}

		if(!a[h1])
			a[h1]=1;
		else
		{
			break;
		}

		count1++;
		h1=(h1*x1+y1)%m;
	}

	if(p)
	{
		printf("-1");
		return 0;
	}

	p=1;

	for(i=0;i<m+2;i++)
	{
		if(h2==a2)
		{
			p=0;
			break;
		}

		if(!b[h2])
			b[h2]=1;
		else
		{
			break;
		}

		count2++;
		h2=(h2*x2+y2)%m;
	}

	if(p)
	{
		printf("-1");
		return 0;
	}

	lc=lcm(count1,count2);

	ans=(count1*count2)/lc;

	printf("%lld",ans);

	return 0;
}

#include<stdio.h>

long long a[1000006],b[1000006],ans,count1,count2,lc,count3,count4,count;

long long lcm(long long a,long long b)
{
	if(a>b)
	{
		if(a%b==0)
			return b;

		return lcm(b,a%b);
	}
	else
	{
		if(b%a==0)
			return a;

		return lcm(a,b%a);
	}
}

int main()
{
	long long m,h1,h2,x1,x2,y1,y2,a1,a2,p,i,n;

	scanf("%lld",&m);

	scanf("%lld%lld",&h1,&a1);

	scanf("%lld%lld",&x1,&y1);

	scanf("%lld%lld",&h2,&a2);

	scanf("%lld%lld",&x2,&y2);

	p=1;

	for(i=0;i<m+2;i++)
		a[i]=b[i]=0;

	count1=count2=0;

	for(i=0;i<m+2;i++)
	{
		if(h1==a1)
		{
			p=0;
			break;
		}

		if(!a[h1])
			a[h1]=1;
		else
		{
			break;
		}

		count1++;
		h1=(h1*x1+y1)%m;
	}

	if(p)
	{
		printf("-1");
		return 0;
	}

	p=1;

	for(i=0;i<m+2;i++)
	{
		if(h2==a2)
		{
			p=0;
			break;
		}

		if(!b[h2])
			b[h2]=1;
		else
		{
			break;
		}

		count2++;
		h2=(h2*x2+y2)%m;
	}

	if(p)
	{
		printf("-1");
		return 0;
	}

	for(i=0;i<m+2;i++)
		a[i]=b[i]=0;

	h2=(h2*x2+y2)%m;
	h1=(h1*x1+y1)%m;

	count3=count4=1;

	p=1;

	for(i=0;i<m+2;i++)
	{
		if(h1==a1)
		{
			p=0;
			break;
		}

		if(!a[h1])
			a[h1]=1;
		else
		{
			break;
		}

		count3++;
		h1=(h1*x1+y1)%m;
	}

	if(p)
	{
		printf("-1");
		return 0;
	}

	p=1;

	for(i=0;i<m+2;i++)
	{
		if(h2==a2)
		{
			p=0;
			break;
		}

		if(!b[h2])
			b[h2]=1;
		else
		{
			break;
		}

		count4++;
		h2=(h2*x2+y2)%m;
	}

	if(p)
	{
		printf("-1");
		return 0;
	}

	if(count1==count2)
	{
		printf("%lld",count1);
		return 0;
	}

	if(count3==count4)
	{
		printf("-1");
		return 0;
	}

	count=(count1>count2)?count1:count2;
	i=0;

	//printf("%lld %lld ",count1,count2);
	//printf("%lld %lld ",count3,count4);

	while(1)
	{
		if(i>10000000)
		{
			printf("-1");
			return 0;
		}
		i++;
		if(((count-count1)%count3==0) && ((count-count2)%count4==0))
			break;
		count=count3*i+count1;
	}

	printf("%lld",count);

	return 0;
}

#include<stdio.h>

long long a[1000006],b[1000006],ans,count1,count2,lc,count3,count4,count;

long long lcm(long long a,long long b)
{
	if(a>b)
	{
		if(a%b==0)
			return b;

		return lcm(b,a%b);
	}
	else
	{
		if(b%a==0)
			return a;

		return lcm(a,b%a);
	}
}

int main()
{
	long long m,h1,h2,x1,x2,y1,y2,a1,a2,p,i,n;

	scanf("%lld",&m);

	scanf("%lld%lld",&h1,&a1);

	scanf("%lld%lld",&x1,&y1);

	scanf("%lld%lld",&h2,&a2);

	scanf("%lld%lld",&x2,&y2);

	p=1;

	for(i=0;i<m+2;i++)
		a[i]=b[i]=0;

	count1=count2=0;

	for(i=0;i<m+2;i++)
	{
		if(h1==a1)
		{
			p=0;
			break;
		}

		if(!a[h1])
			a[h1]=1;
		else
		{
			break;
		}

		count1++;
		h1=(h1*x1+y1)%m;
	}

	if(p)
	{
		printf("-1");
		return 0;
	}

	p=1;

	for(i=0;i<m+2;i++)
	{
		if(h2==a2)
		{
			p=0;
			break;
		}

		if(!b[h2])
			b[h2]=1;
		else
		{
			break;
		}

		count2++;
		h2=(h2*x2+y2)%m;
	}

	if(p)
	{
		printf("-1");
		return 0;
	}

	for(i=0;i<m+2;i++)
		a[i]=b[i]=0;

	h2=(h2*x2+y2)%m;
	h1=(h1*x1+y1)%m;

	//printf("a");

	count3=count4=1;

	p=1;

	for(i=0;i<m+2;i++)
	{
		if(h1==a1)
		{
			p=0;
			break;
		}

		if(!a[h1])
			a[h1]=1;
		else
		{
			break;
		}

		count3++;
		h1=(h1*x1+y1)%m;
	}

	if(p)
	{
		printf("-1");
		return 0;
	}

	p=1;

	for(i=0;i<m+2;i++)
	{
		if(h2==a2)
		{
			p=0;
			break;
		}

		if(!b[h2])
			b[h2]=1;
		else
		{
			break;
		}

		count4++;
		h2=(h2*x2+y2)%m;
	}

	if(p)
	{
		printf("-1");
		return 0;
	}

	if(count1==count2)
	{
		printf("%lld",count1);
		return 0;
	}

	count=(count1>count2)?count1:count2;
	i=0;

	//printf("%lld %lld ",count1,count2);
	//printf("%lld %lld ",count3,count4);

	while(1)
	{
		if(i>10000000)
		{
			printf("-1");
			return 0;
		}
		i++;
		if(((count-count1)%count3==0) && ((count-count2)%count4==0))
			break;
		count=count3*i+count1;
	}

	printf("%lld",count);

	return 0;
}

#include<stdio.h>

long long a[1000006],b[1000006],ans,count1,count2,lc,count3,count4;

long long lcm(long long a,long long b)
{
	if(a>b)
	{
		if(a%b==0)
			return b;

		return lcm(b,a%b);
	}
	else
	{
		if(b%a==0)
			return a;

		return lcm(a,b%a);
	}
}

int main()
{
	long long m,h1,h2,x1,x2,y1,y2,a1,a2,p,i;

	scanf("%lld",&m);

	scanf("%lld%lld",&h1,&a1);

	scanf("%lld%lld",&x1,&y1);

	scanf("%lld%lld",&h2,&a2);

	scanf("%lld%lld",&x2,&y2);

	p=1;

	for(i=0;i<m+2;i++)
		a[i]=b[i]=0;

	count1=count2=0;

	for(i=0;i<m+2;i++)
	{
		if(h1==a1)
		{
			p=0;
			break;
		}

		if(!a[h1])
			a[h1]=1;
		else
		{
			break;
		}

		count1++;
		h1=(h1*x1+y1)%m;
	}

	if(p)
	{
		printf("-1");
		return 0;
	}

	p=1;

	for(i=0;i<m+2;i++)
	{
		if(h2==a2)
		{
			p=0;
			break;
		}

		if(!b[h2])
			b[h2]=1;
		else
		{
			break;
		}

		count2++;
		h2=(h2*x2+y2)%m;
	}

	if(p)
	{
		printf("-1");
		return 0;
	}

	for(i=0;i<m+2;i++)
		a[i]=b[i]=0;

	h2=(h2*x2+y2)%m;
	h1=(h1*x1+y1)%m;

	count3=count4=1;

	for(i=0;i<m+2;i++)
	{
		if(h1==a1)
		{
			p=0;
			break;
		}

		if(!a[h1])
			a[h1]=1;
		else
		{
			break;
		}

		count3++;
		h1=(h1*x1+y1)%m;
	}

	for(i=0;i<m+2;i++)
	{
		if(h2==a2)
		{
			p=0;
			break;
		}

		if(!b[h2])
			b[h2]=1;
		else
		{
			break;
		}

		count4++;
		h2=(h2*x2+y2)%m;
	}

	if(count1==count2)
	{
		printf("%lld",count1);
		return 0;
	}

	if(count1>count2)
	{
		count1=count1-count2;
		lc=lcm(count1,count4);
		ans=(count1*count4)/lc;
	}
	else
	{
		count2=count2-count1;
		lc=lcm(count3,count2);
		ans=(count3*count2)/lc;
	}

	printf("%lld",ans);

	return 0;
}

#include<stdio.h>

long long a[1000006],b[1000006],ans,count1,count2,lc,count3,count4,count;

long long lcm(long long a,long long b)
{
	if(a>b)
	{
		if(a%b==0)
			return b;

		return lcm(b,a%b);
	}
	else
	{
		if(b%a==0)
			return a;

		return lcm(a,b%a);
	}
}

int main()
{
	long long m,h1,h2,x1,x2,y1,y2,a1,a2,p,i,n;

	scanf("%lld",&m);

	scanf("%lld%lld",&h1,&a1);

	scanf("%lld%lld",&x1,&y1);

	scanf("%lld%lld",&h2,&a2);

	scanf("%lld%lld",&x2,&y2);

	p=1;

	for(i=0;i<m+2;i++)
		a[i]=b[i]=0;

	count1=count2=0;

	for(i=0;i<m+2;i++)
	{
		if(h1==a1)
		{
			p=0;
			break;
		}

		if(!a[h1])
			a[h1]=1;
		else
		{
			break;
		}

		count1++;
		h1=(h1*x1+y1)%m;
	}

	if(p)
	{
		printf("-1");
		return 0;
	}

	p=1;

	for(i=0;i<m+2;i++)
	{
		if(h2==a2)
		{
			p=0;
			break;
		}

		if(!b[h2])
			b[h2]=1;
		else
		{
			break;
		}

		count2++;
		h2=(h2*x2+y2)%m;
	}

	if(p)
	{
		printf("-1");
		return 0;
	}

	if(count1==count2)
	{
		printf("%lld",count1);
		return 0;
	}

	for(i=0;i<m+2;i++)
		a[i]=b[i]=0;

	h2=(h2*x2+y2)%m;
	h1=(h1*x1+y1)%m;

	//printf("a");

	count3=count4=1;

	p=1;

	for(i=0;i<m+2;i++)
	{
		if(h1==a1)
		{
			p=0;
			break;
		}

		if(!a[h1])
			a[h1]=1;
		else
		{
			break;
		}

		count3++;
		h1=(h1*x1+y1)%m;
	}

	if(p)
	{
		printf("-1");
		return 0;
	}

	p=1;

	for(i=0;i<m+2;i++)
	{
		if(h2==a2)
		{
			p=0;
			break;
		}

		if(!b[h2])
			b[h2]=1;
		else
		{
			break;
		}

		count4++;
		h2=(h2*x2+y2)%m;
	}

	if(p)
	{
		printf("-1");
		return 0;
	}

	count=(count1>count2)?count1:count2;
	i=0;

	printf("%lld %lld ",count1,count2);
	printf("%lld %lld ",count3,count4);

	while(1)
	{
		if(i>10000000)
		{
			printf("-1");
			return 0;
		}
		i++;
		if(((count-count1)%count3==0) && ((count-count2)%count4==0))
			break;
		count=count3*i+count1;
	}

	printf("%lld",count);

	return 0;
}

#include<stdio.h>

long long a[1000006],b[1000006],ans,count1,count2,lc,count3,count4;

long long lcm(long long a,long long b)
{
	if(a>b)
	{
		if(a%b==0)
			return b;

		return lcm(b,a%b);
	}
	else
	{
		if(b%a==0)
			return a;

		return lcm(a,b%a);
	}
}

int main()
{
	long long m,h1,h2,x1,x2,y1,y2,a1,a2,p,i,n;

	scanf("%lld",&m);

	scanf("%lld%lld",&h1,&a1);

	scanf("%lld%lld",&x1,&y1);

	scanf("%lld%lld",&h2,&a2);

	scanf("%lld%lld",&x2,&y2);

	p=1;

	for(i=0;i<m+2;i++)
		a[i]=b[i]=0;

	count1=count2=0;

	for(i=0;i<m+2;i++)
	{
		if(h1==a1)
		{
			p=0;
			break;
		}

		if(!a[h1])
			a[h1]=1;
		else
		{
			break;
		}

		count1++;
		h1=(h1*x1+y1)%m;
	}

	if(p)
	{
		printf("-1");
		return 0;
	}

	p=1;

	for(i=0;i<m+2;i++)
	{
		if(h2==a2)
		{
			p=0;
			break;
		}

		if(!b[h2])
			b[h2]=1;
		else
		{
			break;
		}

		count2++;
		h2=(h2*x2+y2)%m;
	}

	if(p)
	{
		printf("-1");
		return 0;
	}

	for(i=0;i<m+2;i++)
		a[i]=b[i]=0;

	h2=(h2*x2+y2)%m;
	h1=(h1*x1+y1)%m;

	count3=count4=1;

	for(i=0;i<m+2;i++)
	{
		if(h1==a1)
		{
			p=0;
			break;
		}

		if(!a[h1])
			a[h1]=1;
		else
		{
			break;
		}

		count3++;
		h1=(h1*x1+y1)%m;
	}

	for(i=0;i<m+2;i++)
	{
		if(h2==a2)
		{
			p=0;
			break;
		}

		if(!b[h2])
			b[h2]=1;
		else
		{
			break;
		}

		count4++;
		h2=(h2*x2+y2)%m;
	}

	if(count1==count2)
	{
		printf("%lld",count1);
		return 0;
	}

	n=(count1-count2)/(count4-count3)+1;

	ans=count1+(n-1)*count3;

	printf("%lld",ans);

	return 0;
}

#include<stdio.h>

long long a[1000006],b[1000006],ans,count1,count2,lc,count3,count4,count;

long long lcm(long long a,long long b)
{
	if(a>b)
	{
		if(a%b==0)
			return b;

		return lcm(b,a%b);
	}
	else
	{
		if(b%a==0)
			return a;

		return lcm(a,b%a);
	}
}

int main()
{
	long long m,h1,h2,x1,x2,y1,y2,a1,a2,p,i,n;

	scanf("%lld",&m);

	scanf("%lld%lld",&h1,&a1);

	scanf("%lld%lld",&x1,&y1);

	scanf("%lld%lld",&h2,&a2);

	scanf("%lld%lld",&x2,&y2);

	p=1;

	for(i=0;i<m+2;i++)
		a[i]=b[i]=0;

	count1=count2=0;

	for(i=0;i<m+2;i++)
	{
		if(h1==a1)
		{
			p=0;
			break;
		}

		if(!a[h1])
			a[h1]=1;
		else
		{
			break;
		}

		count1++;
		h1=(h1*x1+y1)%m;
	}

	if(p)
	{
		printf("-1");
		return 0;
	}

	p=1;

	for(i=0;i<m+2;i++)
	{
		if(h2==a2)
		{
			p=0;
			break;
		}

		if(!b[h2])
			b[h2]=1;
		else
		{
			break;
		}

		count2++;
		h2=(h2*x2+y2)%m;
	}

	if(p)
	{
		printf("-1");
		return 0;
	}

	if(count1==count2)
	{
		printf("%lld",count1);
		return 0;
	}

	for(i=0;i<m+2;i++)
		a[i]=b[i]=0;

	h2=(h2*x2+y2)%m;
	h1=(h1*x1+y1)%m;

	//printf("a");

	count3=count4=1;

	p=1;

	for(i=0;i<m+2;i++)
	{
		if(h1==a1)
		{
			p=0;
			break;
		}

		if(!a[h1])
			a[h1]=1;
		else
		{
			break;
		}

		count3++;
		h1=(h1*x1+y1)%m;
	}

	if(p)
	{
		printf("-1");
		return 0;
	}

	p=1;

	for(i=0;i<m+2;i++)
	{
		if(h2==a2)
		{
			p=0;
			break;
		}

		if(!b[h2])
			b[h2]=1;
		else
		{
			break;
		}

		count4++;
		h2=(h2*x2+y2)%m;
	}

	if(p)
	{
		printf("-1");
		return 0;
	}

	count=(count1>count2)?count1:count2;
	i=0;

	//printf("%lld %lld ",count1,count2);
	//printf("%lld %lld ",count3,count4);

	while(1)
	{
		if(i>10000000)
		{
			printf("-1");
			return 0;
		}
		i++;
		if(((count-count1)%count3==0) && ((count-count2)%count4==0))
			break;
		count=count3*i+count1;
	}

	printf("%lld",count);

	return 0;
}

#include<stdio.h>

long long a[1000006],b[1000006],ans,count1,count2,lc,count3,count4,count;

long long lcm(long long a,long long b)
{
	if(a>b)
	{
		if(a%b==0)
			return b;

		return lcm(b,a%b);
	}
	else
	{
		if(b%a==0)
			return a;

		return lcm(a,b%a);
	}
}

int main()
{
	long long m,h1,h2,x1,x2,y1,y2,a1,a2,p,i,n;

	scanf("%lld",&m);

	scanf("%lld%lld",&h1,&a1);

	scanf("%lld%lld",&x1,&y1);

	scanf("%lld%lld",&h2,&a2);

	scanf("%lld%lld",&x2,&y2);

	p=1;

	for(i=0;i<m+2;i++)
		a[i]=b[i]=0;

	count1=count2=0;

	for(i=0;i<m+2;i++)
	{
		if(h1==a1)
		{
			p=0;
			break;
		}

		if(!a[h1])
			a[h1]=1;
		else
		{
			break;
		}

		count1++;
		h1=(h1*x1+y1)%m;
	}

	if(p)
	{
		printf("-1");
		return 0;
	}

	p=1;

	for(i=0;i<m+2;i++)
	{
		if(h2==a2)
		{
			p=0;
			break;
		}

		if(!b[h2])
			b[h2]=1;
		else
		{
			break;
		}

		count2++;
		h2=(h2*x2+y2)%m;
	}

	if(p)
	{
		printf("-1");
		return 0;
	}

	if(count1==count2)
	{
		printf("%lld",count1);
		return 0;
	}

	for(i=0;i<m+2;i++)
		a[i]=b[i]=0;

	h2=(h2*x2+y2)%m;
	h1=(h1*x1+y1)%m;

	//printf("a");

	count3=count4=1;

	for(i=0;i<m+2;i++)
	{
		if(h1==a1)
		{
			break;
		}

		if(!a[h1])
			a[h1]=1;
		else
		{
			count3=-1;
			break;
		}

		count3++;
		h1=(h1*x1+y1)%m;
	}

	for(i=0;i<m+2;i++)
	{
		if(h2==a2)
		{
			break;
		}

		if(!b[h2])
			b[h2]=1;
		else
		{
			count4=-1;
			break;
		}

		count4++;
		h2=(h2*x2+y2)%m;
	}

	count=(count1>count2)?count1:count2;

	//printf("%lld %lld ",count3,count4);
	//printf("%lld %lld ",count1,count2);

	if(count3==-1 && count4==-1)
	{
		printf("-1");
		return 0;
	}
	else if(count3==-1)
	{
		if(count1<count2)
		{
			printf("-1");
			return 0;
		}

		if((count1-count2)%count4==0)
		{
			printf("%lld",count1);
			return 0;
		}
	}
	else if(count4==-1)
	{
		if(count2<count1)
		{
			printf("-1");
			return 0;
		}

		if((count2-count1)%count3==0)
		{
			printf("%lld",count2);
			return 0;
		}
	}

	i=0;

	while(1)
	{
		if(i>10000000)
		{
			printf("-1");
			return 0;
		}
		i++;
		if(((count-count1)%count3==0) && ((count-count2)%count4==0))
			break;
		count=count3*i+count1;
	}

	printf("%lld",count);

	return 0;
}

#include<stdio.h>

long long a[1000006],b[1000006],ans,count1,count2,lc,count3,count4,count;

long long lcm(long long a,long long b)
{
	if(a>b)
	{
		if(a%b==0)
			return b;

		return lcm(b,a%b);
	}
	else
	{
		if(b%a==0)
			return a;

		return lcm(a,b%a);
	}
}

int main()
{
	long long m,h1,h2,x1,x2,y1,y2,a1,a2,p,i,n;

	scanf("%lld",&m);

	scanf("%lld%lld",&h1,&a1);

	scanf("%lld%lld",&x1,&y1);

	scanf("%lld%lld",&h2,&a2);

	scanf("%lld%lld",&x2,&y2);

	p=1;

	for(i=0;i<m+2;i++)
		a[i]=b[i]=0;

	count1=count2=0;

	for(i=0;i<m+2;i++)
	{
		if(h1==a1)
		{
			p=0;
			break;
		}

		if(!a[h1])
			a[h1]=1;
		else
		{
			break;
		}

		count1++;
		h1=(h1*x1+y1)%m;
	}

	if(p)
	{
		printf("-1");
		return 0;
	}

	p=1;

	for(i=0;i<m+2;i++)
	{
		if(h2==a2)
		{
			p=0;
			break;
		}

		if(!b[h2])
			b[h2]=1;
		else
		{
			break;
		}

		count2++;
		h2=(h2*x2+y2)%m;
	}

	if(p)
	{
		printf("-1");
		return 0;
	}

	for(i=0;i<m+2;i++)
		a[i]=b[i]=0;

	h2=(h2*x2+y2)%m;
	h1=(h1*x1+y1)%m;

	count3=count4=1;

	for(i=0;i<m+2;i++)
	{
		if(h1==a1)
		{
			p=0;
			break;
		}

		if(!a[h1])
			a[h1]=1;
		else
		{
			break;
		}

		count3++;
		h1=(h1*x1+y1)%m;
	}

	for(i=0;i<m+2;i++)
	{
		if(h2==a2)
		{
			p=0;
			break;
		}

		if(!b[h2])
			b[h2]=1;
		else
		{
			break;
		}

		count4++;
		h2=(h2*x2+y2)%m;
	}

	if(count1==count2)
	{
		printf("%lld",count1);
		return 0;
	}

	count=0;

	while(1)
	{
		if(((count-count1)%count3==0) && ((count-count2)%count4==0))
			break;
		count++;
	}

	printf("%lld",count);

	return 0;
}

#include <stdio.h>

int main()
{
    long m,i;

    scanf("%ld",&m);

    long h1,a1;
    scanf("%ld %ld",&h1,&a1);

    long x1,y1;
    scanf("%ld %ld",&x1,&y1);

    long h2,a2;
    scanf("%ld %ld",&h2,&a2);

    long x2,y2;
    scanf("%ld %ld",&x2,&y2);

    long count=0;

    long initial1 = h1;
    long initial2 = h2;

        long flag=0;
        long temp=0;

        while (h1!=a1)
        {

            h1 = (((x1%m)*(h1%m))%m + y1%m)%m;
            h2 = (((x2%m)*(h2%m))%m + y2%m)%m;
            //printf("h1 is %ld\n",h1);
            //printf("h2 is %ld\n",h2);
            count = count+1;
            temp = temp+1;
            if (temp> (m+10))
            {
                flag=1;
                printf("-1\n");
                break;
            }

        }
    //printf("count is %ld\n",count);

        if (flag==0)
        {
            long temp1=0;

            if (h2==a2)
            {
                printf("%ld\n",count);
                flag=1;

            }
            else
            {
                long z1=1, z2=0;
                while (h1!=a1 || temp1==0)
                {
                    h1 = (((x1%m)*(h1%m))%m + y1%m)%m;
                    //h2 = (((x2%m)*(h2%m))%m + y2%m)%m;
                    z1 = ((z1%m) * (x2%m))%m;
                    z2 = (((z2%m)*(x2%m))%m + y2%m)%m;
                    temp1++;
                    //count++;
                    if (temp1>(m+10))
                    {
                        flag=1;
                        //prlongf("here\n");
                        printf("-1\n");
                        break;
                    }
                }

                if (flag==0)
                {

                    long temp2=0;

                    while (h2!=a2)
                    {
                        h2 = (((z1%m)*(h2%m))%m + z2%m)%m;
                        temp2++;
                        if (temp2 > (m+10))
                        {
                            flag=1;
                            //prlongf("second\n");
                            printf("-1\n");
                            break;
                        }
                    }

                    if (flag==0)
                    {
                        printf("%ld\n",count+temp1*temp2);
                    }

                }

            }

        }

    return 0;
}

#include <stdio.h>

int main()
{
    long m,i;

    scanf("%ld",&m);

    long h1,a1;
    scanf("%ld %ld",&h1,&a1);

    long x1,y1;
    scanf("%ld %ld",&x1,&y1);

    long h2,a2;
    scanf("%ld %ld",&h2,&a2);

    long x2,y2;
    scanf("%ld %ld",&x2,&y2);

    long count=0;

    long initial1 = h1;
    long initial2 = h2;

        long flag=0;
        long temp=0;

        while (h1!=a1)
        {

            h1 = (((x1%m)*(h1%m))%m + y1%m)%m;
            h2 = (((x2%m)*(h2%m))%m + y2%m)%m;
            //printf("h1 is %ld\n",h1);
            //printf("h2 is %ld\n",h2);
            count = count+1;
            temp = temp+1;
            if (temp> (m+1))
            {
                flag=1;
                printf("-1\n");
                break;
            }

        }
    //printf("count is %ld\n",count);

        if (flag==0)
        {
            long temp1=0;

            if (h2==a2)
            {
                printf("%ld\n",count);
                flag=1;

            }
            else
            {
                long z1=1, z2=0;
                while (h1!=a1 || temp1==0)
                {
                    h1 = (((x1%m)*(h1%m))%m + y1%m)%m;
                    //h2 = (((x2%m)*(h2%m))%m + y2%m)%m;
                    z1 = ((z1%m) * (x2%m))%m;
                    z2 = (((z2%m)*(x2%m))%m + y2%m)%m;
                    temp1++;
                    //count++;
                    if (temp1>(m+1))
                    {
                        flag=1;
                        //prlongf("here\n");
                        printf("-1\n");
                        break;
                    }
                }

                if (flag==0)
                {

                    long temp2=0;

                    while (h2!=a2)
                    {
                        h2 = (((z1%m)*(h2%m))%m + z2%m)%m;
                        temp2++;
                        if (temp2 > (m+1))
                        {
                            flag=1;
                            //prlongf("second\n");
                            printf("-1\n");
                            break;
                        }
                    }

                    if (flag==0)
                    {
                        printf("%ld\n",count+temp1*temp2);
                    }

                }

            }

        }

    return 0;
}

#include <stdio.h>

int main()
{
    int m,i;

    scanf("%d",&m);

    int h1,a1;
    scanf("%d %d",&h1,&a1);

    int x1,y1;
    scanf("%d %d",&x1,&y1);

    int h2,a2;
    scanf("%d %d",&h2,&a2);

    int x2,y2;
    scanf("%d %d",&x2,&y2);

    int count=0;

    int initial1 = h1;
    int initial2 = h2;
    int flag=0;
    while (h1!=a1 || h2!=a2)
    {

        h1 = (((x1%m)*(h1%m))%m + y1%m)%m;
        h2 = (((x2%m)*(h2%m))%m + y2%m)%m;
        count = count+1;

        if (h1==initial1 && h2==initial2 || count > m)
        {
            flag=1;
            break;
        }
    }
    if (flag==1)
        printf("-1\n");
    else
        printf("%d\n",count);

    return 0;
}

#include <stdio.h>

int main()
{
    int m,i;

    scanf("%d",&m);

    int h1,a1;
    scanf("%d %d",&h1,&a1);

    int x1,y1;
    scanf("%d %d",&x1,&y1);

    int h2,a2;
    scanf("%d %d",&h2,&a2);

    int x2,y2;
    scanf("%d %d",&x2,&y2);

    int count=0;

    int initial1 = h1;
    int initial2 = h2;

        int flag=0;
        int temp=0;

        while (h1!=a1)
        {

            h1 = (((x1%m)*(h1%m))%m + y1%m)%m;
            h2 = (((x2%m)*(h2%m))%m + y2%m)%m;

            count = count+1;
            temp = temp+1;
            if (temp>m+1)
            {
                flag=1;
                printf("-1\n");
                break;
            }

        }

        if (flag==0)
        {
            int temp1=0;

            if (h2==a2)
            {
                printf("%d\n",count);
                flag=1;

            }
            else
            {

                while (h1!=a1 || temp1==0)
                {
                    h1 = (((x1%m)*(h1%m))%m + y1%m)%m;
                    //h2 = (((x2%m)*(h2%m))%m + y2%m)%m;
                    temp1++;
                    //count++;
                    if (temp1>m+1)
                    {
                        flag=1;
                        printf("-1\n");
                        break;
                    }
                }

                if (flag==0)
                {

                    int temp2=0;

                    while (h2!=a2)
                    {
                        h2 = (((x2%m)*(h2%m))%m + y2%m)%m;
                        temp2++;
                        if (temp2 > m+1)
                        {
                            flag=1;
                            printf("-1\n");
                            break;
                        }
                    }

                    if (flag==0)
                    {
                        printf("%d\n",count+temp1*temp2);
                    }

                }

            }

        }

    return 0;
}

#include <stdio.h>

int main()
{
    int m,i;

    scanf("%d",&m);

    int h1,a1;
    scanf("%d %d",&h1,&a1);

    int x1,y1;
    scanf("%d %d",&x1,&y1);

    int h2,a2;
    scanf("%d %d",&h2,&a2);

    int x2,y2;
    scanf("%d %d",&x2,&y2);

    int count=0;

    int initial1 = h1;
    int initial2 = h2;

    while (1)
    {

        int flag=0;
        int temp=0;
        while (h1!=a1)
        {

            h1 = (((x1%m)*(h1%m))%m + y1%m)%m;
            h2 = (((x2%m)*(h2%m))%m + y2%m)%m;

            count = count+1;
            temp = temp+1;
            if (temp>m+1)
            {
                flag=1;
                printf("-1\n");
                break;
            }

        }

        if (flag==0)
        {
            int temp1=0;

            if (h2==a2)
            {
                printf("%d\n",count);
                flag=1;
                break;
            }
            else
            {
                while (h2!=a2)
                {
                    h1 = (((x1%m)*(h1%m))%m + y1%m)%m;
                    h2 = (((x2%m)*(h2%m))%m + y2%m)%m;
                    temp1++;
                    count++;
                    if (temp1>m+1)
                    {
                        flag=1;
                        printf("-1\n");
                        break;
                    }
                }

            }

        }
        if (flag==1)
            break;
    }

    return 0;
}

#include<stdio.h>
int p,h1,a1,x1,y,h2,a2,x2,y2;
int hone(int h)
{
	return (x1*h+y)%p;
}
int htwo(int h)
{
	return (x2*h+y2)%p;
}
int main()
{
	scanf("%d %d %d %d %d %d %d %d %d",&p,&a1,&h1,&x1,&y,&a2,&h2,&x2,&y2);
	int i,hi=a1,c=0,d=1,x=0,h=hone(h1);
	while(hi!=h1 && c<=p)
	{
		hi=hone(hi);
		c++;
	}
	if(c>p)
		printf("-1\n");
	else
	{
		hi=a2;
		for(i=0;i<c;i++)
			hi=htwo(hi);
		if(hi==h2)
			printf("%d\n",c);
		else
		{
			while(h!=h1 && d<=p)
			{
				h=hone(h);
				d++;
			}
			if(d>p)
				printf("-1\n");
			else
			{
				while(x<=p && hi!=h2)
				{
					for(i=0;i<d;i++)
						hi=htwo(hi);
					x++;
				}
				if(x>p)
					printf("-1\n");
				else
					printf("%d\n",c+x*d);
			}
		}
	}
	return 0;
}

#include <stdio.h>
#include <stdlib.h>

typedef struct Node {
    unsigned long value;
    struct Node *left;
    struct Node *right;
} Node;

Node *create_node(unsigned long value) {
    Node *node = malloc(sizeof(Node));
    node->value = value;
    node->left = node->right = NULL;
    return node;
}

/**
 * @return 1 if insert successfully, 0 other wise
 */
int add_to_tree(Node *root, Node *new_node) {
    if (root->value == new_node->value)
        return 0;
    if (root->value > new_node->value) {
        if (root->left == NULL)
            root->left = new_node;
        else
            return add_to_tree(root->left, new_node);
    } else {
        if (root->right == NULL)
            root->right = new_node;
        else
            return add_to_tree(root->right, new_node);
    }
    return 1;
}

long count_step(unsigned long h, unsigned long a, unsigned long x, unsigned long y, unsigned int m) {
    Node *root = create_node(h);
    unsigned long c = 0;
    while (h != a) {
        h = (x * h + y) % m;
        Node *new_node = create_node(h);
        if (!add_to_tree(root, new_node)) {
            return -1;
        }
        c++;
    }
    return c;
}

void no_solution() {
    puts("-1");
    exit(0);
}

long gcd(long a, long b) {
    long t;
    while (b != 0) {
        t = b;
        b = a % b;
        a = t;
    }
    return a;
}

/**
 * v1 == 0
 */
void zero(long u1, long u2, long v2) {
    if (v2 == 0) {
        if (u1 != u2)
            no_solution();
        else
            printf("%lu", u1);
    } else {
        if (u1 < u2 || (u1 - u2) % v2 != 0)
            no_solution();
        else {
            printf("%lu", u1);
        }
    }
}

int main() {
    unsigned int m;
    unsigned long h1, a1, x1, y1;
    unsigned long h2, a2, x2, y2;

    scanf("%d %lu %lu %lu %lu %lu %lu %lu %lu", &m, &h1, &a1, &x1, &y1, &h2, &a2, &x2, &y2);

    long u1, v1;
    u1 = count_step(h1, a1, x1, y1, m);
    if (u1 == -1)
        no_solution();
    v1 = count_step((a1 * x1 + y1) % m, a1, x1, y1, m) + 1;

    long u2, v2;
    u2 = count_step(h2, a2, x2, y2, m);
    if (u2 == -2)
        no_solution();
    v2 = count_step((a2 * x2 + y2) % m, a2, x2, y2, m) + 1;

    // solve u1 + v1 * t1 = u2 + v2 * t2
    if (v1 == 0)
        zero(u1, u2, v2);
    else if (v2 == 0)
        zero(u2, u1, v1);
    else {
        // t1 = (u2 - u1 + v2 * t2) / v1
        if (labs(u1 - u2) % gcd(v1, v2) != 0)
            no_solution();
        else {
            long t2 = 0;
            long s = (u2 - u1 + v2 * t2);
            while (s < 0 || s % v1 != 0) {
                t2++;
                s = (u2 - u1 + v2 * t2);
            }
            printf("%ld", u2 + v2 * t2);
        }
    }

    return 0;
}

#include <stdio.h>
#include <stdlib.h>

typedef struct Node {
    unsigned long value;
    struct Node *left;
    struct Node *right;
} Node;

Node *create_node(unsigned long value) {
    Node *node = malloc(sizeof(Node));
    node->value = value;
    node->left = node->right = NULL;
    return node;
}

/**
 * @return 1 if insert successfully, 0 other wise
 */
int add_to_tree(Node *root, Node *new_node) {
    if (root->value == new_node->value)
        return 0;
    if (root->value > new_node->value) {
        if (root->left == NULL)
            root->left = new_node;
        else
            return add_to_tree(root->left, new_node);
    } else {
        if (root->right == NULL)
            root->right = new_node;
        else
            return add_to_tree(root->right, new_node);
    }
    return 1;
}

long count_step(unsigned long h, unsigned long a, unsigned long x, unsigned long y, unsigned int m) {
    Node *root = create_node(h);
    unsigned long c = 0;
    while (h != a) {
        h = (x * h + y) % m;
        Node *new_node = create_node(h);
        if (!add_to_tree(root, new_node)) {
            return -1;
        }
        c++;
    }
    return c;
}

void no_solution() {
    puts("-1");
    exit(0);
}

long gcd(long a, long b) {
    long t;
    while (b != 0) {
        t = b;
        b = a % b;
        a = t;
    }
    return a;
}

/**
 * v1 == 0
 */
void zero(long u1, long u2, long v2) {
    if (v2 == 0) {
        if (u1 != u2)
            no_solution();
        else
            printf("%lu", u1);
    } else {
        if (u1 < u2 || (u1 - u2) % v2 != 0)
            no_solution();
        else {
            printf("%lu", u1);
        }
    }
}

int main() {
    unsigned int m;
    unsigned long h1, a1, x1, y1;
    unsigned long h2, a2, x2, y2;

    scanf("%d %lu %lu %lu %lu %lu %lu %lu %lu", &m, &h1, &a1, &x1, &y1, &h2, &a2, &x2, &y2);

    long u1, v1;
    u1 = count_step(h1, a1, x1, y1, m);
    if (u1 == -1)
        no_solution();
    v1 = count_step((a1 * x1 + y1) % m, a1, x1, y1, m) + 1;

    long u2, v2;
    u2 = count_step(h2, a2, x2, y2, m);
    if (u2 == -1)
        no_solution();
    v2 = count_step((a2 * x2 + y2) % m, a2, x2, y2, m) + 1;

    // solve u1 + v1 * t1 = u2 + v2 * t2
    if (v1 == 0)
        zero(u1, u2, v2);
    else if (v2 == 0)
        zero(u2, u1, v1);
    else {
        // t1 = (u2 - u1 + v2 * t2) / v1
        if (labs(u1 - u2) % gcd(v1, v2) != 0)
            no_solution();
        else {
            long t2 = 0;
            long s = (u2 - u1 + v2 * t2);
            while (s < 0 || s % v1 != 0) {
                t2++;
                s = (u2 - u1 + v2 * t2);
            }
            printf("%ld", u2 + v2 * t2);
        }
    }

    return 0;
}

#include <stdio.h>
#include <stdlib.h>

typedef struct Node {
    unsigned long value;
    struct Node *left;
    struct Node *right;
} Node;

Node *create_node(unsigned long value) {
    Node *node = malloc(sizeof(Node));
    node->value = value;
    node->left = node->right = NULL;
    return node;
}

/**
 * @return 1 if insert successfully, 0 other wise
 */
int add_to_tree(Node *root, Node *new_node) {
    if (root->value == new_node->value)
        return 0;
    if (root->value > new_node->value) {
        if (root->left == NULL)
            root->left = new_node;
        else
            return add_to_tree(root->left, new_node);
    } else {
        if (root->right == NULL)
            root->right = new_node;
        else
            return add_to_tree(root->right, new_node);
    }
    return 1;
}

long
count_step(unsigned long h, unsigned long a, unsigned long x, unsigned long y, unsigned int m) {
    Node *root = create_node(h);
    unsigned long c = 0;
    while (h != a) {
        h = (x * h + y) % m;
        Node *new_node = create_node(h);
        if (!add_to_tree(root, new_node)) {
            return -1;
        }
        c++;
    }
    return c;
}

void no_solution() {
    puts("-1");
    exit(0);
}

long gcd(long a, long b) {
    long t;
    while (b != 0) {
        t = b;
        b = a % b;
        a = t;
    }
    return a;
}

/**
 * v1 == 0
 */
void zero(long u1, long u2, long v2) {
    if (v2 == 0 && u1 != u2 || (u1 < u2 || (u1 - u2) % v2 != 0))
        no_solution();
    printf("%lu", u1);
    exit(0);
}

int main() {
    unsigned int m;
    unsigned long h1, a1, x1, y1;
    unsigned long h2, a2, x2, y2;

    scanf("%d %lu %lu %lu %lu %lu %lu %lu %lu", &m, &h1, &a1, &x1, &y1, &h2, &a2, &x2, &y2);

    long u1, v1;
    u1 = count_step(h1, a1, x1, y1, m);
    if (u1 == -1)
        no_solution();
    v1 = count_step((a1 * x1 + y1) % m, a1, x1, y1, m) + 1;

    long u2, v2;
    u2 = count_step(h2, a2, x2, y2, m);
    if (u2 == -1)
        no_solution();
    v2 = count_step((a2 * x2 + y2) % m, a2, x2, y2, m) + 1;

    // solve u1 + v1 * t1 = u2 + v2 * t2
    if (v1 == 0)
        zero(u1, u2, v2);

    if (v2 == 0)
        zero(u2, u1, v1);

    // t1 = (u2 - u1 + v2 * t2) / v1
    if (labs(u1 - u2) % gcd(v1, v2) != 0)
        no_solution();
    else {
        long t2 = 0;
        long s = u2 - u1 + v2 * t2;
        while (s < 0 || s % v1 != 0) {
            t2++;
            s = u2 - u1 + v2 * t2;
        }
        printf("%lu", u2 + v2 * t2);
    }

    return 0;
}

#include <stdio.h>
#include <stdlib.h>

typedef struct Node {
    int h1;
    int h2;
    struct Node *left;
    struct Node *right;
} Node;

Node *create_node(int h1, int h2) {
    Node *node = malloc(sizeof(Node));
    node->h1 = h1;
    node->h2 = h2;
    node->left = node->right = NULL;
    return node;
}

int compare(Node *node1, Node *node2) {
    int result = node1->h1 - node2->h1;
    return (result == 0) ? node1->h2 - node2->h2 : result;
}

/**
 * @return 1 if insert successfully, 0 other wise
 */
int add_to_tree(Node *root, Node *new_node) {
    int cmp_result = compare(root, new_node);
    if (cmp_result == 0)
        return 0;
    if (cmp_result > 0) {
        if (root->left == NULL)
            root->left = new_node;
        else
            return add_to_tree(root->left, new_node);
    } else {
        if (root->right == NULL)
            root->right = new_node;
        else
            return add_to_tree(root->right, new_node);
    }
    return 1;
}

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

    scanf("%d %d %d %d %d %d %d %d %d", &m, &h1, &a1, &x1, &y1, &h2, &a2, &x2, &y2);

    Node *root = create_node(h1, h2);
    unsigned long count = 0;

    while (h1 != a1 || h2 != a2) {
        h1 = (x1 * h1 + y1) % m;
        h2 = (x2 * h2 + y2) % m;
        Node *new_node = create_node(h1, h2);
        if (!add_to_tree(root, new_node)) {
            puts("-1");
            return 0;
        }
        count++;
    }

    printf("%lu", count);

    return 0;
}

#include <stdio.h>
#include <stdlib.h>

typedef struct Node {
    unsigned long long value;
    struct Node *left;
    struct Node *right;
} Node;

Node *create_node(unsigned long long value) {
    Node *node = malloc(sizeof(Node));
    node->value = value;
    node->left = node->right = NULL;
    return node;
}

/**
 * @return 1 if insert successfully, 0 other wise
 */
int add_to_tree(Node *root, Node *new_node) {
    if (root->value == new_node->value)
        return 0;
    if (root->value > new_node->value) {
        if (root->left == NULL)
            root->left = new_node;
        else
            return add_to_tree(root->left, new_node);
    } else {
        if (root->right == NULL)
            root->right = new_node;
        else
            return add_to_tree(root->right, new_node);
    }
    return 1;
}

long long
count_step(unsigned long long h, unsigned long long a, unsigned long long x, unsigned long long y, unsigned int m) {
    Node *root = create_node(h);
    unsigned long long c = 0;
    while (h != a) {
        h = (x * h + y) % m;
        Node *new_node = create_node(h);
        if (!add_to_tree(root, new_node)) {
            return -1;
        }
        c++;
    }
    return c;
}

void no_solution() {
    puts("-1");
    exit(0);
}

long gcd(long long a, long long b) {
    long t;
    while (b != 0) {
        t = b;
        b = a % b;
        a = t;
    }
    return a;
}

/**
 * v1 == 0
 */
void zero(long long u1, long long u2, long long v2) {
    if (v2 == 0) {
        if (u1 != u2)
            no_solution();
        else
            printf("%I64d", u1);
    } else {
        if (u1 < u2 || (u1 - u2) % v2 != 0)
            no_solution();
        else {
            printf("%I64d", u1);
        }
    }
}

int main() {
    unsigned int m;
    unsigned long long h1, a1, x1, y1;
    unsigned long long h2, a2, x2, y2;

    scanf("%d %I64 %I64 %I64 %I64 %I64 %I64 %I64 %I64", &m, &h1, &a1, &x1, &y1, &h2, &a2, &x2, &y2);

    long long u1, v1;
    u1 = count_step(h1, a1, x1, y1, m);
    if (u1 == -1)
        no_solution();
    v1 = count_step((a1 * x1 + y1) % m, a1, x1, y1, m) + 1;

    long long u2, v2;
    u2 = count_step(h2, a2, x2, y2, m);
    if (u2 == -2)
        no_solution();
    v2 = count_step((a2 * x2 + y2) % m, a2, x2, y2, m) + 1;

    // solve u1 + v1 * t1 = u2 + v2 * t2
    if (v1 == 0)
        zero(u1, u2, v2);
    else if (v2 == 0)
        zero(u2, u1, v1);
    else {
        // t1 = (u2 - u1 + v2 * t2) / v1
        if (labs(u1 - u2) % gcd(v1, v2) != 0)
            no_solution();
        else {
            long long t2 = 0;
            long long s = (u2 - u1 + v2 * t2);
            while (s < 0 || s % v1 != 0) {
                t2++;
                s = (u2 - u1 + v2 * t2);
            }
            printf("%I64d", u2 + v2 * t2);
        }
    }

    return 0;
}

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void no_solution() {
    puts("-1");
    exit(0);
}

long long
count_step(unsigned long long h, unsigned long long a, unsigned long long x, unsigned long long y, unsigned int m) {
    if (h == a) return 0;

    char mark[m];
    memset(mark, 0, m);

    unsigned long long c = 0;
    while (h != a) {
        if (mark[h])
            no_solution();
        mark[h] = 1;
        h = (x * h + y) % m;
        c++;
    }
    return c;
}

long long gcd(long long a, long long b) {
    long long t;
    while (b != 0) {
        t = b;
        b = a % b;
        a = t;
    }
    return a;
}

/**
 * v1 == 0
 */
void zero(long long u1, long long u2, long long v2) {
    if (v2 == 0 && u1 != u2 || (u1 < u2 || (u1 - u2) % v2 != 0))
        no_solution();
    printf("%I64d", u1);
    exit(0);
}

int main() {
    unsigned int m;
    unsigned long long h1, a1, x1, y1;
    unsigned long long h2, a2, x2, y2;

    scanf("%d %I64d %I64d %I64d %I64d %I64d %I64d %I64d %I64d", &m, &h1, &a1, &x1, &y1, &h2, &a2, &x2, &y2);

    long long u1, v1;
    u1 = count_step(h1, a1, x1, y1, m);
    if (u1 == -1)
        no_solution();
    v1 = count_step((a1 * x1 + y1) % m, a1, x1, y1, m) + 1;

    long long u2, v2;
    u2 = count_step(h2, a2, x2, y2, m);
    if (u2 == -1)
        no_solution();
    v2 = count_step((a2 * x2 + y2) % m, a2, x2, y2, m) + 1;

    // solve u1 + v1 * t1 = u2 + v2 * t2
    if (v1 == 0)
        zero(u1, u2, v2);

    if (v2 == 0)
        zero(u2, u1, v1);

    // t1 = (u2 - u1 + v2 * t2) / v1
    if (labs(u1 - u2) % gcd(v1, v2) != 0)
        no_solution();
    else {
        long long t2 = 0;
        long long s = u2 - u1 + v2 * t2;
        while (s < 0 || s % v1 != 0) {
            t2++;
            s = u2 - u1 + v2 * t2;
        }
        printf("%I64d", u2 + v2 * t2);
    }

    return 0;
}

#include <stdio.h>
#include <string.h>

#define INF 1000000000

int flag[1000000];

int main(void) {
    int tmp;
    long long m, h1, a1, x1, y1, h2, a2, x2, y2;
    int cur, sum, l1, r1, l2, r2, pos1, pos2;

    scanf("%lld %lld %lld %lld %lld %lld %lld %lld %lld", &m, &h1, &a1, &x1, &y1, &h2, &a2, &x2, &y2);

    sum = 0;
    cur = h1;
    pos1 = INF;
    do {
        flag[cur] = 1;
        if (cur == a1)
            pos1 = sum;
        cur = (x1 * cur + y1) % m;
        sum++;
    } while (!flag[cur]);
    tmp = cur;
    l1 = 0;
    cur = h1;
    while (cur != tmp) {
        cur = (x1 * cur + y1) % m;
        l1++;
    }
    r1 = sum - l1;
    if (pos1 == INF) {
        puts("-1");
        return 0;
    }

    memset(flag, 0, sizeof flag);
    sum = 0;
    cur = h2;
    pos2 = INF;
    do {
        flag[cur] = 1;
        if (cur == a2)
            pos2 = sum;
        cur = (x2 * cur + y2) % m;
        sum++;
    } while (!flag[cur]);
    tmp = cur;
    l2 = 0;
    cur = h2;
    while (cur != tmp) {
        cur = (x2 * cur + y2) % m;
        l2++;
    }
    r2 = sum - l2;
    if (pos2 == INF) {
        puts("-1");
        return 0;
    }

    if (pos1 < l1 && pos2 < l2) {
        if (pos1 != pos2) {
            puts("-1");
            return 0;
        }
        printf("%d\n", pos1);
    } else if (pos1 < l1) {
        for (cur = pos2; cur <= pos1; cur += r2)
            if (cur == pos1) {
                printf("%d\n", pos1);
                return 0;
            }
        puts("-1");
    } else if (pos2 < l2) {
        for (cur = pos1; cur <= pos2; cur += r1)
            if (cur == pos2) {
                printf("%d\n", pos2);
                return 0;
            }
        puts("-1");
    } else
        if (pos1 > pos2) {
            for (cur = pos1; cur < pos1 + r1 * r2; cur += r1)
                if ((cur - l2) % r2 + l2 == pos2) {
                    printf("%d\n", cur);
                    return 0;
                }
            puts("-1");
        } else {
            for (cur = pos2; cur < pos2 + r2 * r1; cur += r2)
                if ((cur - l1) % r1 + l1 == pos1) {
                    printf("%d\n", cur);
                    return 0;
                }
            puts("-1");
        }

    return 0;
}

#include <stdio.h>
#include <string.h>
#ifndef maxn
#define maxn (1000000+1000)
#endif

int main(int argc, char const *argv[])
{
    int m,h1,h2,a1,a2,x1,x2,y1,y2;
    int i,cnt1,cnt2,tmp,tmp1,tmp2;
    while(scanf("%d",&m)!=EOF)
    {
        scanf("%d %d %d %d %d %d %d %d",&h1,&a1,&x1,&y1,&h2,&a2,&x2,&y2);

        for(i=0,tmp1=0,tmp=h1;i<m;i++)
        {
            tmp=(tmp*x1+y1)%m;
            if(tmp==a1)
            {
                if(!tmp1) {tmp1=i+1;i=-1;}
                else {cnt1=i+1;break;}
            }
        }
        for(i=0,tmp2=0,tmp=h2;i<m;i++)
        {
            tmp=(tmp*x2+y2)%m;
            if(tmp==a2)
            {
                if(!tmp2) {tmp2=i+1;i=-1;}
                else {cnt2=i+1;break;}
            }
        }

        if (!tmp1||!tmp2)
        {
            printf("-1\n"); continue;
        }

        if(tmp1==tmp2) {printf("%d\n",tmp1);continue;}
        if(cnt1==cnt2) {printf("-1\n");continue;}
        for(i=0,tmp=tmp1;i<cnt2;tmp+=cnt1)
        {
            if (tmp>tmp2&&(tmp-tmp2)%cnt2==0)
            {
                printf("%d\n", tmp);
                break;
            }
        }

    }
    return 0;
}

#include <stdio.h>
#include <string.h>
#ifndef maxn
#define maxn (1000000+1000)
#endif

int main(int argc, char const *argv[])
{
    int i,m,h1,h2,a1,a2,x1,x2,y1,y2;
    __int64 cnt1,cnt2,tmp,tmp1,tmp2;
    while(scanf("%d",&m)!=EOF)
    {
        scanf("%d %d %d %d %d %d %d %d",&h1,&a1,&x1,&y1,&h2,&a2,&x2,&y2);

        for(i=0,tmp1=0,tmp=h1;i<m;i++)
        {
            tmp=(tmp*x1+y1)%m;
            if(tmp==a1)
            {
                if(!tmp1) {tmp1=i+1;i=-1;}
                else {cnt1=i+1;break;}
            }
        }
        for(i=0,tmp2=0,tmp=h2;i<m;i++)
        {
            tmp=(tmp*x2+y2)%m;
            if(tmp==a2)
            {
                if(!tmp2) {tmp2=i+1;i=-1;}
                else {cnt2=i+1;break;}
            }
        }

        if (!tmp1||!tmp2)
        {
            printf("-1\n"); continue;
        }

        if(tmp1==tmp2) {printf("%I64d\n",tmp1);continue;}
        if(cnt1==cnt2) {printf("-1\n");continue;}
        for(i=0,tmp=tmp1;i<cnt2;tmp+=cnt1)
        {
            if (tmp>tmp2&&(tmp-tmp2)%cnt2==0)
            {
                printf("%I64d\n", tmp);
                break;
            }
        }

    }
    return 0;
}

#include <stdio.h>
int main()
{
	int m=1;
	int h1=0,a1=0,x1=0,y1=0;
	int h2=0,a2=0,x2=0,y2=0;
	scanf("%d",&m);
	scanf("%d%d",&h1,&a1);
	scanf("%d%d",&x1,&y1);
	scanf("%d%d",&h2,&a2);
	scanf("%d%d",&x2,&y2);
	int initial1=h1;
	int initial2=h2;
	int count=0;
	int q=0;
	while(++count){
	h1=(x1*h1+y1)%m;
	h2=(x2*h2+y2)%m;
	if(count>m){
		printf("-1\n");
		return 0;}

		 if(h1==a1) {
		q=count;
		break;}
	}
	if(h2==a2){
		printf("%d\n",q );
		return 0;
	}
	int c=0;
	while(++c){
		h1=(x1*h1+y1)%m;
		if(h1==a1) break;
	}
	if(c>m){
		printf("-1\n");
		return 0;
	}
	int i, a=1, b=0;
	for(i=0;i<c;i++){
		a=(a*x2)%m;
		b=(b*x2+y2)%m;
	}
	int count1=0;
	while(count1<=m){
		count1++;
		h2=(a*h2+b)%m;
		if(h2==a2){
			printf("%d\n",q+count1*c );
			return 0;
		}
	}
	printf("-1\n");
	return 0;
}

#include <stdio.h>
int main()
{
	long long int m=1;
	long long int h1=0,a1=0,x1=0,y1=0;
	long long int h2=0,a2=0,x2=0,y2=0;
	scanf("%lld",&m);
	scanf("%lld%lld",&h1,&a1);
	scanf("%lld%lld",&x1,&y1);
	scanf("%lld%lld",&h2,&a2);
	scanf("%lld%lld",&x2,&y2);
	int initial1=h1;
	int initial2=h2;
	int count=0;
	long long int q=0;
	while(++count){
	h1=(x1*h1+y1)%m;
	h2=(x2*h2+y2)%m;
	if(count>m+1){
		printf("-1\n");
		return 0;}

		 if(h1==a1) {
		q=count;
		break;}
	}
	if(h2==a2){
		printf("%lld\n",q );
		return 0;
	}
	int c=0;
	while(++c){
		h1=(x1*h1+y1)%m;
		if(h1==a1) break;
	}
	if(c>m+1){
		printf("-1\n");
		return 0;
	}
	int i; long long a=1, b=0;
	for(i=0;i<c;i++){
		a=(a*x2)%m;
		b=(b*x2+y2)%m;
	}
	printf("%lld %lld\n",a,b );
	int count1=0;
	while(h2!=a2){
		count1++;
		h2=(a*h2+b)%m;
		if(h2==a2){
			printf("%lld\n",q+count1*c );
			return 0;
		}
		printf("%lld\n",h2 );
	}
	if(count1>m+1)printf("-1\n");
	return 0;
}

#include <stdio.h>
int main()
{
	long long int m=1;
	long long int h1=0,a1=0,x1=0,y1=0;
	long long int h2=0,a2=0,x2=0,y2=0;
	scanf("%lld",&m);
	scanf("%lld%lld",&h1,&a1);
	scanf("%lld%lld",&x1,&y1);
	scanf("%lld%lld",&h2,&a2);
	scanf("%lld%lld",&x2,&y2);
	int initial1=h1;
	int initial2=h2;
	int count=0;
	long long int q=0;
	while(++count){
	h1=(x1*h1+y1)%m;
	h2=(x2*h2+y2)%m;
	if(count>m+1){
		printf("a-1\n");
		return 0;}

		 if(h1==a1) {
		q=count;
		break;}
	}
	if(h2==a2){
		printf("%lld\n",q );
		return 0;
	}
	int c=0;
	while(++c){
		h1=(x1*h1+y1)%m;
		if(h1==a1) break;
	}
	if(c>m+1){
		printf("b-1\n");
		return 0;
	}
	int i; long long a=1, b=0;
	for(i=0;i<c;i++){
		a=(a*x2)%m;
		b=(b*x2+y2)%m;
	}
	printf("%lld %lld\n",a,b );
	int count1=0;
	while(h2!=a2){
		count1++;
		h2=(a*h2+b)%m;
		if(h2==a2){
			printf("%lld\n",q+count1*c );
			return 0;
		}
		printf("%lld\n",h2 );
	}
	if(count1>m+1)printf("c-1\n");
	return 0;
}

#include <stdio.h>
int main()
{
	int m=1;
	int h1=0,a1=0,x1=0,y1=0;
	int h2=0,a2=0,x2=0,y2=0;
	scanf("%d",&m);
	scanf("%d%d",&h1,&a1);
	scanf("%d%d",&x1,&y1);
	scanf("%d%d",&h2,&a2);
	scanf("%d%d",&x2,&y2);
	int initial1=h1;
	int initial2=h2;
	int count=0;
	int q=0;
	while(++count){
	h1=(x1*h1+y1)%m;
	h2=(x2*h2+y2)%m;
	//printf("check%d\nm-%d h1-%d a1-%d x1-%d y1-%d h2-%d a2-%d x2-%d y2-%d  ",count,m,h1,a1,x1,y1,h2,a2,x2,y2);
	 if(h1==a1) {
		q=count;
		break;}
	if(count>=m){
		printf("-1\n");
		return 0;
	}
	//printf("check1\n");
	}
	if(h2==a2) {printf("%d\n",q);return 0;}
	count=0;
	while(h1!=initial1){h1=(x1*h1+y1)%m;h2=(x2*h2+y2)%m;count++;}
	if(count>m) {printf("-1\n");return 0;}
	if(h2==a2){printf("%d\n",count+q);return 0;}
	int c=count;
	count=0;
	while(count<m){
		int i=c;
		while(i--){
			h2=(x2*h2+y2)%m;
		}
	count+=c;
	if(h2==a2){printf("%d\n",q+count*c);return 0;}
	}
	if(h2!=a2) printf("-1\n");
	return 0;
}

#include <stdio.h>
int main()
{
	long long int m=1;
	long long int h1=0,a1=0,x1=0,y1=0;
	long long int h2=0,a2=0,x2=0,y2=0;
	scanf("%lld",&m);
	scanf("%lld%lld",&h1,&a1);
	scanf("%lld%lld",&x1,&y1);
	scanf("%lld%lld",&h2,&a2);
	scanf("%lld%lld",&x2,&y2);
	int initial1=h1;
	int initial2=h2;
	int count=0;
	long long int q=0;
	while(++count){
	h1=(x1*h1+y1)%m;
	h2=(x2*h2+y2)%m;
	if(count>m+1){
		printf("-1\n");
		return 0;}

		 if(h1==a1) {
		q=count;
		break;}
	}
	if(h2==a2){
		printf("%lld\n",q );
		return 0;
	}
	int c=0;
	while(++c){
		h1=(x1*h1+y1)%m;
		if(h1==a1) break;
	}
	if(c>m+1){
		printf("-1\n");
		return 0;
	}
	int i; long long a=1, b=0;
	for(i=0;i<c;i++){
		a=(a*x2)%m;
		b=(b*x2+y2)%m;
	}
	//printf("%lld %lld\n",a,b );
	int count1=0;
	while(h2!=a2){
		count1++;
		h2=(a*h2+b)%m;
		if(h2==a2){
			printf("%lld\n",q+count1*c );
			return 0;
		}
		if(count1>m+1){printf("-1\n");return 0;}
		//printf("%lld\n",h2 );
	}

	return 0;
}

#include<stdio.h>
#define MAXN 1000002

long long number1[MAXN],number2[MAXN];

void gcd(long long a,long long b,long long * d,long long * x,long long * y)
{
    if(!b){*d=a;*x=1;*y=0;}
    else {gcd(b,a%b,d,y,x);(*y)-=(*x)*(a/b);}
}

int main()
{
    long long m,h1,a1,x1,y1,h2,a2,x2,y2;
    long long x,y,d,i,len1,len2,b1,b2;
    long long aa,bb,flag1,flag2;

    scanf("%I64d%I64d%I64d%I64d%I64d%I64d%I64d%I64d%I64d",&m,&h1,&a1,&x1,&y1,&h2,&a2,&x2,&y2);
    number1[h1]=1;
    len1=1;
    flag1=1;
    for(x=h1,i=0;i<=m;i++)
    {
        x=(x1*x+y1)%m;
        if(!number1[x])number1[x]=++len1;
        else
        {
            len1=len1+1-number1[x];
            break;
        }
    }
    if(number1[a1]>0)b1=number1[a1]-number1[h1];
    else flag1=0;

    flag2=1;
    number2[h2]=1;
    len2=1;
    for(x=h2,i=0;i<=m;i++)
    {
        x=(x2*x+y2)%m;
        if(!number2[x])number2[x]=++len2;
        else
        {
            len2=len2+1-number2[x];
            break;
        }
    }
    if(number2[a2]>0)b2=number2[a2]-number2[h2];
    else flag2=0;

    if(flag1==0|flag2==0)
    {
        printf("-1\n");
        return 0;
    }
    gcd(len1,len2,&d,&x,&y);
    if((b2-b1)%d!=0)printf("-1\n");
    else
    {
        if((b2-b1)!=d)
        {
            x*=(b2-b1)/d;
            y*=(b2-b1)/d;
        }
        aa=len1/d;
        bb=len2/d;
        while(x<0||y>0)
        {
            x+=bb;
            y-=aa;
        }
        while(x-bb>=0&&y+aa<=0)
        {
            x-=bb;
            y+=aa;
        }
        printf("%I64d\n",b1+x*len1);
    }

    return 0;
}

#include<stdio.h>
#define MAXN 1000002

long long number1[MAXN],number2[MAXN];

void gcd(long long a,long long b,long long * d,long long * x,long long * y)
{
    if(!b){*d=a;*x=1;*y=0;}
    else {gcd(b,a%b,d,y,x);(*y)-=(*x)*(a/b);}
}

int main()
{
    long long m,h1,a1,x1,y1,h2,a2,x2,y2;
    long long x,y,d,i,len1,len2,b1,b2;
    long long aa,bb,flag1,flag2,flag11,flag22;

    scanf("%I64d%I64d%I64d%I64d%I64d%I64d%I64d%I64d%I64d",&m,&h1,&a1,&x1,&y1,&h2,&a2,&x2,&y2);
    number1[h1]=1;
    len1=1;
    flag1=1;
    flag11=flag22=0;
    for(x=h1,i=0;i<=m;i++)
    {
        x=(x1*x+y1)%m;
        if(!number1[x])number1[x]=++len1;
        else
        {
            len1=len1+1-number1[x];
            if(number1[a1]>0&&number1[a1]<number1[x])flag11=1;
            break;
        }
    }
    if(number1[a1]>0)b1=number1[a1]-number1[h1];
    else flag1=0;

    flag2=1;
    number2[h2]=1;
    len2=1;
    for(x=h2,i=0;i<=m;i++)
    {
        x=(x2*x+y2)%m;
        if(!number2[x])number2[x]=++len2;
        else
        {
            len2=len2+1-number2[x];
            if(number2[a2]>0&&number2[a2]<number2[x])flag22=1;
            break;
        }
    }
    if(number2[a2]>0)b2=number2[a2]-number2[h2];
    else flag2=0;

    if(flag1==0|flag2==0)
    {
        printf("-1\n");
        return 0;
    }
    if(flag11&&flag22)
    {
        if(b1==b2)printf("%I64d\n",b1);
        else printf("-1\n");
        return 0;
    }
    else if(flag11)
    {
        if(b1>b2&&(b1-b2)%len2==0)printf("%I64d\n",b1);
        else printf("-1\n");
        return 0;
    }
    else if(flag22)
    {
        if(b2>b1&&(b2-b1)%len1==0)printf("%I64d\n",b2);
        else printf("-1\n");
        return 0;
    }
    gcd(len1,len2,&d,&x,&y);
    if((b2-b1)%d!=0)printf("-1\n");
    else
    {
        if((b2-b1)!=d)
        {
            x*=(b2-b1)/d;
            y*=(b2-b1)/d;
        }
        aa=len1/d;
        bb=len2/d;
        while(x<0||y>0)
        {
            x+=bb;
            y-=aa;
        }
        while(x-bb>=0&&y+aa<=0)
        {
            x-=bb;
            y+=aa;
        }
        printf("%I64d\n",b1+x*len1);
    }

    return 0;
}

#include<stdio.h>
#define MAXN 1000002

long long number1[MAXN],number2[MAXN];

void gcd(long long a,long long b,long long * d,long long * x,long long * y)
{
    if(!b){*d=a;*x=1;*y=0;}
    else {gcd(b,a%b,d,y,x);(*y)-=(*x)*(a/b);}
}

int main()
{
    long long m,h1,a1,x1,y1,h2,a2,x2,y2;
    long long x,y,d,i,len1,len2,b1,b2;
    long long aa,bb,flag1,flag2,flag11,flag22;

    scanf("%I64d%I64d%I64d%I64d%I64d%I64d%I64d%I64d%I64d",&m,&h1,&a1,&x1,&y1,&h2,&a2,&x2,&y2);
    number1[h1]=1;
    len1=1;
    flag1=1;
    for(x=h1,i=0;i<=m;i++)
    {
        x=(x1*x+y1)%m;
        if(!number1[x])number1[x]=++len1;
        else
        {
            len1=len1+1-number1[x];
            if(number1[a1]<number1[x])flag11=1;
            break;
        }
    }
    if(number1[a1]>0)b1=number1[a1]-number1[h1];
    else flag1=0;

    flag2=1;
    number2[h2]=1;
    len2=1;
    for(x=h2,i=0;i<=m;i++)
    {
        x=(x2*x+y2)%m;
        if(!number2[x])number2[x]=++len2;
        else
        {
            len2=len2+1-number2[x];
            if(number2[a2]<number2[x])flag22=1;
            break;
        }
    }
    if(number2[a2]>0)b2=number2[a2]-number2[h2];
    else flag2=0;

    if(flag1==0|flag2==0)
    {
        printf("-1\n");
        return 0;
    }
    if(flag11&&flag22)
    {
        if(b1==b2)printf("%I64d\n",b1);
        else printf("-1\n");
        return 0;
    }
    else if(flag11)
    {
        if(b1>b2&&(b1-b2)%len2==0)printf("%I64d\n",b1);
        else printf("-1\n");
        return 0;
    }
    else if(flag22)
    {
        if(b2>b1&&(b2-b1)%len1==0)printf("%I64d\n",b2);
        else printf("-1\n");
        return 0;
    }
    gcd(len1,len2,&d,&x,&y);
    printf("%I64d %I64d\n",x,y);
    if((b2-b1)%d!=0)printf("-1\n");
    else
    {
        if((b2-b1)!=d)
        {
            x*=(b2-b1)/d;
            y*=(b2-b1)/d;
        }
        aa=len1/d;
        bb=len2/d;
        while(x<0||y>0)
        {
            x+=bb;
            y-=aa;
        }
        while(x-bb>=0&&y+aa<=0)
        {
            x-=bb;
            y+=aa;
        }
        printf("%I64d\n",b1+x*len1);
    }

    return 0;
}

#include<stdio.h>
#define MAXN 1000002

long long number1[MAXN],number2[MAXN];

void gcd(long long a,long long b,long long * d,long long * x,long long * y)
{
    if(!b){*d=a;*x=1;*y=0;}
    else {gcd(b,a%b,d,y,x);(*y)-=(*x)*(a/b);}
}

int main()
{
    long long m,h1,a1,x1,y1,h2,a2,x2,y2;
    long long x,y,d,i,len1,len2,b1,b2;
    long long aa,bb,flag1,flag2,T1,T2,flag=0;

    scanf("%I64d%I64d%I64d%I64d%I64d%I64d%I64d%I64d%I64d",&m,&h1,&a1,&x1,&y1,&h2,&a2,&x2,&y2);
    number1[h1]=1;
    len1=1;
    flag1=1;
    for(x=h1,i=0;i<=m;i++)
    {
        x=(x1*x+y1)%m;
        if(!number1[x])number1[x]=++len1;
        else
        {
            len1=len1+1-number1[x];
            if(number1[a1]<number1[x])flag=0;
            T1=number1[x];
            break;
        }
    }
    if(number1[a1]>0)b1=number1[a1]-number1[h1];
    else flag1=0;

    flag2=1;
    number2[h2]=1;
    len2=1;
    for(x=h2,i=0;i<=m;i++)
    {
        x=(x2*x+y2)%m;
        if(!number2[x])number2[x]=++len2;
        else
        {
            len2=len2+1-number2[x];
            if(number2[a2]<number2[x])flag=0;
            T2=number2[x];
            break;
        }
    }
    if(number2[a2]>0)b2=number2[a2]-number2[h2];
    else flag2=0;

    if(flag1==0|flag2==0)
    {
        printf("-1\n");
        return 0;
    }
    gcd(len1,len2,&d,&x,&y);
    if((b2-b1)%d!=0)printf("-1\n");
    else
    {
        if((b2-b1)!=d)
        {
            x*=(b2-b1)/d;
            y*=(b2-b1)/d;
        }
        aa=len1/d;
        bb=len2/d;
        while(x<0||y>0)
        {
            x+=bb;
            y-=aa;
        }
        while(x-bb>=0&&y+aa<=0)
        {
            x-=bb;
            y+=aa;
        }
        if(flag==0&&(b1+x*len1>b2))printf("-1\n");
        else printf("%I64d\n",b1+x*len1);
    }

    return 0;
}

#include<stdio.h>
long long int p,h1,a1,x1,y,h2,a2,x2,y2;
long long int hone(long long int h)
{
	return (x1*h+y)%p;
}
long long int htwo(long long int h)
{
	return (x2*h+y2)%p;
}
int main()
{
	scanf("%lld %lld %lld %lld %lld %lld %lld %lld %lld",&p,&a1,&h1,&x1,&y,&a2,&h2,&x2,&y2);
	long long int i,hi=a1,c=0,d=1,x=0,h=hone(h1),o=0;
	while(hi!=h1 && c<=p)
	{
		hi=hone(hi);
		c++;
	}
	if(c>p)
		printf("-1\n");
	else
	{
		hi=a2;
		for(i=0;i<c;i++)
			hi=htwo(hi);
		if(hi==h2)
			printf("%lld\n",c);
		else
		{
			while(h!=h1 && d<=p)
			{
				h=hone(h);
				d++;
			}
			if(d>p)
				printf("-1\n");
			else
			{
				while(hi!=h2 && o<=p)
				{
					hi=htwo(hi);
					o++;
				}
				if(o>p)
					printf("-1\n");
				else
					printf("%lld\n",c+o*d);
			}
		}
	}
	return 0;
}

#include<stdio.h>
int p,h1,a1,x1,y,h2,a2,x2,y2;
int hone(int h)
{
	return (x1*h+y)%p;
}
int htwo(int h)
{
	return (x2*h+y2)%p;
}
int main()
{
	scanf("%d %d %d %d %d %d %d %d %d",&p,&a1,&h1,&x1,&y,&a2,&h2,&x2,&y2);
	int i,hi=a1,c=0,d=1,x=0,h=hone(h1);
	while(hi!=h1 && c<=p)
	{
		hi=hone(hi);
		c++;
	}
	if(c>p)
		printf("-1\n");
	else
	{
		hi=a2;
		for(i=0;i<c;i++)
			hi=htwo(hi);
		if(hi==h2)
			printf("%d\n",c);
		else
		{
			while(h!=h1 && d<=p)
			{
				h=hone(h);
				d++;
			}
			if(d>p)
				printf("-1\n");
			else
			{
				while(c+x*d<=p && hi!=h2)
				{
					for(i=0;i<d;i++)
						hi=htwo(hi);
					x++;
				}
				if(c+x*d>p)
					printf("-1\n");
				else
					printf("%d\n",c+x*d);
			}
		}
	}
	return 0;
}

#include<stdio.h>
long long int p,h1,a1,x1,y,h2,a2,x2,y2;
long long int hone(long long int h)
{
	return (x1*h+y)%p;
}
long long int htwo(long long int h)
{
	return (x2*h+y2)%p;
}
int main()
{
	scanf("%lld %lld %lld %lld %lld %lld %lld %lld %lld",&p,&a1,&h1,&x1,&y,&a2,&h2,&x2,&y2);
	long long int i,hi=a1,c=0,d=1,h=hone(h1),o=0;
	while(hi!=h1 && c<=p)
	{
		hi=hone(hi);
		c++;
	}
	while(h!=h1 && d<=p)
	{
		h=hone(h);
		d++;
	}
	if(c>p)
		printf("-1\n");
	else
	{
		hi=a2;
		for(i=0;i<c;i++)
			hi=htwo(hi);
		if(hi==h2)
			printf("%lld\n",c);
		else if(d>p)
			printf("-1\n");
		else
		{
			while(hi!=h2 && o<=p)
			{
				hi=htwo(hi);
				o++;
			}
			if(o>p)
				printf("-1\n");
			else if(o==d)
				printf("%lld\n",c+o);
			else{
				hi=htwo(hi);
				int oo=1;
				while(hi!=h2 && oo<=p)
				{
					hi=htwo(h2);
					oo++;
				}
				if(oo>p)
					printf("-1\n");
				else
					printf("%lld\n",c+o*d);
			}
		}
	}
	return 0;
}

#include<stdio.h>
long long int p,h1,a1,x1,y,h2,a2,x2,y2;
long long int hone(long long int h)
{
	return (x1*h+y)%p;
}
long long int htwo(long long int h)
{
	return (x2*h+y2)%p;
}
int main()
{
	scanf("%lld %lld %lld %lld %lld %lld %lld %lld %lld",&p,&a1,&h1,&x1,&y,&a2,&h2,&x2,&y2);
	long long int i,hi=a1,c=0,d=1,h=hone(h1),o=0;
	while(hi!=h1 && c<=p)
	{
		hi=hone(hi);
		c++;
	}
	while(h!=h1 && d<=p)
	{
		h=hone(h);
		d++;
	}
	if(c>p)
		printf("-1\n");
	else
	{
		hi=a2;
		for(i=0;i<c;i++)
			hi=htwo(hi);
		//printf("%lld\n",hi);
		if(hi==h2)
			printf("%lld\n",c);
		else if(d>p)
			printf("-1\n");
		else
		{
			while(hi!=h2 && o<=p)
			{
				hi=htwo(hi);
				o++;
			}
			if(o>p)
				printf("-1\n");
			else if(o==d)
				printf("%lld\n",c+o);
			else
				printf("%lld\n",c+o*d);
		}
	}
//	printf("%lld %lld %lld\n",c,d,o);
	return 0;
}