#include<bits/stdc++.h>
#define int long long
using namespace std;
int lcm(int a,int b){return a/__gcd(a,b)*b;}
int a1,a2,a3,m1,m2,m3,a,b,c,x,y,z;
main(){
  while(cin>>a1>>m1>>a2>>m2>>a3>>m3,a1){
    a=b=c=0;
    x=y=z=1;
    while(!(a&&x==1))x=(a1*x)%m1,a++;
    while(!(b&&y==1))y=(a2*y)%m2,b++;
    while(!(c&&z==1))z=(a3*z)%m3,c++;
    cout<<lcm(a,lcm(b,c))<<endl;
  }
}