#include<iostream>
using namespace std;
long gcd(long a,long b){return b==0?a:gcd(b,a%b);}
long lcm(long a,long b){return a/gcd(a,b)*b;}
int main(){
long a1,m1,a2,m2,a3,m3,x,y,z,n1,n2,n3;
while(cin>>a1){
cin>>m1>>a2>>m2>>a3>>m3;
if(a1==0)break;
for(n1=1,x=1;;){
x=(a1*x)%m1;
if(x==1)break;
n1++;
}
for(n2=1,y=1;;){
y=(a2*y)%m2;
if(y==1)break;
n2++;
}
for(n3=1,z=1;;){
z=(a3*z)%m3;
if(z==1)break;
n3++;
}
cout<<lcm(lcm(n1,n2),n3)<<endl;
}
return 0;
}