#include<iostream>
using namespace std;
typedef long long ll;
ll ax,mx,ay,my,az,mz,tmp,a,b,c,nx,ny,nz;
ll compute(ll x,ll y){
  a=x;
  b=y;
  c=x%y;
  while(c!=0){
    a=b;
    b=c;
    c=a%b;
  }
  return x/b*y;
}
ll compute2(ll a,ll m){
  b=1;
  c=1;
  b=b*a%m;
  while(b!=1){
    b=b*a%m;
    c++;
  }
  return c;
}

int main(){
  while(cin>>ax>>mx>>ay>>my>>az>>mz&&mx){
    nx=compute2(ax,mx);
    ny=compute2(ay,my);
    nz=compute2(az,mz);
    cout<<compute(nx,compute(ny,nz))<<endl;
  }
  return 0;
}