#include<iostream>
using namespace std;

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

int main()
{
	int a,aa,b,bb,c,cc;
	while(cin>>a>>aa>>b>>bb>>c>>cc,a) 
	{
		int x=1,y=1,z=1;
		long long xx=0,yy=0,zz=0,ans=0;

		do {
			x=a*x%aa;
			xx++;
		} while(x!=1);
		do {
			y=b*y%bb;
			yy++;
		} while(y!=1);
		do {
			z=c*z%cc;
			zz++;
		} while(z!=1);

		ans=xx*yy/gcd(xx,yy);
		ans=ans*zz/gcd(ans,zz);

		cout<<ans<<endl;

	}
  return 0;
}