#include <cstdio>
#include <cmath>
#include <cstring>
#include <string>
#include <algorithm>
#include <queue>
#include <map>
#include <set>
#include <vector>
#include <iostream>
using namespace std;

const double pi=acos(-1.0);
double eps=0.000001;
int main()
{
int h1,h2,x1,x2,y1,y2,a1,a2 ;
int m;
cin>>m;
cin>>h1>>a1;
cin>>x1>>y1;
cin>>h2>>a2;
cin>>x2>>y2;

int tmp1=h1;
int tmp2=h2;
int cun=0;
while(cun<=1e6)
{
	cun++;
	tmp1=(tmp1*x1+y1)%m;
	tmp2=(tmp2*x2+y2)%m;
	if (tmp1==a1&&tmp2==a2) break;
}
if (tmp1==a1&&tmp2==a2)
printf("%d\n",cun);
else
printf("-1\n");

	return 0;

}


#include <cstdio>
#include <cmath>
#include <cstring>
#include <string>
#include <algorithm>
#include <queue>
#include <map>
#include <set>
#include <vector>
#include <iostream>
using namespace std;

const double pi=acos(-1.0);
double eps=0.000001;
__int64 vis1[1000006];
__int64 vis2[1000006];
__int64 when1[1000006];
__int64 when2[1000006];
__int64 gcdc(__int64 a,__int64 b){
    if(b==0)
        return a;
	return gcdc(b,a%b);
}
__int64 t,p,c;
void exgcd(__int64 a,__int64 b,__int64&d,__int64&x,__int64&y)//ax+by=gcd(a,b)
{
    if(!b)
    {
        d=a;
        x=1;
        y=0;
    }
    else
    {
        exgcd(b,a%b,d,y,x);
        y-=x*(a/b);
    }
}

int main()
{

	__int64 h1,h2,x1,x2,y1,y2,a1,a2 ;
	__int64 m;
	scanf("%I64d",&m);
	scanf("%I64d%I64d",&h1,&a1);
	scanf("%I64d%I64d",&x1,&y1);
	scanf("%I64d%I64d",&h2,&a2);
	scanf("%I64d%I64d",&x2,&y2);
	__int64 tmp1=h1;
	__int64 tmp2=h2;
	__int64 cun=0;
	int flag=0;
	while(cun<=2*m)
	{
		cun++;
		tmp1=(tmp1*x1+y1)%m;
		if (tmp1==a1&&vis1[a1]) {flag=1; break;}
		vis1[tmp1]=1;
		when1[tmp1]=cun;
	}
	__int64 len1;

	if (flag)len1=cun-when1[a1];
	__int64 t1=when1[a1];
	if (t1 ==0)  	{printf("-1\n");return 0;}
	cun=0;
	flag=0;
	while(cun<=2*m)
	{
		cun++;
		tmp2=(tmp2*x2+y2)%m;
		if (tmp2==a2&&vis2[a2])
		{flag=1;	break;}
		vis2[tmp2]=1;
		when2[tmp2]=cun;
	}
	__int64 len2=0;
	if (flag)
	len2=cun-when2[a2];

	__int64 t2=when2[a2];

	if (t2==0)  	{printf("-1\n");return 0;}

	for (__int64 i=1;i<=2*m;i++)
	{

		if (t1==t2)
		{printf("%I64d\n",t1);return 0;}
		else
			if (t1>t2)
				t2+=len2;
			else
				t1+=len1;
	}
	printf("-1\n");

	return 0;

}


#include <cstdio>
#include <cmath>
#include <cstring>
#include <string>
#include <algorithm>
#include <queue>
#include <map>
#include <set>
#include <vector>
#include <iostream>
using namespace std;

const double pi=acos(-1.0);
double eps=0.000001;
__int64 vis1[1000006];
__int64 vis2[1000006];
__int64 when1[1000006];
__int64 when2[1000006];
__int64 gcdc(__int64 a,__int64 b){
    if(b==0)
        return a;
	return gcdc(b,a%b);
}
__int64 t,p,c;
void exgcd(__int64 a,__int64 b,__int64&d,__int64&x,__int64&y)//ax+by=gcd(a,b)
{
    if(!b)
    {
        d=a;
        x=1;
        y=0;
    }
    else
    {
        exgcd(b,a%b,d,y,x);
        y-=x*(a/b);
    }
}

int main()
{

	__int64 h1,h2,x1,x2,y1,y2,a1,a2 ;
	__int64 m;
	scanf("%I64d",&m);
	scanf("%I64d%I64d",&h1,&a1);
	scanf("%I64d%I64d",&x1,&y1);
	scanf("%I64d%I64d",&h2,&a2);
	scanf("%I64d%I64d",&x2,&y2);
	__int64 tmp1=h1;
	__int64 tmp2=h2;
	__int64 cun=0;
	int flag=0;
	while(cun<=2*m)
	{
		cun++;
		tmp1=(tmp1*x1+y1)%m;
		if (tmp1==a1&&vis1[a1]) {flag=1; break;}
		vis1[tmp1]=1;
		when1[tmp1]=cun;
	}
	__int64 len1=cun-when1[a1];
	__int64 t1=when1[a1];
	if (!flag){printf("-1\n");return 0;}
	cun=0;
	flag=0;
	while(cun<=2*m)
	{
		cun++;
		tmp2=(tmp2*x2+y2)%m;
		if (tmp2==a2&&vis2[a2])
		{flag=1;	break;}
		vis2[tmp2]=1;
		when2[tmp2]=cun;
	}
	__int64 len2=cun-when2[a2];

	__int64 t2=when2[a2];
	if (flag==0){printf("-1\n");return 0;}

	for (__int64 i=1;i<=2*m;i++)
	{

		if (t1==t2)
		{printf("%I64d\n",t1);return 0;}
		else
			if (t1>t2)
				t2+=len2;
			else
				t1+=len1;
	}
	printf("-1\n");

	return 0;

}


#include <cstdio>
#include <cmath>
#include <cstring>
#include <string>
#include <algorithm>
#include <queue>
#include <map>
#include <set>
#include <vector>
#include <iostream>
using namespace std;

const double pi=acos(-1.0);
double eps=0.000001;
__int64 vis1[1000006];
__int64 vis2[1000006];
__int64 when1[1000006];
__int64 when2[1000006];
__int64 gcdc(__int64 a,__int64 b){
    if(b==0)
        return a;
	return gcdc(b,a%b);
}
__int64 t,p,c;
void exgcd(__int64 a,__int64 b,__int64&d,__int64&x,__int64&y)//ax+by=gcd(a,b)
{
    if(!b)
    {
        d=a;
        x=1;
        y=0;
    }
    else
    {
        exgcd(b,a%b,d,y,x);
        y-=x*(a/b);
    }
}

int main()
{

	__int64 h1,h2,x1,x2,y1,y2,a1,a2 ;
	__int64 m;
	scanf("%I64d",&m);
	scanf("%I64d%I64d",&h1,&a1);
	scanf("%I64d%I64d",&x1,&y1);
	scanf("%I64d%I64d",&h2,&a2);
	scanf("%I64d%I64d",&x2,&y2);
	__int64 tmp1=h1;
	__int64 tmp2=h2;
	__int64 cun=0;
	int flag=0;
	while(cun<=2*m)
	{
		cun++;
		tmp1=(tmp1*x1+y1)%m;
		if (tmp1==a1&&vis1[a1]) {flag=1; break;}
		vis1[tmp1]=1;
		when1[tmp1]=cun;
	}
	__int64 len1;

	if (flag)len1=cun-when1[a1];
	__int64 t1=when1[a1];
	cun=0;
	flag=0;
	while(cun<=2*m)
	{
		cun++;
		tmp2=(tmp2*x2+y2)%m;
		if (tmp2==a2&&vis2[a2])
		{flag=1;	break;}
		vis2[tmp2]=1;
		when2[tmp2]=cun;
	}
	__int64 len2=0;
	if (flag)
	len2=cun-when2[a2];

	__int64 t2=when2[a2];

	for (__int64 i=1;i<=2*m;i++)
	{

		if (t1==t2)
		{printf("%I64d\n",t1);return 0;}
		else
			if (t1>t2)
				t2+=len2;
			else
				t1+=len1;
	}
	printf("-1\n");

	return 0;

}


#include <cstdio>
#include <cmath>
#include <cstring>
#include <string>
#include <algorithm>
#include <queue>
#include <map>
#include <set>
#include <vector>
#include <iostream>
using namespace std;

const double pi=acos(-1.0);
double eps=0.000001;
int vis1[1000006];
int vis2[1000006];
int when1[1000006];
int when2[1000006];
  __int64 gcdc(__int64 a,__int64 b){
    if(b==0)
        return a;
        return gcdc(b,a%b);
}
__int64 t,p,c;
void extend_euild(__int64 a, __int64 b)
{
	if (b==0)
	{
		t=1;
		p=0;

	}
	else
	{
		extend_euild(b,a%b);
		__int64 tmp=t;
		t=p;
		p=tmp-a/b*p;
	}
}
int main()
{
int h1,h2,x1,x2,y1,y2,a1,a2 ;
int m;
cin>>m;
cin>>h1>>a1;
cin>>x1>>y1;
cin>>h2>>a2;
cin>>x2>>y2;

int tmp1=h1;
int tmp2=h2;
int cun=1;
while(cun<=2*m)
{
	cun++;
	tmp1=(tmp1*x1+y1)%m;
	if (tmp1==h1) break;
	vis1[tmp1]=1;
	when1[tmp1]=cun-1;
}
int len1=cun-1;
 int t1=when1[a1];
if (t1==0){printf("-1\n");return 0;}
cun=1;
while(cun<=2*m)
{
	cun++;
	tmp2=(tmp2*x2+y2)%m;
	if (tmp2==h2) break;
	vis2[tmp2]=1;
	when2[tmp2]=cun-1;
}
int len2=cun-1;

int t2=when2[a2];
if (t2==0){printf("-1\n");return 0;}

int every=len1-len2;

if (t1==t2) {printf("%d\n",t1);return 0;}

//dis=t2-t1+(h2-h1)=dis=a*every+b*m;
//a*x+
	__int64 a=len1;
	__int64 b=len2;
	__int64 d=(t2-t1);
 	__int64 gcd=gcdc(a,b);

	if (d%gcd)
	{
		printf("-1\n");
		return 0;
	}

        extend_euild( a, b );
      //  t=p;
        t*=  d/gcd ;
        t%=b;
		if (t<0 ) t+=b;
        printf( "%I64d\n", t*len1+t1 );

	return 0;

}


#include <cstdio>
#include <cmath>
#include <cstring>
#include <string>
#include <algorithm>
#include <queue>
#include <map>
#include <set>
#include <vector>
#include <iostream>
using namespace std;

const double pi=acos(-1.0);
double eps=0.000001;
int vis1[1000006];
int vis2[1000006];
int when1[1000006];
int when2[1000006];
  __int64 gcdc(__int64 a,__int64 b){
    if(b==0)
        return a;
        return gcdc(b,a%b);
}
__int64 t,p,c;
void exgcd(__int64 a,__int64 b,__int64&d,__int64&x,__int64&y)//ax+by=gcd(a,b)
{
    if(!b)
    {
        d=a;
        x=1;
        y=0;
    }
    else
    {
        exgcd(b,a%b,d,y,x);
        y-=x*(a/b);
    }
}

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

int tmp1=h1;
int tmp2=h2;
int cun=0;
while(cun<=2*m)
{
	cun++;
	tmp1=(tmp1*x1+y1)%m;
	if (tmp1==a1&&vis1[a1]) break;
	 vis1[tmp1]=1;
	when1[tmp1]=cun;
}
int len1=cun-when1[a1];
 int t1=when1[a1];
if (t1==0){printf("-1\n");return 0;}
cun=0;
while(cun<=2*m)
{
	cun++;
	tmp2=(tmp2*x2+y2)%m;
	if (tmp2==a2&&vis2[a2]) break;
	vis2[tmp2]=1;
 	when2[tmp2]=cun;
}
int len2=cun-when2[a2];

int t2=when2[a2];
if (t2==0){printf("-1\n");return 0;}

int every=len1-len2;

if (t1==t2) {printf("%d\n",t1);return 0;}

//dis=t2-t1+(h2-h1)=dis=a*every+b*m;
//a*x+
	__int64 a=len1;
	__int64 b= len2;
	__int64 d=(t2-t1);
	__int64 x,y;
	__int64 gcd;
	exgcd(a,b,gcd,x,y);
	if (d%gcd)
	{
		printf("-1\n");
		return 0;
	}
//	d/=gcd;

//         extend_euild( a, b );
      //  t=p;
       x*=  d/gcd ;
        x%=b;
		if (x<0 ) x+=b;
		if (!x) x+=b;
        printf( "%I64d\n", x*len1+t1 );

	return 0;

}


#include <cstdio>
#include <cmath>
#include <cstring>
#include <string>
#include <algorithm>
#include <queue>
#include <map>
#include <set>
#include <vector>
#include <iostream>
using namespace std;

const double pi=acos(-1.0);
double eps=0.000001;
int main()
{
int h1,h2,x1,x2,y1,y2,a1,a2 ;
int m;
cin>>m;
cin>>h1>>a1;
cin>>x1>>y1;
cin>>h2>>a2;
cin>>x2>>y2;

int tmp1=h1;
int tmp2=h2;
int cun=0;
while(cun<=1e8)
{
	cun++;
	tmp1=(tmp1*x1+y1)%m;
	tmp2=(tmp2*x2+y2)%m;
	if (tmp1==a1&&tmp2==a2) break;
}
if (tmp1==a1&&tmp2==a2)
printf("%d\n",cun);
else
printf("-1\n");

	return 0;

}


#include <cstdio>
#include <cmath>
#include <cstring>
#include <string>
#include <algorithm>
#include <queue>
#include <map>
#include <set>
#include <vector>
#include <iostream>
using namespace std;

const double pi=acos(-1.0);
double eps=0.000001;
int vis1[1000006];
int vis2[1000006];
int when1[1000006];
int when2[1000006];
  __int64 gcdc(__int64 a,__int64 b){
    if(b==0)
        return a;
        return gcdc(b,a%b);
}
__int64 t,p,c;
void extend_euild(__int64 a, __int64 b)
{
	if (b==0)
	{
		t=1;
		p=0;

	}
	else
	{
		extend_euild(b,a%b);
		__int64 tmp=t;
		t=p;
		p=tmp-a/b*p;
	}
}
int main()
{
int h1,h2,x1,x2,y1,y2,a1,a2 ;
int m;
cin>>m;
cin>>h1>>a1;
cin>>x1>>y1;
cin>>h2>>a2;
cin>>x2>>y2;

int tmp1=h1;
int tmp2=h2;
int cun=0;
while(cun<=2*m)
{
	cun++;
	tmp1=(tmp1*x1+y1)%m;
	if (tmp1==a1&&vis1[a1]) break;
	 vis1[tmp1]=1;
	when1[tmp1]=cun;
}
int len1=cun-when1[a1];
 int t1=when1[a1];
if (t1==0){printf("-1\n");return 0;}
cun=0;
while(cun<=2*m)
{
	cun++;
	tmp2=(tmp2*x2+y2)%m;
	if (tmp2==a2&&vis2[a2]) break;
	vis2[tmp2]=1;
 	when2[tmp2]=cun;
}
int len2=cun-when2[a2];

int t2=when2[a2];
if (t2==0){printf("-1\n");return 0;}

int every=len1-len2;

if (t1==t2) {printf("%d\n",t1);return 0;}

//dis=t2-t1+(h2-h1)=dis=a*every+b*m;
//a*x+
	__int64 a=len1;
	__int64 b=len2;
	__int64 d=(t2-t1);
 	__int64 gcd=gcdc(a,b);

	if (d%gcd)
	{
		printf("-1\n");
		return 0;
	}
	 a/=gcd;
	b/=gcd;
	d/=gcd;

        extend_euild( a, b );
      //  t=p;
       t*=  d/gcd ;
        t%=b;
		if (t<0 ) t+=b;
        printf( "%I64d\n", t*len1+t1 );

	return 0;

}


#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <windows.h>
using namespace std;

int findTick(int m, int h, int  x, int y, int a)
{
	int tick = 0;
	while (tick < m)
	{
		tick++;
		h = (x*h + y) % m;
		if (h == a)
			return tick;
	}
	return -1;
}

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

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

	m = 29;
	h1 = 4;
	a1 = 0;
	x1 = 1;
	y1 = 1;
	h2 = 25;
	a2 = 20;
	x2 = 16;
	y2 = 0;

	int s1 = findTick(m, h1, x1, y1, a1);
	if (s1 == -1)
	{
		printf("-1");
		return 0;
	}
	int s2 = findTick(m, h2, x2, y2, a2);
	if (s2 == -1)
	{
		printf("-1");
		return 0;
	}
	if (s1 == s2)
	{
		printf("%d", s1);
		return 0;
	}
	int c1 = findTick(m, a1, x1, y1, a1);
	int c2 = findTick(m, a2, x2, y2, a2);

	if ((s1 != s2) && ((c1 == -1) || (c2 == -1) || (c1==c2)))
	{
		printf("-1");
		return 0;
	}

	if (c1 == c2)
	{
		printf("-1");
		return 0;
	}

	//printf("%d %d %d %d\n", s1, s2, c1, c2);

	int t = (s1 - s2) / (c2 - c1);
	if (s1 + c1*t == s2 + c2*t)
		printf("%d", s1 + c1*t);
	else
		printf("-1");

	return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <windows.h>
#define lli long long int
using namespace std;

int findTick(lli m, lli h, lli  x, lli y, lli a)
{
	int tick = 0;
	while (tick < m)
	{
		tick++;
		h = (x*h + y) % m;
		if (h == a)
			return tick;
	}
	return -1;
}

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

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

	int s1 = findTick(m, h1, x1, y1, a1);
	if (s1 == -1)
	{
		printf("-1");
		return 0;
	}
	int c1 = findTick(m, a1, x1, y1, a1);

	lli h3 = h2;
	lli x3 = x2;
	lli y3 = y2;

	for (int i = 0; i < s1; i++)
		h3 = (x2*h3 + y2) % m;

	if (h3 == a2)
	{
		printf("%d", s1);
		return 0;
	}

	for (int i = 1; i < c1; i++)
	{
		x3 = (x3*x2)%m;
		y3 = (y3*x2 + y2)%m;
	}
	int c3 = findTick(m, h3, x3, y3, a2);

	//printf("%d %d %d %d %d\n", s1, s2, c1, c2, c3);

	if (c3!=-1)
		printf("%d", s1+c1*c3);
	else
		printf("-1");

	return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <windows.h>
using namespace std;

int findTick(int m, int h, int  x, int y, int a)
{
	int tick = 0;
	while (tick < m)
	{
		tick++;
		h = (x*h + y) % m;
		if (h == a)
			return tick;
	}
	return -1;
}

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

	//scanf("%d %d %d %d %d %d %d %d %d", &m, &h1, &a1, &x1, &y1, &h2, &a2, &x2, &y2);
	m = 1023;
	h1 = 1;
	a1 = 2;
	x1 = 1;
	y1 = 2;
	h2 = 1;
	a2 = 2;
	x2 = 1;
	y2 = 2;

	int s1 = findTick(m, h1, x1, y1, a1);
	if (s1 == -1)
	{
		printf("-1");
		return 0;
	}
	int s2 = findTick(m, h2, x2, y2, a2);
	if (s2 == -1)
	{
		printf("-1");
		return 0;
	}
	int c1 = findTick(m, a1, x1, y1, a1);
	int c2 = findTick(m, a2, x2, y2, a2);

	if ((s1 != s2) && ((c1 == -1) || (c2 == -1) || (c1==c2)))
	{
		printf("-1");
		return 0;
	}

	if (c1 == c2)
	{
		if (s1 == s2)
			printf("%d", s1);
		else
			printf("-1");
		return 0;
	}

	int t = (s1 - s2) / (c2 - c1);
	if (s1 + c1*t == s2 + c2*t)
		printf("%d", s1 + c1*t);

	return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <windows.h>
using namespace std;

int findTick(int m, int h, int  x, int y, int a)
{
	int tick = 0;
	while (tick < m)
	{
		tick++;
		h = (x*h + y) % m;
		if (h == a)
			return tick;
	}
	return -1;
}

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

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

	//m = 29;
	//h1 = 4;
	//a1 = 0;
	//x1 = 1;
	//y1 = 1;
	//h2 = 25;
	//a2 = 20;
	//x2 = 16;
	//y2 = 0;

	int s1 = findTick(m, h1, x1, y1, a1);
	if (s1 == -1)
	{
		printf("-1");
		return 0;
	}
	int c1 = findTick(m, a1, x1, y1, a1);

	int h3 = h2;
	int x3 = x2;
	int y3 = y2;

	for (int i = 0; i < s1; i++)
		h3 = (x2*h3 + y2) % m;

	for (int i = 1; i < c1; i++)
	{
		x3 = (x3*x2)%m;
		y3 = (y3*x2 + y2)%m;
	}
	int c3 = findTick(m, h3, x3, y3, a2);

	//printf("%d %d %d %d %d\n", s1, s2, c1, c2, c3);

	if (c3!=-1)
		printf("%d", s1+c1*c3);
	else
		printf("-1");

	return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <windows.h>
using namespace std;

int findTick(int m, int h, int  x, int y, int a)
{
	int tick = 0;
	while (tick < m)
	{
		tick++;
		h = (x*h + y) % m;
		if (h == a)
			return tick;
	}
	return -1;
}

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

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

	//m = 29;
	//h1 = 4;
	//a1 = 0;
	//x1 = 1;
	//y1 = 1;
	//h2 = 25;
	//a2 = 20;
	//x2 = 16;
	//y2 = 0;

	int s1 = findTick(m, h1, x1, y1, a1);
	if (s1 == -1)
	{
		printf("-1");
		return 0;
	}
	int c1 = findTick(m, a1, x1, y1, a1);

	int h3 = h2;
	int x3 = x2;
	int y3 = y2;

	for (int i = 0; i < s1; i++)
		h3 = (x2*h3 + y2) % m;

	if (h3 == a2)
	{
		printf("%d", s1);
		return 0;
	}

	for (int i = 1; i < c1; i++)
	{
		x3 = (x3*x2)%m;
		y3 = (y3*x2 + y2)%m;
	}
	int c3 = findTick(m, h3, x3, y3, a2);

	//printf("%d %d %d %d %d\n", s1, s2, c1, c2, c3);

	if (c3!=-1)
		printf("%d", s1+c1*c3);
	else
		printf("-1");

	return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <windows.h>
using namespace std;

int findTick(int m, int h, int  x, int y, int a)
{
	int tick = 0;
	while (tick < m)
	{
		tick++;
		h = (x*h + y) % m;
		if (h == a)
			return tick;
	}
	return -1;
}

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

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

	int s1 = findTick(m, h1, x1, y1, a1);
	if (s1 == -1)
	{
		printf("-1");
		return 0;
	}
	int s2 = findTick(m, h2, x2, y2, a2);
	if (s2 == -1)
	{
		printf("-1");
		return 0;
	}
	if (s1 == s2)
	{
		printf("%d", s1);
		return 0;
	}
	int c1 = findTick(m, a1, x1, y1, a1);
	int c2 = findTick(m, a2, x2, y2, a2);

	if ((s1 != s2) && ((c1 == -1) || (c2 == -1) || (c1==c2)))
	{
		printf("-1");
		return 0;
	}

	if (c1 == c2)
	{
		printf("-1");
		return 0;
	}

	int t = (s1 - s2) / (c2 - c1);
	if (s1 + c1*t == s2 + c2*t)
		printf("%d", s1 + c1*t);

	return 0;
}


#include <iostream>
#define lli long long int
using namespace std;

lli findTick(lli m, lli h, lli  x, lli y, lli a)
{
	lli tick = 0;
	while (tick < m)
	{
		tick++;
		h = (x*h + y) % m;
		if (h == a)
			return tick;
	}
	return -1;
}

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

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

	int s1 = findTick(m, h1, x1, y1, a1);
	if (s1 == -1)
	{
		printf("-1");
		return 0;
	}
	int c1 = findTick(m, a1, x1, y1, a1);

	lli h3 = h2;
	lli x3 = x2;
	lli y3 = y2;

	for (int i = 0; i < s1; i++)
		h3 = (x2*h3 + y2) % m;

	if (h3 == a2)
	{
		cout << s1;
		return 0;
	}
	if (c1 == -1)
	{
		cout << "-1";
		return 0;
	}

	for (int i = 1; i < c1; i++)
	{
		x3 = (x3*x2)%m;
		y3 = (y3*x2 + y2)%m;
	}
	lli c3 = findTick(m, h3, x3, y3, a2);

	if (c3!=-1)
		cout << s1+c1*c3;
	else
		cout << "-1";

	return 0;
}


#include <iostream>
#define lli long long int
using namespace std;

lli findTick(lli m, lli h, lli  x, lli y, lli a)
{
	lli tick = 0;
	while (tick < m)
	{
		tick++;
		h = (x*h + y) % m;
		if (h == a)
			return tick;
	}
	return -1;
}

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

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

	int s1 = findTick(m, h1, x1, y1, a1);
	if (s1 == -1)
	{
		printf("-1");
		return 0;
	}
	int c1 = findTick(m, a1, x1, y1, a1);

	lli h3 = h2;
	lli x3 = x2;
	lli y3 = y2;

	for (int i = 0; i < s1; i++)
		h3 = (x2*h3 + y2) % m;

	if (h3 == a2)
	{
		cout << s1;
		return 0;
	}

	for (int i = 1; i < c1; i++)
	{
		x3 = (x3*x2)%m;
		y3 = (y3*x2 + y2)%m;
	}
	lli c3 = findTick(m, h3, x3, y3, a2);

	if (c3!=-1)
		cout << s1+c1*c3;
	else
		cout << "-1";

	return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <windows.h>
using namespace std;

int findTick(int m, int h, int  x, int y, int a)
{
	int tick = 0;
	while (tick < m)
	{
		tick++;
		h = (x*h + y) % m;
		if (h == a)
			return tick;
	}
	return -1;
}

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

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

	int s1 = findTick(m, h1, x1, y1, a1);
	if (s1 == -1)
	{
		printf("-1");
		return 0;
	}
	int s2 = findTick(m, h2, x2, y2, a2);
	if (s2 == -1)
	{
		printf("-1");
		return 0;
	}
	int c1 = findTick(m, a1, x1, y1, a1);
	int c2 = findTick(m, a2, x2, y2, a2);

	if ((s1 != s2) && ((c1 == -1) || (c2 == -1)))
	{
		printf("-1");
		return 0;
	}

	int t = (s1 - s2) / (c2 - c1);
	if (s1 + c1*t == s2 + c2*t)
		printf("%d", s1 + c1*t);

	return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <windows.h>
using namespace std;

int findTick(int m, int h, int  x, int y, int a)
{
	int tick = 0;
	while (tick < m)
	{
		tick++;
		h = (x*h + y) % m;
		if (h == a)
			return tick;
	}
	return -1;
}

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

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

	int s1 = findTick(m, h1, x1, y1, a1);
	if (s1 == -1)
	{
		printf("-1");
		return 0;
	}
	int s2 = findTick(m, h2, x2, y2, a2);
	if (s2 == -1)
	{
		printf("-1");
		return 0;
	}
	if (s1 == s2)
	{
		printf("%d", s1);
		return 0;
	}
	int c1 = findTick(m, a1, x1, y1, a1);
	int c2 = findTick(m, a2, x2, y2, a2);

	if ((s1 != s2) && ((c1 == -1) || (c2 == -1) || (c1==c2)))
	{
		printf("-1");
		return 0;
	}

	if (c1 == c2)
	{
		printf("-1");
		return 0;
	}

	//printf("%d %d %d %d\n", s1, s2, c1, c2);

	int t = (s1 - s2) / (c2 - c1);
	if (s1 + c1*t == s2 + c2*t)
		printf("%d", s1 + c1*t);
	else
		printf("-1");

	return 0;
}


#include <cstdio>
#include <iostream>
#include <string>
#include <algorithm>
#include <cmath>
#include <vector>
#include <stack>
#include <queue>
#include <list>
#include <map>
#include <set>
#include <stdlib.h>
#include <sstream>
#include <assert.h>
#include <memory.h>
#include <complex>
#include <time.h>
#pragma comment(linker, "/STACK:100000000")
using namespace std;

#define mp make_pair
#define pb push_back
#define ll long long
#define sz(x) (int)(x).size()

int main() {
    //freopen("input.txt","rt",stdin);
    //freopen("gem.in","rt",stdin);
    //freopen("gem.out","wt",stdout);

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

    ll first1 = -1, first2 = -1;
    for(int i = 1; i <= 2500000; i++) {
        h1 = (x1 * h1 + y1) % m;
        if(h1 == a1) {
            if(first1 == -1) first1 = i;
            else if(first2 == -1) first2 = i;
        }
    }

    ll second1 = -1, second2 = -1;
    for(int i = 1; i <= 2500000; i++) {
        h2 = (x2 * h2 + y2) % m;
        if(h2 == a2) {
            if(second1 == -1) second1 = i;
            else if(second2 == -1) second2 = i;
        }
    }

    if(first2 == -1 || second2 == -1) {
        printf("-1\n");
        return 0;
    }

    ll pos1 = first1;
    ll pos2 = second1;
    ll diff1 = first2 - first1;
    ll diff2 = second2 - second1;

    for(int i = 0; i < 5000000; i++) {
        if(pos1 == pos2) {
            printf("%lld\n", pos1);
            return 0;
        }
        if(pos1 + diff1 <= pos2) {
            pos1 += diff1;
        }
        else pos2 += diff2;

    }

    printf("-1\n");
    return 0;
}


#include <cstdio>
#include <iostream>
#include <string>
#include <algorithm>
#include <cmath>
#include <vector>
#include <stack>
#include <queue>
#include <list>
#include <map>
#include <set>
#include <stdlib.h>
#include <sstream>
#include <assert.h>
#include <memory.h>
#include <complex>
#include <time.h>
#pragma comment(linker, "/STACK:100000000")
using namespace std;

#define mp make_pair
#define pb push_back
#define ll long long
#define sz(x) (int)(x).size()

int main() {
    //freopen("input.txt","rt",stdin);
    //freopen("gem.in","rt",stdin);
    //freopen("gem.out","wt",stdout);

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

    ll first1 = -1, first2 = -1;
    for(int i = 1; i <= 2500000; i++) {
        h1 = (x1 * h1 + y1) % m;
        if(h1 == a1) {
            if(first1 == -1) first1 = i;
            else if(first2 == -1) first2 = i;
        }
    }

    ll second1 = -1, second2 = -1;
    for(int i = 1; i <= 2500000; i++) {
        h2 = (x2 * h2 + y2) % m;
        if(h2 == a2) {
            if(second1 == -1) second1 = i;
            else if(second2 == -1) second2 = i;
        }
    }

    if(first1 == -1 || second1 == -1) {
        printf("-1\n");
        return 0;
    }

    ll pos1 = first1;
    ll pos2 = second1;
        if(pos1 == pos2) {
            printf("%lld\n", pos1);
            return 0;
        }
    if(first2 == -1 || second2 == -1) {
        printf("-1\n");
        return 0;
    }
    ll diff1 = first2 - first1;
    ll diff2 = second2 - second1;

    for(int i = 0; i < 5000000; i++) {
        if(pos1 == pos2) {
            printf("%lld\n", pos1);
            return 0;
        }
        if(pos1 + diff1 <= pos2) {
            pos1 += diff1;
        }
        else pos2 += diff2;

    }

    printf("-1\n");
    return 0;
}


 От NVAL, соревнование: Codeforces Round #305 (Div. 1), задача: (A) Майк, лягушка и цветок, Превышено ограничение времени на тесте 65, #

#include <cstdio>
#include <iostream>
#include <string>
#include <algorithm>
#include <cmath>
#include <vector>
#include <queue>
#include <list>
#include <map>
#include <set>
#include <stdlib.h>
#include <sstream>
#include <assert.h>
#include <memory.h>
#include <string.h>
#include <time.h>
using namespace std;
#pragma comment(linker, "/STACK:200000000")

#define mp make_pair
#define pb push_back
#define ll long long
#define sz(x) (int)(x).size()

int main()
{
	//freopen("input.txt","rt",stdin);
    //freopen("output.txt","wt",stdout);
    //freopen("a.in","rt",stdin);
    //freopen("a.out","wt",stdout);

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

	ll step1 = 0;

	ll cur1 = h1;
	while(cur1 != a1) {
		step1++;
		cur1 = (x1 * cur1 + y1) % m;
		if(step1 > 2e6) {
			printf("-1\n");
			return 0;
		}
	}

	ll step2 = 0;

	ll cur2 = h2;
	while(cur2 != a2) {
		step2++;
		cur2 = (x2 * cur2 + y2) % m;
		if(step2 > 2e6) {
			printf("-1\n");
			return 0;
		}
	}
	if(step1 == step2) {
		printf("%lld\n", step1);
		return 0;
	}

	ll len1 = 0;
	do {
		len1++;
		cur1 = (x1 * cur1 + y1) % m;
if(len1 > 2e6) {
printf("-1");
return 0;
}
	} while(cur1 != a1);

	ll len2 = 0;
	do {
		len2++;
		cur2 = (x2 * cur2 + y2) % m;
if(len2 > 2e6) {
printf("-1");
return 0;
}
	} while(cur2 != a2);

	for(int i = 0; i < 2e6; i++) {
		if(step1 == step2) {
			printf("%lld\n", step1);
			return 0;
		}
		if(step2 < step1) step2 += len2;
		else step1 += len1;
	}

	printf("-1");

    return 0;
}


#include <cstdio>
#include <iostream>
#include <string>
#include <algorithm>
#include <cmath>
#include <vector>
#include <queue>
#include <list>
#include <map>
#include <set>
#include <stdlib.h>
#include <sstream>
#include <assert.h>
#include <memory.h>
#include <string.h>
#include <time.h>
using namespace std;
#pragma comment(linker, "/STACK:200000000")

#define mp make_pair
#define pb push_back
#define ll long long
#define sz(x) (int)(x).size()

int main()
{
	//freopen("input.txt","rt",stdin);
    //freopen("output.txt","wt",stdout);
    //freopen("a.in","rt",stdin);
    //freopen("a.out","wt",stdout);

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

	ll step1 = 0;

	ll cur1 = h1;
	while(cur1 != a1) {
		step1++;
		cur1 = (x1 * cur1 + y1) % m;
		if(step1 > 2e6) {
			printf("-1\n");
			return 0;
		}
	}

	ll step2 = 0;

	ll cur2 = h2;
	while(cur2 != a2) {
		step2++;
		cur2 = (x2 * cur2 + y2) % m;
		if(step2 > 2e6) {
			printf("-1\n");
			return 0;
		}
	}
	if(step1 == step2) {
		printf("%lld\n", step1);
		return 0;
	}

	ll len1 = 0;
	do {
		len1++;
		cur1 = (x1 * cur1 + y1) % m;
if(len1 > 2e6) {
printf("-1");
return 0;
}
	} while(cur1 != a1);

	ll len2 = 0;
	do {
		len2++;
		cur2 = (x2 * cur2 + y2) % m;
if(len2 > 2e6) {
printf("-1");
return 0;
}
	} while(cur2 != a2);

	for(int i = 0; i < 2e6; i++) {
		if(step1 == step2) {
			printf("%lld\n", step1);
			return 0;
		}
		if(step2 < step1) step2 += len2;
		else step1 += len1;
	}

	printf("-1");

    return 0;
}


#include <cstdio>
#include <iostream>
#include <string>
#include <algorithm>
#include <cmath>
#include <vector>
#include <stack>
#include <queue>
#include <list>
#include <map>
#include <set>
#include <stdlib.h>
#include <sstream>
#include <assert.h>
#include <memory.h>
#include <complex>
#include <time.h>
#pragma comment(linker, "/STACK:100000000")
using namespace std;

#define mp make_pair
#define pb push_back
#define ll long long
#define sz(x) (int)(x).size()

int main() {
    //freopen("input.txt","rt",stdin);
    //freopen("gem.in","rt",stdin);
    //freopen("gem.out","wt",stdout);

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

    ll first1 = -1, first2 = -1;
    for(int i = 1; i <= 2500000; i++) {
        h1 = (x1 * h1 + y1) % m;
        if(h1 == a1) {
            if(first1 == -1) first1 = i;
            else if(first2 == -1) first2 = i;
        }
    }

    ll second1 = -1, second2 = -1;
    for(int i = 1; i <= 2500000; i++) {
        h2 = (x2 * h2 + y2) % m;
        if(h2 == a2) {
            if(second1 == -1) second1 = i;
            else if(second2 == -1) second2 = i;
        }
    }

    if(first2 == -1 || second2 == -1) {
        printf("-1\n");
        return 0;
    }

    ll pos1 = first1;
    ll pos2 = second1;
    ll diff1 = first2 - first1;
    ll diff2 = second2 - second1;

    for(int i = 0; i < 50000000; i++) {
        if(pos1 == pos2) {
            printf("%lld\n", pos1);
            return 0;
        }
        if(pos1 + diff1 <= pos2) {
            pos1 += diff1;
        }
        else pos2 += diff2;

    }

    printf("-1\n");
    return 0;
}


#include <cstdio>
#include <iostream>
#include <string>
#include <algorithm>
#include <cmath>
#include <vector>
#include <stack>
#include <queue>
#include <list>
#include <map>
#include <set>
#include <stdlib.h>
#include <sstream>
#include <assert.h>
#include <memory.h>
#include <complex>
#include <time.h>
#pragma comment(linker, "/STACK:100000000")
using namespace std;

#define mp make_pair
#define pb push_back
#define ll long long
#define sz(x) (int)(x).size()

int main() {
    //freopen("input.txt","rt",stdin);
    //freopen("gem.in","rt",stdin);
    //freopen("gem.out","wt",stdout);

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

    ll first1 = -1, first2 = -1;
    for(int i = 1; i <= 2500000; i++) {
        h1 = (x1 * h1 + y1) % m;
        if(h1 == a1) {
            if(first1 == -1) first1 = i;
            else if(first2 == -1) first2 = i;
        }
    }

    ll second1 = -1, second2 = -1;
    for(int i = 1; i <= 2500000; i++) {
        h2 = (x2 * h2 + y2) % m;
        if(h2 == a2) {
            if(second1 == -1) second1 = i;
            else if(second2 == -1) second2 = i;
        }
    }

    if(first1 == -1 || second1 == -1) {
        printf("-1\n");
        return 0;
    }

    ll pos1 = first1;
    ll pos2 = second1;
    ll tmp = 1e9;

    if(first1 != -1 && second1 != -1 && first1 == second1) {
        tmp = min(tmp, first1);
    }
    if(first1 != -1 && second2 != -1 && first1 == second2) {
        tmp = min(tmp, first1);
    }
    if(first2 != -1 && second1 != -1 && first2 == second1) {
        tmp = min(tmp, first2);
    }
    if(tmp != 1e9) {
        printf("%lld", tmp);
        return 0;
    }

    if(first2 == -1 || second2 == -1) {
        printf("-1\n");
        return 0;
    }
    ll diff1 = first2 - first1;
    ll diff2 = second2 - second1;

    for(int i = 0; i < 5000000; i++) {
        if(pos1 == pos2) {
            printf("%lld\n", pos1);
            return 0;
        }
        if(pos1 + diff1 <= pos2) {
            pos1 += diff1;
        }
        else pos2 += diff2;

    }

    printf("-1\n");
    return 0;
}


#include <cstdio>
#include <iostream>
#include <string>
#include <algorithm>
#include <cmath>
#include <vector>
#include <queue>
#include <list>
#include <map>
#include <set>
#include <stdlib.h>
#include <sstream>
#include <assert.h>
#include <memory.h>
#include <string.h>
#include <time.h>
using namespace std;
#pragma comment(linker, "/STACK:200000000")

#define mp make_pair
#define pb push_back
#define ll long long
#define sz(x) (int)(x).size()

int main()
{
	//freopen("input.txt","rt",stdin);
    //freopen("output.txt","wt",stdout);
    //freopen("a.in","rt",stdin);
    //freopen("a.out","wt",stdout);

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

	ll step1 = 0;

	ll cur1 = h1;
	while(cur1 != a1) {
		step1++;
		cur1 = (x1 * cur1 + y1) % m;
		if(step1 > 2e6) {
			printf("-1\n");
			return 0;
		}
	}

	ll step2 = 0;

	ll cur2 = h2;
	while(cur2 != a2) {
		step2++;
		cur2 = (x2 * cur2 + y2) % m;
		if(step2 > 2e6) {
			printf("-1\n");
			return 0;
		}
	}

	ll len1 = 0;
	do {
		len1++;
		cur1 = (x1 * cur1 + y1) % m;
	} while(cur1 != a1);

	ll len2 = 0;
	do {
		len2++;
		cur2 = (x2 * cur2 + y2) % m;
	} while(cur2 != a2);

	if(step1 == step2) {
		printf("%lld\n", step1);
		return 0;
	}

	for(int i = 0; i < 2e6; i++) {
		if(step1 == step2) {
			printf("%lld\n", step1);
			return 0;
		}
		if(step2 < step1) step2 += len2;
		else step1 += len1;
	}

	printf("-1");

    return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<string>
#include<math.h>
#include<queue>
#include<stack>
#include<sstream>
#include<stdio.h>
#include<map>
#include<set>
#include<memory.h>
#include<algorithm>
#include<vector>
using namespace std;
typedef long long ll;
ll gcd(ll a, ll b){
    if (!b)
        return a;
    return gcd(b, a%b);
}
ll lcm(ll a, ll b){
    return b / gcd(a, b)*a;
}
#define FOR(I,N) for(int(i)=0;i<int(N);++i)
#define FORK(I,N,K) for(int(i)=0;i<int(N);i+=int(K))
const ll N = 1e6 + 2;
bool vis1[N], vis2[N];
ll a1, a2, h1, h2, x1, x2, z1, z2, m;
ll res;
vector<int>a, b;
int main(){
    cin >> m;
    cin >> h1 >> a1 >> x1 >> z1;
    cin >> h2 >> a2 >> x2 >> z2;
    while (res <= N){
        if (h1 == a1 && h2 == a2){
            cout << res << endl;
            return 0;
        }
        ++res;
        h1 = (h1 * x1 + z1) % m;
        h2 = (h2 * x2 + z2) % m;
    }
    ll H1 = h1, H2 = h2;
    while (!vis1[h1]){
        a.push_back(h1);
        vis1[h1] = 1;
        h1 = (h1 * x1 + z1) % m;
    }
    while (!vis2[h2]){
        b.push_back(h2);
        vis2[h2] = 1;
        h2 = (h2 * x2 + z2) % m;
    }
    int pos1, pos2, tar1 = -1, tar2 = -1;
    for (int i = 0; i<a.size(); ++i){
        if (a[i] == h1)
            pos1 = i;
        if (a[i] == a1)
            tar1 = i;
    }
    for (int i = 0; i<b.size(); ++i){
        if (b[i] == h2)
            pos2 = i;
        if (b[i] == a2)
            tar2 = i;
    }
    while (res <= N *N && tar1 != -1 && tar2 != -1){
        if (pos1 == tar1 && pos2 == tar2)
            break;
        if (pos1 + 1 == a.size() || pos2 == b.size()){
            pos1 = (pos1 + 1) % a.size();
            pos2 = (pos2 + 1) % b.size();
        }
        else{
            int x, y;
            if (pos1 <= tar1)
                x = tar1 - pos1;
            else
                x = a.size() - pos1 - 1;
            if (pos2 <= tar2)
                y = tar2 - pos2;
            else
                y = b.size() - pos2 - 1;
            x = min(x, y);
            res += x;
            pos1 += x;
            pos2 += x;
        }
    }
    if (res > N * N || tar1 == -1 || tar2 == -1)
        res = -1;
    cout << res << endl;
}


#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<string>
#include<math.h>
#include<queue>
#include<stack>
#include<sstream>
#include<stdio.h>
#include<map>
#include<set>
#include<memory.h>
#include<algorithm>
#include<vector>
using namespace std;
typedef long long ll;
ll gcd(ll a, ll b){
    if (!b)
        return a;
    return gcd(b, a%b);
}
ll lcm(ll a, ll b){
    return b / gcd(a, b)*a;
}
#define FOR(I,N) for(int(i)=0;i<int(N);++i)
#define FORK(I,N,K) for(int(i)=0;i<int(N);i+=int(K))
const ll N = 1e6 + 2;
bool vis1[N], vis2[N];
ll a1, a2, h1, h2, x1, x2, z1, z2, m;
ll res;
vector<int>a, b;
int main(){
    cin >> m;
    cin >> h1 >> a1 >> x1 >> z1;
    cin >> h2 >> a2 >> x2 >> z2;
    while (res <= N){
        if (h1 == a1 && h2 == a2){
            cout << res << endl;
            return 0;
        }
        ++res;
        h1 = (h1 * x1 + z1) % m;
        h2 = (h2 * x2 + z2) % m;
    }
    while (!vis1[h1]){
        a.push_back(h1);
        vis1[h1] = 1;
        h1 = (h1 * x1 + z1) % m;
    }
    while (!vis2[h2]){
        b.push_back(h2);
        vis2[h2] = 1;
        h2 = (h2 * x2 + z2) % m;
    }
    int pos1, pos2, tar1 = -1, tar2 = -1;
    for (int i = 0; i<a.size(); ++i){
        if (a[i] == h1)
            pos1 = i;
        if (a[i] == a1)
            tar1 = i;
    }
    for (int i = 0; i<b.size(); ++i){
        if (b[i] == h2)
            pos2 = i;
        if (b[i] == a2)
            tar2 = i;
    }
    while (res <= 1ll * a.size() * b.size() && tar1 != -1 && tar2 != -1){
        if (pos1 == tar1 && pos2 == tar2)
            break;
        if (pos1 + 1 == a.size() || pos2 == b.size()){
            pos1 = (pos1 + 1) % a.size();
            pos2 = (pos2 + 1) % b.size();
        }
        else{
            int x, y;
            if (pos1 <= tar1)
                x = tar1 - pos1;
            else
                x = a.size() - pos1 - 1 + tar1 + 1;
            if (pos2 <= tar2)
                y = tar2 - pos2;
            else
                y = b.size() - pos2 - 1 + tar2 + 1;
            x = max(x, y);
            res += x;
            pos1 = (pos1 + x) % a.size();
            pos2 = (x + pos2) % b.size();
        }
    }
    if (res >1ll * a.size() * b.size() || tar1 == -1 || tar2 == -1)
        res = -1;
    cout << res << endl;
}


#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<string>
#include<math.h>
#include<queue>
#include<stack>
#include<sstream>
#include<stdio.h>
#include<map>
#include<set>
#include<memory.h>
#include<algorithm>
#include<vector>
using namespace std;
typedef long long ll;
ll gcd(ll a, ll b){
    if (!b)
        return a;
    return gcd(b, a%b);
}
ll lcm(ll a, ll b){
    return b / gcd(a, b)*a;
}
#define FOR(I,N) for(int(i)=0;i<int(N);++i)
#define FORK(I,N,K) for(int(i)=0;i<int(N);i+=int(K))
const int N = 1e6 + 2;
bool vis[1000001];
bool vis2[1000001];
int a1, a2, h1, h2, x1, x2, Y1, y2;
int m;
vector<int>s1, s2;
int idx1[N], idx2[N];
int l1, l2;
int main(){
    cin >> m;
    cin >> h1 >> a1 >> x1 >> Y1;
    cin >> h2 >> a2 >> x2 >> y2;
    ll x = h1 % m;
    while (!vis[x]){
        idx1[x] = s1.size();
        s1.push_back(x);
        vis[x] = 1;
        x = (x * x1 + Y1) % m;
        l1 = x;
    }
    if (!vis[a1]){
        cout << -1;
        return 0;
    }
    x = h2 % m;
    while (!vis2[x]){
        idx2[x] = s2.size();
        s2.push_back(x);
        vis2[x] = 1;
        x = (x * x2 + y2) % m;
        l2 = x;
    }
    if (!vis2[a2]){
        cout << -1;
        return 0;
    }
    int pos1 = -1, t1 = -1;
    for (int i = 0; i < s1.size(); ++i){
        if (s1[i] == h1)
            pos1 = i;
        else if (s1[i] == a1)
            t1 = i;
    }
    int pos2 = -1, t2 = -1;
    for (int i = 0; i < s2.size(); ++i){
        if (s2[i] == h2)
            pos2 = i;
        else if (s2[i] == a2)
            t2 = i;
    }
    if (pos1 == -1 || pos2 == -1 || t1 == -1 || t2 == -1)
        pos1 = 1 / (pos1 + 1);
    ll res = 0;
    while (true){
        if (pos1 == t1 &&  pos2 == t2)
            break;
        int nxt = 0;
        int r1 = 0;
        if (pos1 > t1)
            r1 = s1.size() - pos1 - 1;
        else if (pos1 == t1)
            r1 = s1.size();
        else
            r1 = t1 - pos1;
        int r2 = 0;
        if (pos2 > t2)
            r2 = s2.size() - pos2 - 1;
        else if (pos2 == t2)
            r2 = s2.size();
        else
            r2 = t2 - pos2;
        nxt = max(r1, r2);
        pos1 = (pos1 + nxt) % s1.size();
        pos2 = (pos2 + nxt) % s2.size();
        res += nxt;
        if (pos1 == t1 &&  pos2 == t2)
            break;
        if (pos1 + 1 == s1.size() && pos2 + 1 == s2.size()){
            pos1 = idx1[l1];
            pos2 = idx2[l2];
            ++res;
        }
        else if (pos1 + 1 == s1.size()){
            pos1 = idx1[l1];
            ++pos2;
            ++res;
        }
        else if (pos2 + 1 == s2.size()){
            pos2 = idx2[l2];
            ++pos1;
            ++res;
        }
        if (res > 1ll * s1.size() * s2.size()){
            res = -1;
            break;
        }
    }
    cout << res << endl;
}


#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<string>
#include<math.h>
#include<queue>
#include<stack>
#include<sstream>
#include<stdio.h>
#include<map>
#include<set>
#include<memory.h>
#include<algorithm>
#include<vector>
using namespace std;
typedef long long ll;
ll gcd(ll a, ll b){
    if (!b)
        return a;
    return gcd(b, a%b);
}
ll lcm(ll a, ll b){
    return b / gcd(a, b)*a;
}
#define FOR(I,N) for(int(i)=0;i<int(N);++i)
#define FORK(I,N,K) for(int(i)=0;i<int(N);i+=int(K))
const int N = 1e6 + 2;
bool vis[1000001];
bool vis2[1000001];
int a1, a2, h1, h2, x1, x2, Y1, y2;
int m;
vector<int>s1, s2;
int idx1[N], idx2[N];
int l1, l2;
int main(){
    cin >> m;
    cin >> h1 >> a1 >> x1 >> Y1;
    cin >> h2 >> a2 >> x2 >> y2;
    ll x = h1 % m;
    while (!vis[x]){
        idx1[x] = s1.size();
        s1.push_back(x);
        vis[x] = 1;
        x = (x * x1 + Y1) % m;
        l1 = x;
    }
    if (!vis[a1]){
        cout << -1;
        return 0;
    }
    x = h2 % m;
    while (!vis2[x]){
        idx2[x] = s2.size();
        s2.push_back(x);
        vis2[x] = 1;
        x = (x * x2 + y2) % m;
        l2 = x;
    }
    if (!vis2[a2]){
        cout << -1;
        return 0;
    }
    int pos1 = -1,t1 = -1;
    for (int i = 0; i < s1.size(); ++i){
        if (s1[i] == h1)
            pos1 = i;
        else if (s1[i] == a1)
            t1 = i;
    }
    int pos2 = -1,t2 = -1;
    for (int i = 0; i < s2.size(); ++i){
        if (s2[i] == h2)
            pos2 = i;
        else if (s2[i] == a2)
            t2 = i;
    }
    if (pos1 == -1 || pos2 == -1 || t1 == -1 || t2 == -1)
        pos1 = 1 / (pos1 + 1);
    ll res = 0;
    while (true){
        if (pos1 == t1 &&  pos2 == t2)
            break;
        int nxt = 0;
        int r1 = 0;
        if (pos1 > t1)
            r1 = s1.size() - pos1 - 1;
        else if (pos1 == t1)
            r1 = s1.size();
        else
            r1 = t1 - pos1;
        int r2 = 0;
        if (pos2 > t2)
            r2 = s2.size() - pos2 - 1;
        else if (pos2 == t2)
            r2 = s2.size();
        else
            r2 = t2 - pos2;
        nxt = max(r1, r2);
        pos1 = (pos1 + nxt) % s1.size();
        pos2 = (pos2 + nxt) % s2.size();
        res += nxt;
        if (pos1 == t1 &&  pos2 == t2)
            break;
        if (pos1 + 1== s1.size() && pos2 +1== s2.size()){
            pos1 = idx1[l1];
            pos2 = idx2[l2];
            ++res;
        }
        else if (pos1+1 == s1.size()){
            pos1 = idx1[l1];
            ++pos2;
            ++res;
        }
        else if (pos2 + 1 == s2.size()){
            pos2 = idx2[l2];
            ++pos1;
            ++res;
        }
    }
    cout << res << endl;
}


#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<string>
#include<math.h>
#include<queue>
#include<stack>
#include<sstream>
#include<stdio.h>
#include<map>
#include<set>
#include<memory.h>
#include<algorithm>
#include<vector>
using namespace std;
typedef long long ll;
ll gcd(ll a, ll b){
    if (!b)
        return a;
    return gcd(b, a%b);
}
ll lcm(ll a, ll b){
    return b / gcd(a, b)*a;
}
#define FOR(I,N) for(int(i)=0;i<int(N);++i)
#define FORK(I,N,K) for(int(i)=0;i<int(N);i+=int(K))
const int N = 1e6 + 2;
bool vis[1000001];
bool vis2[1000001];
int a1, a2, h1, h2, x1, x2, Y1, y2;
int m;
vector<int>s1, s2;
int idx1[N], idx2[N];
int l1, l2;
int main(){
    cin >> m;
    cin >> h1 >> a1 >> x1 >> Y1;
    cin >> h2 >> a2 >> x2 >> y2;
    ll x = h1 % m;
    while (!vis[x]){
        idx1[x] = s1.size();
        s1.push_back(x);
        vis[x] = 1;
        x = (x * x1 + Y1) % m;
        l1 = x;
    }
    if (!vis[a1]){
        cout << -1;
        return 0;
    }
    x = h2 % m;
    while (!vis2[x]){
        idx2[x] = s2.size();
        s2.push_back(x);
        vis2[x] = 1;
        x = (x * x2 + y2) % m;
        l2 = x;
    }
    if (!vis2[a2]){
        cout << -1;
        return 0;
    }
    int pos1 = -1, t1 = -1;
    for (int i = 0; i < s1.size(); ++i){
        if (s1[i] == h1)
            pos1 = i;
        else if (s1[i] == a1)
            t1 = i;
    }
    int pos2 = -1, t2 = -1;
    for (int i = 0; i < s2.size(); ++i){
        if (s2[i] == h2)
            pos2 = i;
        else if (s2[i] == a2)
            t2 = i;
    }
    if (pos1 == -1 || pos2 == -1 || t1 == -1 || t2 == -1)
        pos1 = 1 / (pos1 + 1);
    ll res = 0;
    cout << s1.size() << " " << s2.size() << endl;
    while (true){
        if (pos1 == t1 &&  pos2 == t2)
            break;
        int nxt = 0;
        int r1 = 0;
        if (pos1 > t1)
            r1 = s1.size() - pos1 - 1;
        else if (pos1 == t1)
            r1 = s1.size();
        else
            r1 = t1 - pos1;
        int r2 = 0;
        if (pos2 > t2)
            r2 = s2.size() - pos2 - 1;
        else if (pos2 == t2)
            r2 = s2.size();
        else
            r2 = t2 - pos2;
        nxt = max(r1, r2);
        pos1 = (pos1 + nxt) % s1.size();
        pos2 = (pos2 + nxt) % s2.size();
        res += nxt;
        if (pos1 == t1 &&  pos2 == t2)
            break;
        if (pos1 + 1 == s1.size() && pos2 + 1 == s2.size()){
            pos1 = idx1[l1];
            pos2 = idx2[l2];
            ++res;
        }
        else if (pos1 + 1 == s1.size()){
            pos1 = idx1[l1];
            ++pos2;
            ++res;
        }
        else if (pos2 + 1 == s2.size()){
            pos2 = idx2[l2];
            ++pos1;
            ++res;
        }
        if (res > 1ll * s1.size() * s2.size()){
            res = -1;
            break;
        }
    }
    cout << res << endl;
}


#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<string>
#include<math.h>
#include<queue>
#include<stack>
#include<sstream>
#include<stdio.h>
#include<map>
#include<set>
#include<memory.h>
#include<algorithm>
#include<vector>
using namespace std;
typedef long long ll;
ll gcd(ll a, ll b){
    if (!b)
        return a;
    return gcd(b, a%b);
}
ll lcm(ll a, ll b){
    return b / gcd(a, b)*a;
}
#define FOR(I,N) for(int(i)=0;i<int(N);++i)
#define FORK(I,N,K) for(int(i)=0;i<int(N);i+=int(K))
const int N = 1e6 + 2;
int vis[1000001];
int vis2[1000001];
int par[N], par2[N];
int cost[N], cost2[N];
int a1, a2, h1, h2, x1, x2, Y1, y2;
int m;
vector<int>s1, s2;
int main(){
    memset(cost, -1, sizeof(cost));
    memset(cost2, -1, sizeof(cost2));
    cin >> m;
    cin >> h1 >> a1 >> x1 >> Y1;
    cin >> h2 >> a2 >> x2 >> y2;
    ll x = h1 % m;
    int c = 0;
    while (!vis[x]){
        s1.push_back(x);
        vis[x] = ++c;
        x = (x * x1 + Y1) % m;
    }
    if (!vis[a1]){
        cout << -1;
        return 0;
    }
    int c2 = 0;
    x = h2 % m;
    while (!vis2[x]){
        s2.push_back(x);
        vis2[x] = ++c2;
        x = (x * x2 + y2) % m;
    }
    if (!vis2[a2]){
        cout << -1;
        return 0;
    }
    int pos1 = -1,t1;
    for (int i = 0; i < s1.size(); ++i){
        //cout << s1[i] << endl;
        if (s1[i] == h1){
            pos1 = i;
        }
        else if (s1[i] == a1){
            t1 = i;
        }
    }
    int pos2 = -1,t2;
    //puts("");
    for (int i = 0; i < s2.size(); ++i){
        //cout << s2[i] << endl;
        if (s2[i] == h2){
            pos2 = i;
        }
        else if (s2[i] == a2){
            t2 = i;
        }
    }
    ll res = 0;
    while (true){
        if (pos1 == t1 &&  pos2 == t2)
            break;
        int nxt = 0;
        int r1 = 0;
        if (pos1 > t1)
            r1 = s1.size() - pos1 + t1;
        else if (pos1 == t1)
            r1 = s1.size();
        else
            r1 = t1 - pos1;
        int r2 = 0;
        if (pos2 > t2)
            r2 = s2.size() - pos2 + t2;
        else if (pos2 == t2)
            r2 = s2.size();
        else
            r2 = t2 - pos2;
        nxt = max(r1, r2);
        pos1 = (pos1 + nxt) % s1.size();
        pos2 = (pos2 + nxt) % s2.size();
        res += nxt;
    }
    cout << res << endl;
}


#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<string>
#include<math.h>
#include<queue>
#include<stack>
#include<sstream>
#include<stdio.h>
#include<map>
#include<set>
#include<memory.h>
#include<algorithm>
#include<vector>
using namespace std;
typedef long long ll;
ll gcd(ll a, ll b){
    if (!b)
        return a;
    return gcd(b, a%b);
}
ll lcm(ll a, ll b){
    return b / gcd(a, b)*a;
}
#define FOR(I,N) for(int(i)=0;i<int(N);++i)
#define FORK(I,N,K) for(int(i)=0;i<int(N);i+=int(K))
const ll N = 1e6 + 2;
bool vis1[N], vis2[N];
ll a1, a2, h1, h2, x1, x2, z1, z2, m;
ll res;
vector<int>a, b;
int main(){
    cin >> m;
    cin >> h1 >> a1 >> x1 >> z1;
    cin >> h2 >> a2 >> x2 >> z2;
    while (res <= N){
        if (h1 == a1 && h2 == a2){
            cout << res << endl;
            return 0;
        }
        ++res;
        h1 = (h1 * x1 + z1) % m;
        h2 = (h2 * x2 + z2) % m;
    }
    ll H1 = h1, H2 = h2;
    while (!vis1[h1]){
        a.push_back(h1);
        vis1[h1] = 1;
        h1 = (h1 * x1 + z1) % m;
    }
    while (!vis2[h2]){
        b.push_back(h2);
        vis2[h2] = 1;
        h2 = (h2 * x2 + z2) % m;
    }
    int pos1, pos2, tar1 = -1, tar2 = -1;
    for (int i = 0; i<a.size(); ++i){
        if (a[i] == h1)
            pos1 = i;
        if (a[i] == a1)
            tar1 = i;
    }
    for (int i = 0; i<b.size(); ++i){
        if (b[i] == h2)
            pos2 = i;
        if (b[i] == a2)
            tar2 = i;
    }
    while (res <= N *N && tar1 != -1 && tar2 != -1){
        if (pos1 == tar1 && pos2 == tar2)
            break;
        if (pos1 + 1 == a.size() || pos2 == b.size()){
            pos1 = (pos1 + 1) % a.size();
            pos2 = (pos2 + 1) % b.size();
        }
        else{
            int x, y;
            if (pos1 <= tar1)
                x = tar1 - pos1;
            else
                x = a.size() - pos1 - 1;
            if (pos2 <= tar2)
                y = tar2 - pos2;
            else
                y = b.size() - pos2 - 1;
            x = max(x, y);
            res += x;
            pos1 += x;
            pos2 += x;
        }
    }
    if (res > N * N || tar1 == -1 || tar2 == -1)
        res = -1;
    cout << res << endl;
}


#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<string>
#include<math.h>
#include<queue>
#include<stack>
#include<sstream>
#include<stdio.h>
#include<map>
#include<set>
#include<memory.h>
#include<algorithm>
#include<vector>
using namespace std;
typedef long long ll;
ll gcd(ll a, ll b){
    if (!b)
        return a;
    return gcd(b, a%b);
}
ll lcm(ll a, ll b){
    return b / gcd(a, b)*a;
}
#define FOR(I,N) for(int(i)=0;i<int(N);++i)
#define FORK(I,N,K) for(int(i)=0;i<int(N);i+=int(K))
const ll N = 1e6 + 2;
bool vis1[N], vis2[N];
ll a1, a2, h1, h2, x1, x2, z1, z2, m;
ll res;
vector<int>a, b;
int main(){
    cin >> m;
    cin >> h1 >> a1 >> x1 >> z1;
    cin >> h2 >> a2 >> x2 >> z2;
    while (res <= N){
        if (h1 == a1 && h2 == a2){
            cout << res << endl;
            return 0;
        }
        ++res;
        h1 = (h1 * x1 + z1) % m;
        h2 = (h2 * x2 + z2) % m;
    }
    while (!vis1[h1]){
        a.push_back(h1);
        vis1[h1] = 1;
        h1 = (h1 * x1 + z1) % m;
    }
    while (!vis2[h2]){
        b.push_back(h2);
        vis2[h2] = 1;
        h2 = (h2 * x2 + z2) % m;
    }
    int pos1, pos2, tar1 = -1, tar2 = -1;
    for (int i = 0; i<a.size(); ++i){
        if (a[i] == h1)
            pos1 = i;
        if (a[i] == a1)
            tar1 = i;
    }
    for (int i = 0; i<b.size(); ++i){
        if (b[i] == h2)
            pos2 = i;
        if (b[i] == a2)
            tar2 = i;
    }
    while (res <= 1ll * a.size() * b.size() && tar1 != -1 && tar2 != -1){
        if (pos1 == tar1 && pos2 == tar2)
            break;
        if (pos1 + 1 == a.size() || pos2 == b.size()){
            pos1 = (pos1 + 1) % a.size();
            pos2 = (pos2 + 1) % b.size();
            ++res;
        }
        else{
            int x, y;
            if (pos1 <= tar1)
                x = tar1 - pos1;
            else
                x = a.size() - pos1 - 1 + tar1 + 1;
            if (pos2 <= tar2)
                y = tar2 - pos2;
            else
                y = b.size() - pos2 - 1 + tar2 + 1;
            x = max(x, y);
            res += x;
            pos1 = (pos1 + x) % a.size();
            pos2 = (x + pos2) % b.size();
        }
    }
    if (res >1ll * a.size() * b.size() || tar1 == -1 || tar2 == -1)
        res = -1;
    cout << res << endl;
}


#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<string>
#include<math.h>
#include<queue>
#include<stack>
#include<sstream>
#include<stdio.h>
#include<map>
#include<set>
#include<memory.h>
#include<algorithm>
#include<vector>
using namespace std;
typedef long long ll;
ll gcd(ll a, ll b){
    if (!b)
        return a;
    return gcd(b, a%b);
}
ll lcm(ll a, ll b){
    return b / gcd(a, b)*a;
}
#define FOR(I,N) for(int(i)=0;i<int(N);++i)
#define FORK(I,N,K) for(int(i)=0;i<int(N);i+=int(K))
const int N = 1e6 + 2;
bool vis[1000001];
bool vis2[1000001];
int a1, a2, h1, h2, x1, x2, Y1, y2;
int m;
vector<int>s1, s2;
int idx1[N], idx2[N];
int l1, l2;
int main(){
    cin >> m;
    cin >> h1 >> a1 >> x1 >> Y1;
    cin >> h2 >> a2 >> x2 >> y2;
    ll x = h1 % m;
    while (!vis[x]){
        idx1[x] = s1.size();
        s1.push_back(x);
        vis[x] = 1;
        x = (x * x1 + Y1) % m;
        l1 = x;
    }
    if (!vis[a1]){
        cout << -1;
        return 0;
    }
    x = h2 % m;
    while (!vis2[x]){
        idx2[x] = s2.size();
        s2.push_back(x);
        vis2[x] = 1;
        x = (x * x2 + y2) % m;
        l2 = x;
    }
    if (!vis2[a2]){
        cout << -1;
        return 0;
    }
    int pos1 = -1,t1 = -1;
    for (int i = 0; i < s1.size(); ++i){
        if (s1[i] == h1)
            pos1 = i;
        else if (s1[i] == a1)
            t1 = i;
    }
    int pos2 = -1,t2 = -1;
    for (int i = 0; i < s2.size(); ++i){
        if (s2[i] == h2)
            pos2 = i;
        else if (s2[i] == a2)
            t2 = i;
    }
    if (pos1 == -1 || pos2 == -1 || t1 == -1 || t2 == -1)
        pos1 = 1 / (pos1 + 1);
    ll res = 0;
    while (true){
        if (pos1 == t1 &&  pos2 == t2)
            break;
        int nxt = 0;
        int r1 = 0;
        if (pos1 > t1)
            r1 = s1.size() - pos1 - 1;
        else if (pos1 == t1)
            r1 = s1.size();
        else
            r1 = t1 - pos1;
        int r2 = 0;
        if (pos2 > t2)
            r2 = s2.size() - pos2 - 1;
        else if (pos2 == t2)
            r2 = s2.size();
        else
            r2 = t2 - pos2;
        nxt = max(r1, r2);
        pos1 = (pos1 + nxt) % s1.size();
        pos2 = (pos2 + nxt) % s2.size();
        res += nxt;
        if (pos1 == t1 &&  pos2 == t2)
            break;
        if (pos1 + 1== s1.size() && pos2 +1== s2.size()){
            pos1 = idx1[l1];
            pos2 = idx2[l2];
            ++res;
        }
        else if (pos1+1 == s1.size()){
            pos1 = idx1[l1];
            ++pos2;
            ++res;
        }
        else if (pos2 + 1 == s2.size()){
            pos2 = idx2[l2];
            ++pos1;
            ++res;
        }
        if(res > 10000000000000){
            res = -1;
            break;
            }
    }
    cout << res << endl;
}


#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <iostream>
#include <algorithm>
#include <map>
#include <set>
#include <queue>

using namespace std;

int m;
long long h[2], a[2], x[2], y[2];
long long p[2], z[2];

int main()
{
    cin >> m >> h[0] >> a[0] >> x[0] >> y[0] >> h[1] >> a[1] >> x[1] >> y[1];
    for (int i = 0; i < 2; i++)
    {
        long long he = h[i];
        z[i] = -1;
        for (int j = 0; j < 2 * m; j++)
        {
            if (he == a[i])
            {
                z[i] = j;
                break;
            }
            he = (he * x[i] + y[i]) % m;
        }
        if (z[i] < 0)
        {
            cout << -1 << endl;
            return 0;
        }
        do
        {
            he = (he * x[i] + y[i]) % m;
            p[i] ++;
        } while (he != a[i] && p[i] < 2 * m);
        if (p[i] >= 2 * m)
        {
            cout << -1 << endl;
            return 0;
        }
    }
    if (z[0] < z[1])
        swap(p[0], p[1]), swap(z[0], z[1]);
    long long ans = z[0], inf = p[0] * p[1] + a[0];
    z[1] %= p[1];
    while (ans % p[1] != z[1])
    {
        ans += p[0];
        if (ans > 2 * inf)
        {
            cout << -1 << endl;
            return 0;
        }
    }
    cout << ans << endl;

    return 0;
}


#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <iostream>
#include <algorithm>
#include <map>
#include <set>
#include <queue>

using namespace std;

int m;
long long h[2], a[2], x[2], y[2];
long long p[2], z[2];

int main()
{
    cin >> m >> h[0] >> a[0] >> x[0] >> y[0] >> h[1] >> a[1] >> x[1] >> y[1];
    for (int i = 0; i < 2; i++)
    {
        long long he = h[i];
        z[i] = -1;
        for (int j = 0; j < 2 * m; j++)
        {
            if (he == a[i])
            {
                z[i] = j;
                break;
            }
            he = (he * x[i] + y[i]) % m;
        }
        if (z[i] < 0)
        {
            cout << -1 << endl;
            return 0;
        }
        do
        {
            he = (he * x[i] + y[i]) % m;
            p[i] ++;
        } while (he != a[i] && p[i] < 2 * m);
        if (p[i] >= 2 * m)
        {
            cout << -1 << endl;
            return 0;
        }
    }
    if (z[0] < z[1])
        swap(p[0], p[1]), swap(z[0], z[1]);
    long long ans = z[0], inf = p[0] * p[1] + z[0];
    z[1] %= p[1];
    while (ans % p[1] != z[1])
    {
        ans += p[0];
        if (ans > 3 * inf)
        {
            cout << -1 << endl;
            return 0;
        }
    }
    cout << ans << endl;

    return 0;
}


#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <iostream>
#include <algorithm>
#include <map>
#include <set>
#include <queue>

using namespace std;

int m;
long long h[2], a[2], x[2], y[2];
long long p[2], z[2];

int main()
{

    cin >> m;
    for (int i = 0; i < 2; i++)
        cin >> h[i] >> a[i] >> x[i] >> y[i];

    for (int i = 0; i < 2; i++)
    {
        p[i] = -1;
        long long he = h[i];
        for (z[i] = 0; z[i] < 2 * m; z[i] ++)
        {
            if (he == a[i])
                break;
            he = (he * x[i] + y[i]) % m;
        }
        if (z[i] == 2 * m)
        {
            cout << -1 << endl;
            return 0;
        }
        for (p[i] = 1; p[i] < 2 * m; p[i] ++)
        {
            he = (he * x[i] + y[i]) % m;
            if (he == a[i])
                break;
        }
    }
    if (z[0] == z[1])
    {
        cout << z[0] << endl;
        return 0;
    }
    if (p[0] >= 2 * m || p[1] >= 2 * m)
    {
        cout << -1 << endl;
        return 0;
    }
    if (z[0] < z[1])
        swap(z[0], z[1]), swap(p[0], p[1]);
    long long ans = z[0];
    for (int i = 0; i < 2 * p[1]; i++)
    {
        if (ans % p[1] == z[1] % p[1])
        {
            cout << ans << endl;
            return 0;
        }
        ans += p[0];
    }
    cout << -1 << endl;

    return 0;
}


#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <iostream>
#include <algorithm>
#include <map>
#include <set>
#include <queue>

using namespace std;

int m;
long long h[2], a[2], x[2], y[2];
long long p[2], z[2];

int main()
{
    cin >> m >> h[0] >> a[0] >> x[0] >> y[0] >> h[1] >> a[1] >> x[1] >> y[1];
    for (int i = 0; i < 2; i++)
    {
        long long he = h[i];
        z[i] = -1;
        for (int j = 0; j < 2 * m; j++)
        {
            if (he == a[i])
            {
                z[i] = j;
                break;
            }
            he = (he * x[i] + y[i]) % m;
        }
        if (z[i] < 0)
        {
            cout << -1 << endl;
            return 0;
        }
        do
        {
            he = (he * x[i] + y[i]) % m;
            p[i] ++;
        } while (he != a[i] && p[i] < 2 * m);
        if (p[i] >= 2 * m)
        {
            cout << -1 << endl;
            return 0;
        }
    }
    if (z[0] < z[1])
        swap(p[0], p[1]), swap(z[0], z[1]);
    long long ans = z[0], inf = p[0] * p[1] + a[0];
    z[1] %= p[1];
    while (ans % p[1] != z[1])
    {
        ans += p[0];
        /*if (ans > 2 * inf)
        {
            cout << -1 << endl;
            return 0;
        }*/
    }
    cout << ans << endl;

    return 0;
}


#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <iostream>
#include <algorithm>
#include <map>
#include <set>
#include <queue>

using namespace std;

int m;
long long h[2], a[2], x[2], y[2];
long long p[2], z[2];

int main()
{
    cin >> m >> h[0] >> a[0] >> x[0] >> y[0] >> h[1] >> a[1] >> x[1] >> y[1];
    for (int i = 0; i < 2; i++)
    {
        long long he = h[i];
        z[i] = -1;
        for (int j = 0; j < m; j++)
        {
            if (he == a[i])
            {
                z[i] = j;
                break;
            }
            he = (he * x[i] + y[i]) % m;
        }
        if (z[i] < 0)
        {
            cout << -1 << endl;
            return 0;
        }
        do
        {
            he = (he * x[i] + y[i]) % m;
            p[i] ++;
        } while (he != a[i] && p[i] < 2 * m);
        if (p[i] >= 2 * m)
        {
            cout << -1 << endl;
            return 0;
        }
    }
    if (p[0] < p[1])
        swap(p[0], p[1]), swap(z[0], z[1]);
    long long ans = z[0], inf = p[0] * p[1] + a[0];
    while (ans % p[1] != z[1])
    {
        ans += p[0];
        if (ans > inf)
        {
            cout << -1 << endl;
            return 0;
        }
    }
    cout << ans << endl;

    return 0;
}


#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <iostream>
#include <algorithm>
#include <map>
#include <set>
#include <queue>

using namespace std;

int m;
long long h[2], a[2], x[2], y[2];
long long p[2], z[2];

int main()
{

    cin >> m;
    for (int i = 0; i < 2; i++)
        cin >> h[i] >> a[i] >> x[i] >> y[i];

    for (int i = 0; i < 2; i++)
    {
        p[i] = -1;
        long long he = h[i];
        for (z[i] = 0; z[i] < 2 * m; z[i] ++)
        {
            if (he == a[i])
                break;
            he = (he * x[i] + y[i]) % m;
        }
        if (z[i] == 2 * m)
        {
            cout << -1 << endl;
            return 0;
        }
        for (p[i] = 1; p[i] < 2 * m; p[i] ++)
        {
            he = (he * x[i] + y[i]) % m;
            if (he == a[i])
                break;
        }
    }
    if (p[0] >= 2 * m || p[1] >= 2 * m)
    {
        if (p[0] >= 2 * m)
            swap(z[0], z[1]), swap(p[0], p[1]);
        long long ans = z[0];
        while (ans < z[1])
            ans += p[0];
        if (ans == z[1])
            cout << ans << endl;
        else
            cout << -1 << endl;
        return 0;
    }
    if (z[0] < z[1])
        swap(z[0], z[1]), swap(p[0], p[1]);
    long long ans = z[0];
    for (int i = 0; i < 2 * p[1]; i++)
    {
        if (ans % p[1] == z[1] % p[1])
        {
            cout << ans << endl;
            return 0;
        }
        ans += p[0];
    }
    cout << -1 << endl;

    return 0;
}


#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <iostream>
#include <algorithm>
#include <map>
#include <set>
#include <queue>

using namespace std;

int m;
long long h[2], a[2], x[2], y[2];
long long p[2], z[2];

int main()
{
	cin >> m >> h[0] >> a[0] >> x[0] >> y[0] >> h[1] >> a[1] >> x[1] >> y[1];
	for (int i = 0; i < 2; i++)
	{
		long long he = h[i];
		z[i] = -1;
		for (int j = 0; j < m; j++)
		{
			if (he == a[i])
			{
				z[i] = j;
				break;
			}
			he = (he * x[i] + y[i]) % m;
		}
		if (z[i] < 0)
		{
			cout << -1 << endl;
			return 0;
		}
		do
		{
			he = (he * x[i] + y[i]) % m;
			p[i] ++;
		} while (he != a[i] && p[i] < 2 * m);
		if (p[i] >= 2 * m)
		{
			cout << -1 << endl;
			return 0;
		}
	}
	if (p[0] < p[1])
		swap(p[0], p[1]), swap(z[0], z[1]);
	long long ans = z[0];
	while (ans % p[1] != z[1])
		ans += p[0];
	cout << ans << endl;

	return 0;
}


#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <iostream>
#include <algorithm>
#include <map>
#include <set>
#include <queue>

using namespace std;

int m;
long long h[2], a[2], x[2], y[2];
long long p[2], z[2];

int main()
{
    cin >> m >> h[0] >> a[0] >> x[0] >> y[0] >> h[1] >> a[1] >> x[1] >> y[1];
    for (int i = 0; i < 2; i++)
    {
        long long he = h[i];
        z[i] = -1;
        for (int j = 0; j < 2 * m; j++)
        {
            if (he == a[i])
            {
                z[i] = j;
                break;
            }
            he = (he * x[i] + y[i]) % m;
        }
        if (z[i] < 0)
        {
            cout << -1 << endl;
            return 0;
        }
        do
        {
            he = (he * x[i] + y[i]) % m;
            p[i] ++;
        } while (he != a[i] && p[i] < 2 * m);
        if (p[i] >= 2 * m)
        {
            cout << -1 << endl;
            return 0;
        }
    }
    if (p[0] < p[1])
        swap(p[0], p[1]), swap(z[0], z[1]);
    long long ans = z[0], inf = p[0] * p[1] + a[0];
    while (ans % p[1] != z[1])
    {
        ans += p[0];
        if (ans > 2 * inf)
        {
            cout << -1 << endl;
            return 0;
        }
    }
    cout << ans << endl;

    return 0;
}


#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <iostream>
#include <algorithm>
#include <map>
#include <set>
#include <queue>

using namespace std;

int m;
long long h[2], a[2], x[2], y[2];
long long p[2], z[2];

int main()
{
    cin >> m >> h[0] >> a[0] >> x[0] >> y[0] >> h[1] >> a[1] >> x[1] >> y[1];
    for (int i = 0; i < 2; i++)
    {
        long long he = h[i];
        z[i] = -1;
        for (int j = 0; j < m; j++)
        {
            if (he == a[i])
            {
                z[i] = j;
                break;
            }
            he = (he * x[i] + y[i]) % m;
        }
        if (z[i] < 0)
        {
            cout << -1 << endl;
            return 0;
        }
        do
        {
            he = (he * x[i] + y[i]) % m;
            p[i] ++;
        } while (he != a[i] && p[i] < 2 * m);
        if (p[i] >= 2 * m)
        {
            cout << -1 << endl;
            return 0;
        }
    }
    if (p[0] < p[1])
        swap(p[0], p[1]), swap(z[0], z[1]);
    long long ans = z[0], inf = p[0] * p[1] + a[0];
    while (ans % p[1] != z[1])
    {
        ans += p[0];
        if (ans > 2 * inf)
        {
            cout << -1 << endl;
            return 0;
        }
    }
    cout << ans << endl;

    return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <cstdio>
#include <map>
#include <hash_map>
#include <string>
#include <iomanip>
#include <vector>
#include <memory.h>
#include <queue>
#include <set>
#include <stack>
#include <algorithm>
#include <math.h>
#include <sstream>
#include <functional>
#include <bitset>
#pragma comment (linker, "/STACK:167177216")
using namespace std;
#define mems(A, val) memset(A, val, sizeof(A))
#define mp(a, b) make_pair(a, b)
#define all(B) (B).begin(), (B).end()
#define forn(it, from, to) for(int it = from; it < to; ++it)
#define forit (it, coll) for(auto it = coll.begin(); it != coll.end(); ++it)
const int MAX = 2147483647;
const int MAXN = 100000 + 10000;
typedef long long LL;
const LL MOD = 1000000000 + 7;

int main()
{
#ifdef _DEBUG
    freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout);
#else
    //freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout);
    //freopen("f.in", "r", stdin); freopen("f.out", "w", stdout);
#endif

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

    vector<pair<LL, LL>> frog(m, mp(-1, -1));
    bool flag = false;
    LL lasth = -1;
    LL temph = h1;
    LL i = 0;
    while (!flag)
    {
        if (frog[temph].first == -1)
        {
            frog[temph].first = i;
        }
        else if (frog[temph].second == -1)
        {
            if (frog[lasth].second == -1)
            {
                frog[temph].second = frog[lasth].first - frog[temph].first + 1;
            }
            else
            {
                frog[temph].second = frog[lasth].second;
            }
        }
        else
        {
            flag = true;
        }
        i++;
        lasth = temph;
        temph = (x1*temph + y1) % m;
    }

    vector<pair<LL, LL>> flower(m, mp(-1, -1));
    flag = false;
    lasth = -1;
    temph = h2;
    i = 0;
    while (!flag)
    {
        if (flower[temph].first == -1)
        {
            flower[temph].first = i;
        }
        else if (flower[temph].second == -1)
        {
            if (flower[lasth].second == -1)
            {
                flower[temph].second = flower[lasth].first - flower[temph].first + 1;
            }
            else
            {
                flower[temph].second = flower[lasth].second;
            }
        }
        else
        {
            flag = true;
        }
        i++;
        lasth = temph;
        temph = (x2*temph +  y2) % m;
    }

    if (frog[a1].first == -1 || flower[a2].first == -1) cout << -1;
    else
    {
        LL a = frog[a1].first;
        LL p = frog[a1].second;
        LL b = flower[a2].first;
        LL q = flower[a2].second;

        if (p == -1 && q == -1)
        {
            if (a == b) cout << a;
            else cout << -1;
            return 0;
        }
        if (p == -1 && q != -1)
        {
            if (a > b && (a - b) % q == 0)
            {
                cout << a;
            }
            else cout << -1;
            return 0;
        }

        if (p != -1 && q == -1)
        {
            if (b > a && (b - a) % p == 0)
            {
                cout << b;
            }
            else cout << -1;
            return 0;
        }

        for (LL x = 0; x <= 1000100; x++)
        {
            LL znach = a + p*x;
            if (znach > b && (znach - b) % q == 0)
            {
                cout << a + p*x;
                return 0;
            }
        }

        cout << -1;
    }

    return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <cstdio>
#include <map>
#include <hash_map>
#include <string>
#include <iomanip>
#include <vector>
#include <memory.h>
#include <queue>
#include <set>
#include <stack>
#include <algorithm>
#include <math.h>
#include <sstream>
#include <functional>
#include <bitset>
#pragma comment (linker, "/STACK:167177216")
using namespace std;
#define mems(A, val) memset(A, val, sizeof(A))
#define mp(a, b) make_pair(a, b)
#define all(B) (B).begin(), (B).end()
#define forn(it, from, to) for(int it = from; it < to; ++it)
#define forit (it, coll) for(auto it = coll.begin(); it != coll.end(); ++it)
const int MAX = 2147483647;
const int MAXN = 100000 + 10000;
typedef long long LL;
const LL MOD = 1000000000 + 7;

int main()
{
#ifdef _DEBUG
    freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout);
#else
    //freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout);
    //freopen("f.in", "r", stdin); freopen("f.out", "w", stdout);
#endif

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

    vector<pair<LL, LL>> frog(m, mp(-1, -1));
    bool flag = false;
    LL lasth = -1;
    LL temph = h1;
    LL i = 0;
    while (!flag)
    {
        if (frog[temph].first == -1)
        {
            frog[temph].first = i;
        }
        else if (frog[temph].second == -1)
        {
            if (frog[lasth].second == -1)
            {
                frog[temph].second = frog[lasth].first - frog[temph].first + 1;
            }
            else
            {
                frog[temph].second = frog[lasth].second;
            }
        }
        else
        {
            flag = true;
        }
        i++;
        lasth = temph;
        temph = (x1*temph + y1) % m;
    }

    vector<pair<LL, LL>> flower(m, mp(-1, -1));
    flag = false;
    lasth = -1;
    temph = h2;
    i = 0;
    while (!flag)
    {
        if (flower[temph].first == -1)
        {
            flower[temph].first = i;
        }
        else if (flower[temph].second == -1)
        {
            if (flower[lasth].second == -1)
            {
                flower[temph].second = flower[lasth].first - flower[temph].first + 1;
            }
            else
            {
                flower[temph].second = flower[lasth].second;
            }
        }
        else
        {
            flag = true;
        }
        i++;
        lasth = temph;
        temph = (x2*temph +  y2) % m;
    }

    if (frog[a1].first == -1 || flower[a2].first == -1) cout << -1;
    else
    {
        LL a = frog[a1].first;
        LL p = frog[a1].second;
        LL b = flower[a2].first;
        LL q = flower[a2].second;

        for (int x = 0; x <= 1000000; x++)
        {
            LL znach = a + p*x;
            if ((znach - b) % q == 0)
            {
                cout << a + p*x;
                return 0;
            }
        }

        cout << -1;
    }

    return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <cstdio>
#include <map>
#include <hash_map>
#include <string>
#include <iomanip>
#include <vector>
#include <memory.h>
#include <queue>
#include <set>
#include <stack>
#include <algorithm>
#include <math.h>
#include <sstream>
#include <functional>
#include <bitset>
#pragma comment (linker, "/STACK:167177216")
using namespace std;
#define mems(A, val) memset(A, val, sizeof(A))
#define mp(a, b) make_pair(a, b)
#define all(B) (B).begin(), (B).end()
#define forn(it, from, to) for(int it = from; it < to; ++it)
#define forit (it, coll) for(auto it = coll.begin(); it != coll.end(); ++it)
const int MAX = 2147483647;
const int MAXN = 100000 + 10000;
typedef long long LL;
const LL MOD = 1000000000 + 7;

int main()
{
#ifdef _DEBUG
    freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout);
#else
    //freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout);
    //freopen("f.in", "r", stdin); freopen("f.out", "w", stdout);
#endif

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

    vector<pair<LL, LL>> frog(m, mp(-1, -1));
    bool flag = false;
    LL lasth = -1;
    LL temph = h1;
    LL i = 0;
    while (!flag)
    {
        if (frog[temph].first == -1)
        {
            frog[temph].first = i;
        }
        else if (frog[temph].second == -1)
        {
            if (frog[lasth].second == -1)
            {
                frog[temph].second = frog[lasth].first - frog[temph].first + 1;
            }
            else
            {
                frog[temph].second = frog[lasth].second;
            }
        }
        else
        {
            flag = true;
        }
        i++;
        lasth = temph;
        temph = (x1*temph + y1) % m;
    }

    vector<pair<LL, LL>> flower(m, mp(-1, -1));
    flag = false;
    lasth = -1;
    temph = h2;
    i = 0;
    while (!flag)
    {
        if (flower[temph].first == -1)
        {
            flower[temph].first = i;
        }
        else if (flower[temph].second == -1)
        {
            if (flower[lasth].second == -1)
            {
                flower[temph].second = flower[lasth].first - flower[temph].first + 1;
            }
            else
            {
                flower[temph].second = flower[lasth].second;
            }
        }
        else
        {
            flag = true;
        }
        i++;
        lasth = temph;
        temph = (x2*temph +  y2) % m;
    }

    if (frog[a1].first == -1 || flower[a2].first == -1) cout << -1;
    else
    {
        LL a = frog[a1].first;
        LL p = frog[a1].second;
        LL b = flower[a2].first;
        LL q = flower[a2].second;

        if (p == -1 && q == -1)
        {
            if (a == b) cout << a;
            else cout << -1;
            return 0;
        }
        if (p == -1 && q != -1)
        {
            if (a > b && (a - b) % q == 0)
            {
                cout << a;
            }
            else cout << -1;
            return 0;
        }

        if (p != -1 && q == -1)
        {
            if (b > a && (b - a) % p == 0)
            {
                cout << b;
            }
            else cout << -1;
            return 0;
        }

        for (int x = 0; x <= 1000000; x++)
        {
            LL znach = a + p*x;
            if ((znach - b) % q == 0)
            {
                cout << a + p*x;
                return 0;
            }
        }

        cout << -1;
    }

    return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <cstdio>
#include <map>
#include <hash_map>
#include <string>
#include <iomanip>
#include <vector>
#include <memory.h>
#include <queue>
#include <set>
#include <stack>
#include <algorithm>
#include <math.h>
#include <sstream>
#include <functional>
#include <bitset>
#pragma comment (linker, "/STACK:167177216")
using namespace std;
#define mems(A, val) memset(A, val, sizeof(A))
#define mp(a, b) make_pair(a, b)
#define all(B) (B).begin(), (B).end()
#define forn(it, from, to) for(int it = from; it < to; ++it)
#define forit (it, coll) for(auto it = coll.begin(); it != coll.end(); ++it)
const int MAX = 2147483647;
const int MAXN = 100000 + 10000;
typedef long long LL;
const LL MOD = 1000000000 + 7;

int main()
{
#ifdef _DEBUG
    freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout);
#else
    //freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout);
    //freopen("f.in", "r", stdin); freopen("f.out", "w", stdout);
#endif

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

    vector<pair<LL, LL>> frog(m, mp(-1, -1));
    bool flag = false;
    LL lasth = -1;
    LL temph = h1;
    LL i = 0;
    while (!flag)
    {
        if (frog[temph].first == -1)
        {
            frog[temph].first = i;
        }
        else if (frog[temph].second == -1)
        {
            if (frog[lasth].second == -1)
            {
                frog[temph].second = frog[lasth].first - frog[temph].first + 1;
            }
            else
            {
                frog[temph].second = frog[lasth].second;
            }
        }
        else
        {
            flag = true;
        }
        i++;
        lasth = temph;
        temph = (x1*temph + y1) % m;
    }

    vector<pair<LL, LL>> flower(m, mp(-1, -1));
    flag = false;
    lasth = -1;
    temph = h2;
    i = 0;
    while (!flag)
    {
        if (flower[temph].first == -1)
        {
            flower[temph].first = i;
        }
        else if (flower[temph].second == -1)
        {
            if (flower[lasth].second == -1)
            {
                flower[temph].second = flower[lasth].first - flower[temph].first + 1;
            }
            else
            {
                flower[temph].second = flower[lasth].second;
            }
        }
        else
        {
            flag = true;
        }
        i++;
        lasth = temph;
        temph = (x2*temph +  y2) % m;
    }

    if (frog[a1].first == -1 || flower[a2].first == -1) cout << -1;
    else
    {
        LL a = frog[a1].first;
        LL p = frog[a1].second;
        LL b = flower[a2].first;
        LL q = flower[a2].second;

        if (p == -1 && q == -1)
        {
            if (a == b) cout << a;
            else cout << -1;
            return 0;
        }
        if (p == -1 && q != -1)
        {
            if (a >= b && (a - b) % q == 0)
            {
                cout << a;
            }
            else cout << -1;
            return 0;
        }

        if (p != -1 && q == -1)
        {
            if (b >= a && (b - a) % p == 0)
            {
                cout << b;
            }
            else cout << -1;
            return 0;
        }

        for (LL x = 0; x <= 1000100; x++)
        {
            LL znach = a + p*x;
            if (znach >= b && (znach - b) % q == 0)
            {
                cout << a + p*x;
                return 0;
            }
        }

        cout << -1;
    }

    return 0;
}


// In the Name of Allah
// AD13

#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <algorithm>
using namespace std;

typedef long long ll;		//	typedef unsigned long long  ull;
typedef pair <ll, ll> pll;//	typedef pair <double, double> pdd;
typedef vector <int> VI;
#define MP make_pair
const int INF = 2147483647, MOD = 1000*1000*1000 + 7;
const double eps = 1e-8; // (eps < 1e-15) is the fact (1e-14)
#define For(i, n) for (int i = 0; i < (n); i++)
#define For1(i, n) for (int i = 1; i <= (n); i++)
//#define debug(x) { cerr << #x << " = _" << (x) << "_" << endl; }
void Error(string err) { cout << err; cerr << "_" << err << "_"; exit(0); }
int gcd(int a, int b) { return (b==0)? a: gcd(b, a%b); }
/*-------------------------------------------------------------------------------------*/

ll m;
bool can (ll h, ll a, ll x, ll y) {
	map <ll, bool> old;
	while (h != a) {
		if (old[h]) {
			return false;
		}
		old[h] = true;
		h = x * h + y;
		h %= m;
	}
	return true;
}
ll can2 (ll h, ll a, ll x, ll y) {
	ll ans = 0;
	while (h != a) {
		ans++;
		h = x * h + y;
		h %= m;
	}
	return ans;
}
ll can3 (ll a, ll x, ll y) {
	ll ans = 0;
	ll h = a;
	map <ll, bool> old;
	do {
		ans++;
		h = x * h + y;
		h %= m;
		if (old[h]) return -1;
		old[h] = true;
	}while (h != a);
	return ans;
}
/*_____________________________________________________________________________________*/

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

	ll ans = 0;

	if (can (h1, a1, x1, y1) && can (h2, a2, x2, y2)) {
		ll ans1 = can2 (h1, a1, x1, y1),
			ans2 = can2 (h2, a2, x2, y2);
		ll p1 = can3 (a1, x1, y1),
			p2 = can3 (a2, x2, y2);

		if (ans1 == ans2) ans = ans1;
		else {
			if (p1 == -1 || p2 == -1 || p1 == p2) ans = -1;
			else {
				ll x = ans2 - ans1;
				x /= (p1 - p2);
				//ans1 + x * p1 = ans2 + x * p2;
				//x * (p1 - p2) = ans2 - ans1;
				if (x > 0 && x * (p1 - p2) == ans2 - ans1) {
					ans = ans1 + x * p1;
				}
				else ans = -1;
			}
		}
	}
	else ans = -1;

	cout << ans << endl;

	return 0;
}
/*

*/


// In the Name of Allah
// AD13

#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <algorithm>
using namespace std;

typedef long long ll;		//	typedef unsigned long long  ull;
typedef pair <ll, ll> pll;//	typedef pair <double, double> pdd;
typedef vector <int> VI;
#define MP make_pair
const int INF = 2147483647, MOD = 1000*1000*1000 + 7;
const double eps = 1e-8; // (eps < 1e-15) is the fact (1e-14)
#define For(i, n) for (int i = 0; i < (n); i++)
#define For1(i, n) for (int i = 1; i <= (n); i++)
//#define debug(x) { cerr << #x << " = _" << (x) << "_" << endl; }
void Error(string err) { cout << err; cerr << "_" << err << "_"; exit(0); }
ll gcd(ll a, ll b) { return (b==0)? a: gcd(b, a%b); }
/*-------------------------------------------------------------------------------------*/

ll m;
ll can (ll h, ll a, ll x, ll y) {
	ll ans = 0;
	map <ll, bool> old;
	do {
		ans++;
		h = x * h + y;
		h %= m;
		if (old[h]) return -1;
		old[h] = true;
	}while (h != a);
	return ans;
}
/*_____________________________________________________________________________________*/

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

	ll ans = -1,
		ans1 = can (h1, a1, x1, y1),
		ans2 = can (h2, a2, x2, y2);

	if (ans1 > 0 && ans2 > 0) {
		if (ans1 == ans2) ans = ans1;
		else {
			ll p1 = can (a1, a1, x1, y1),
				p2 = can (a2, a2, x2, y2);

			if (p1 < 1 || p2 < 1 || p1 == p2);// ans = -1;
			else {
				if (ans1 < ans2) { swap (ans1, ans2); swap (p1, p2); }
				//for (ll x = 1; ; x++) {
				//	//ans1 + x * p1 = ans2 + y * p2;
				//	ll a = ans1 + x * p1 - ans2;
				//	ll y = a / p2;
				//	if (y * p2 == a) {
				//		ans = ans1 + x * p1;
				//		break;
				//	}
				//	if (ans1 + x * p1 > ans2 + y * p2) break;
				//}
				ll kmm = p1 * p2 / gcd (p1, p2);
				ll mxa1  = ans1 + kmm, mxa2 = ans2 + kmm;

				ll cnter = 0;
				const ll one = 1;
				map <ll, bool> old;
				while (ans1 != ans2) {
					cnter ++ ;
					ll x = ans1 - ans2;
					if (cnter > 500111 )//|| old[x] || ans1 > mxa1 || ans2 > mxa2)
					{ ans1 = -1; break; }
					old[x] = true;
					if (x < 0) x = -x;
					if (ans1 < ans2) ans1 += max (one, x / p1) * p1;
					else ans2 += max (one, x / p2) * p2;
				}

				ans = ans1;
			}
		}
	}

	cout << ans << endl;

	return 0;
}
/*

*/


// In the Name of Allah
// AD13

#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <algorithm>
using namespace std;

typedef long long ll;		//	typedef unsigned long long  ull;
typedef pair <ll, ll> pll;//	typedef pair <double, double> pdd;
typedef vector <int> VI;
#define MP make_pair
const int INF = 2147483647, MOD = 1000*1000*1000 + 7;
const double eps = 1e-8; // (eps < 1e-15) is the fact (1e-14)
#define For(i, n) for (int i = 0; i < (n); i++)
#define For1(i, n) for (int i = 1; i <= (n); i++)
//#define debug(x) { cerr << #x << " = _" << (x) << "_" << endl; }
void Error(string err) { cout << err; cerr << "_" << err << "_"; exit(0); }
ll gcd(ll a, ll b) { return (b==0)? a: gcd(b, a%b); }
/*-------------------------------------------------------------------------------------*/

bool oldx[1000001];
ll m;
ll can (ll h, ll a, ll x, ll y) {
	ll ans = 0;
	For (i, 1000001) oldx[i] = false;
	do {
		ans++;
		h = x * h + y;
		h %= m;
		if (oldx[h]) return -1;
		oldx[h] = true;
	}while (h != a);
	return ans;
}
/*_____________________________________________________________________________________*/

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

	ll ans = -1,
		ans1 = can (h1, a1, x1, y1),
		ans2 = can (h2, a2, x2, y2);

	if (ans1 > 0 && ans2 > 0) {

		if (ans1 == ans2) ans = ans1;
		else {
			ll p1 = can (a1, a1, x1, y1),
				p2 = can (a2, a2, x2, y2);

			if (p1 < 1 && p2 < 1) ans = -1;
			else {
				if (p1 < 1) p1 = 0;
				if (p2 < 1) p2 = 0;

				ll cnter = 0;
				const ll one = 1;
				if (p1 > 1 && p2 > 1) {
					while (ans1 != ans2) {
						cnter ++ ;
						ll x = ans1 - ans2;
						if (cnter > 2101000 )//|| old[x])
						{ ans1 = -1; break; }
						if (x < 0) x = -x;

						if (ans1 < ans2) ans1 += max (one, x / p1) * p1;
						else ans2 += max (one, x / p2) * p2;
					}
				}
				else {
					while (ans1 != ans2) {
						cnter ++ ;
						ll x = ans1 - ans2;
						if (cnter > 2101000 )//|| old[x])
						{ ans1 = -1; break; }
						if (x < 0) x = -x;

						if (ans1 < ans2) ans1 += p1;
						else ans2 += p2;
					}
				}

				ans = ans1;
			}
		}
	}

	cout << ans << endl;

	return 0;
}
/*

*/


// In the Name of Allah
// AD13

#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <algorithm>
using namespace std;

typedef long long ll;		//	typedef unsigned long long  ull;
typedef pair <ll, ll> pll;//	typedef pair <double, double> pdd;
typedef vector <int> VI;
#define MP make_pair
const int INF = 2147483647, MOD = 1000*1000*1000 + 7;
const double eps = 1e-8; // (eps < 1e-15) is the fact (1e-14)
#define For(i, n) for (int i = 0; i < (n); i++)
#define For1(i, n) for (int i = 1; i <= (n); i++)
//#define debug(x) { cerr << #x << " = _" << (x) << "_" << endl; }
void Error(string err) { cout << err; cerr << "_" << err << "_"; exit(0); }
ll gcd(ll a, ll b) { return (b==0)? a: gcd(b, a%b); }
/*-------------------------------------------------------------------------------------*/

int oldx[1000111];
ll m;
ll can (ll h, ll a, ll x, ll y) {
	static int canCnt = 0;
	canCnt++;
	ll ans = 0;
	do {
		ans++;
		h = x * h + y;
		h %= m;
		if (oldx[h] == canCnt) return -1;
		oldx[h] = canCnt;
	}while (h != a);
	return ans;
}
/*_____________________________________________________________________________________*/

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

	ll ans = -1,
		ans1 = can (h1, a1, x1, y1),
		ans2 = can (h2, a2, x2, y2);

	if (ans1 > 0 && ans2 > 0) {

		if (ans1 == ans2) ans = ans1;
		else {
			ll p1 = can (a1, a1, x1, y1),
				p2 = can (a2, a2, x2, y2);

			if (p1 < 1 && p2 < 1) ans = -1;
			else {
				if (p1 < 1) p1 = 0;
				if (p2 < 1) p2 = 0;
				if (ans1 < ans2) { swap (ans1, ans2); swap (p1, p2); }

				ll cnter = 0;
				const ll one = 1;
				//map <ll, bool> old;
				while (ans1 != ans2) {
					cnter ++ ;
					ll x = ans1 - ans2;
					if (cnter > 1888000 )//|| old[x])
					{ ans1 = -1; break; }
					//old[x] = true;
					if (x < 0) x = -x;
					if (ans1 < ans2) ans1 += p1;
					else ans2 += p2;
				}

				ans = ans1;
			}
		}
	}

	cout << ans << endl;

	return 0;
}
/*

*/


// In the Name of Allah
// AD13

#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <algorithm>
using namespace std;

typedef long long ll;		//	typedef unsigned long long  ull;
typedef pair <ll, ll> pll;//	typedef pair <double, double> pdd;
typedef vector <int> VI;
#define MP make_pair
const int INF = 2147483647, MOD = 1000*1000*1000 + 7;
const double eps = 1e-8; // (eps < 1e-15) is the fact (1e-14)
#define For(i, n) for (int i = 0; i < (n); i++)
#define For1(i, n) for (int i = 1; i <= (n); i++)
//#define debug(x) { cerr << #x << " = _" << (x) << "_" << endl; }
void Error(string err) { cout << err; cerr << "_" << err << "_"; exit(0); }
ll gcd(ll a, ll b) { return (b==0)? a: gcd(b, a%b); }
/*-------------------------------------------------------------------------------------*/

ll m;
ll can (ll h, ll a, ll x, ll y) {
	ll ans = 0;
	map <ll, bool> old;
	do {
		ans++;
		h = x * h + y;
		h %= m;
		if (old[h]) return -1;
		old[h] = true;
	}while (h != a);
	return ans;
}
/*_____________________________________________________________________________________*/

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

	ll ans = -1,
		ans1 = can (h1, a1, x1, y1),
		ans2 = can (h2, a2, x2, y2);

	if (ans1 > 0 && ans2 > 0) {
		if (ans1 == ans2) ans = ans1;
		else {
			ll p1 = can (a1, a1, x1, y1),
				p2 = can (a2, a2, x2, y2);

			if (p1 < 1 || p2 < 1 || p1 == p2);// ans = -1;
			else {
				if (ans1 < ans2) { swap (ans1, ans2); swap (p1, p2); }
				//for (ll x = 1; ; x++) {
				//	//ans1 + x * p1 = ans2 + y * p2;
				//	ll a = ans1 + x * p1 - ans2;
				//	ll y = a / p2;
				//	if (y * p2 == a) {
				//		ans = ans1 + x * p1;
				//		break;
				//	}
				//	if (ans1 + x * p1 > ans2 + y * p2) break;
				//}
				ll kmm = p1 * p2 / gcd (p1, p2);
				ll mxa1  = ans1 + kmm, mxa2 = ans2 + kmm;

				ll cnter = 0;
				const ll one = 1;
				map <ll, bool> old;
				while (ans1 != ans2) {
					cnter ++ ;
					ll x = ans1 - ans2;
					if (cnter > 3000111 || old[x] || ans1 > mxa1 || ans2 > mxa2) { ans1 = -1; break; }
					old[x] = true;
					if (x < 0) x = -x;
					if (ans1 < ans2) ans1 += max (one, x / p1) * p1;
					else ans2 += max (one, x / p2) * p2;
				}

				ans = ans1;
			}
		}
	}

	cout << ans << endl;

	return 0;
}
/*

*/


// In the Name of Allah
// AD13

#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <algorithm>
using namespace std;

typedef long long ll;		//	typedef unsigned long long  ull;
typedef pair <int, int> pii;//	typedef pair <double, double> pdd;
typedef vector <int> VI;
#define MP make_pair
const int INF = 2147483647, MOD = 1000*1000*1000 + 7;
const double eps = 1e-8; // (eps < 1e-15) is the fact (1e-14)
#define For(i, n) for (int i = 0; i < (n); i++)
#define For1(i, n) for (int i = 1; i <= (n); i++)
//#define debug(x) { cerr << #x << " = _" << (x) << "_" << endl; }
void Error(string err) { cout << err; cerr << "_" << err << "_"; exit(0); }
int gcd(int a, int b) { return (b==0)? a: gcd(b, a%b); }
/*-------------------------------------------------------------------------------------*/

const int sz = 100 * 1000;

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

	int ans = 0;
	map <pii, bool> mp;

	while (h1 != a1 || h2 != a2) {
		ans++;
		if (mp[MP(h1,h2)] ) {
			ans = -1;
			break;
		}
		mp[MP(h1, h2)] = true;
		h1 = x1 * h1 + y1;
		h1 %= m;
		h2 = x2 * h2 + y2;
		h2 %= m;
	}

	cout << ans << endl;

	return 0;
}
/*

*/


// In the Name of Allah
// AD13

#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <algorithm>
using namespace std;

typedef long long ll;		//	typedef unsigned long long  ull;
typedef pair <ll, ll> pll;//	typedef pair <double, double> pdd;
typedef vector <int> VI;
#define MP make_pair
const int INF = 2147483647, MOD = 1000*1000*1000 + 7;
const double eps = 1e-8; // (eps < 1e-15) is the fact (1e-14)
#define For(i, n) for (int i = 0; i < (n); i++)
#define For1(i, n) for (int i = 1; i <= (n); i++)
//#define debug(x) { cerr << #x << " = _" << (x) << "_" << endl; }
void Error(string err) { cout << err; cerr << "_" << err << "_"; exit(0); }
ll gcd(ll a, ll b) { return (b==0)? a: gcd(b, a%b); }
/*-------------------------------------------------------------------------------------*/

bool oldx2[4][1000111];
ll m;
ll can (ll h, ll a, ll x, ll y) {
	static int canCnt = 0;
	bool * oldx = &oldx2[canCnt][0];
	canCnt++;
	ll ans = 0;
	//For (i, 1000111) oldx[i] = false;
	do {
		ans++;
		h = x * h + y;
		h %= m;
		if (oldx[h]) return -1;
		oldx[h] = true;
	}while (h != a);
	return ans;
}
/*_____________________________________________________________________________________*/

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

	ll ans = -1,
		ans1 = can (h1, a1, x1, y1),
		ans2 = can (h2, a2, x2, y2);

	if (ans1 > 0 && ans2 > 0) {

		if (ans1 == ans2) ans = ans1;
		else {
			ll p1 = can (a1, a1, x1, y1),
				p2 = can (a2, a2, x2, y2);

			if (p1 < 1 && p2 < 1) ans = -1;
			else {
				if (p1 < 1) p1 = 0;
				if (p2 < 1) p2 = 0;
				if (ans1 < ans2) { swap (ans1, ans2); swap (p1, p2); }

				ll cnter = 0;
				const ll one = 1;
				map <ll, bool> old;
				while (ans1 != ans2) {
					cnter ++ ;
					ll x = ans1 - ans2;
					if (old[x])
					{ ans1 = -1; break; }
					old[x] = true;
					if (x < 0) x = -x;
					if (ans1 < ans2) ans1 += p1;
					else ans2 += p2;
				}

				ans = ans1;
			}
		}
	}

	cout << ans << endl;

	return 0;
}
/*

*/


// In the Name of Allah
// AD13

#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <algorithm>
using namespace std;

typedef long long ll;		//	typedef unsigned long long  ull;
typedef pair <ll, ll> pll;//	typedef pair <double, double> pdd;
typedef vector <int> VI;
#define MP make_pair
const int INF = 2147483647, MOD = 1000*1000*1000 + 7;
const double eps = 1e-8; // (eps < 1e-15) is the fact (1e-14)
#define For(i, n) for (int i = 0; i < (n); i++)
#define For1(i, n) for (int i = 1; i <= (n); i++)
//#define debug(x) { cerr << #x << " = _" << (x) << "_" << endl; }
void Error(string err) { cout << err; cerr << "_" << err << "_"; exit(0); }
ll gcd(ll a, ll b) { return (b==0)? a: gcd(b, a%b); }
/*-------------------------------------------------------------------------------------*/

int oldx[1000111];
ll m;
ll can (ll h, ll a, ll x, ll y) {
	static int canCnt = 0;
	canCnt++;
	ll ans = 0;
	do {
		ans++;
		h = x * h + y;
		h %= m;
		if (oldx[h] == canCnt) return -1;
		oldx[h] = canCnt;
	}while (h != a);
	return ans;
}
/*_____________________________________________________________________________________*/

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

	ll ans = -1,
		ans1 = can (h1, a1, x1, y1),
		ans2 = can (h2, a2, x2, y2);

	if (ans1 > 0 && ans2 > 0) {

		if (ans1 == ans2) ans = ans1;
		else {
			ll p1 = can (a1, a1, x1, y1),
				p2 = can (a2, a2, x2, y2);

			if (p1 < 1 && p2 < 1) ans = -1;
			else {
				if (p1 < 1) p1 = 0;
				if (p2 < 1) p2 = 0;

				ll cnter = 0;
				const ll one = 1;
				//map <ll, bool> old;
				while (ans1 != ans2) {
					cnter ++ ;
					ll x = ans1 - ans2;
					if (cnter > 2555000 )//|| old[x])
					{ ans1 = -1; break; }
					//old[x] = true;
					if (x < 0) x = -x;
					if (ans1 < ans2) ans1 += p1;
					else ans2 += p2;
				}

				ans = ans1;
			}
		}
	}

	cout << ans << endl;

	return 0;
}
/*

*/


// In the Name of Allah
// AD13

#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <algorithm>
using namespace std;

typedef long long ll;		//	typedef unsigned long long  ull;
typedef pair <ll, ll> pll;//	typedef pair <double, double> pdd;
typedef vector <int> VI;
#define MP make_pair
const int INF = 2147483647, MOD = 1000*1000*1000 + 7;
const double eps = 1e-8; // (eps < 1e-15) is the fact (1e-14)
#define For(i, n) for (int i = 0; i < (n); i++)
#define For1(i, n) for (int i = 1; i <= (n); i++)
//#define debug(x) { cerr << #x << " = _" << (x) << "_" << endl; }
void Error(string err) { cout << err; cerr << "_" << err << "_"; exit(0); }
ll gcd(ll a, ll b) { return (b==0)? a: gcd(b, a%b); }
/*-------------------------------------------------------------------------------------*/

bool oldx[1000111];
ll m;
ll can (ll h, ll a, ll x, ll y) {
	static int canCnt = 0;
	canCnt++;
	ll ans = 0;
	For (i, 1000111) oldx[i] = false;
	do {
		ans++;
		h = x * h + y;
		h %= m;
		if (oldx[h]) return -1;
		oldx[h] = true;
	}while (h != a);
	return ans;
}
/*_____________________________________________________________________________________*/

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

	ll ans = -1,
		ans1 = can (h1, a1, x1, y1),
		ans2 = can (h2, a2, x2, y2);

	if (ans1 > 0 && ans2 > 0) {

		if (ans1 == ans2) ans = ans1;
		else {
			ll p1 = can (a1, a1, x1, y1),
				p2 = can (a2, a2, x2, y2);

			if (p1 < 1 && p2 < 1) ans = -1;
			else {
				if (p1 < 1) p1 = 0;
				if (p2 < 1) p2 = 0;
				if (ans1 < ans2) { swap (ans1, ans2); swap (p1, p2); }

				ll cnter = 0;
				const ll one = 1;
				//map <ll, bool> old;
				while (ans1 != ans2) {
					cnter ++ ;
					ll x = ans1 - ans2;
					//if (cnter > 2000111 )//|| old[x])
					//{ ans1 = -1; break; }
					//old[x] = true;
					if (x < 0) x = -x;
					if (ans1 < ans2) ans1 += p1;
					else ans2 += p2;
				}

				ans = ans1;
			}
		}
	}

	cout << ans << endl;

	return 0;
}
/*

*/