#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cmath>
#include<math.h>
#include<string>
#include<string.h>
#include<stack>
#include<queue>
#include<vector>
#include<utility>
#include<set>
#include<map>
#include<stdlib.h>
#include<iomanip>

using namespace std;

#define ll long long
#define ld long double
#define EPS 0.0000000001
#define INF 1e9
#define MOD 1000000007
#define rep(i,n) for(i=0;i<n;i++)
#define loop(i,a,n) for(i=a;i<n;i++)
#define all(in) in.begin(),in.end()
#define shosu(x) fixed<<setprecision(x)

typedef vector<int> vi;
typedef pair<int,int> pii;

int main(void) {
  int i,j;
  ll a1,a2,a3,m1,m2,m3;
  while(1){
    cin>>a1>>m1>>a2>>m2>>a3>>m3;
    if(a1+a2+a3+m1+m2+m3==0)break;
    ll x,y,z,t1,t2,t3;
    x=y=z=1;
    t1=t2=t3=0;
    do{
      x=(a1*x)%m1;
      t1++;
    }while(x!=1);
    do{
      y=(a2*y)%m2;
      t2++;
    }while(y!=1);
    do{
      z=(a3*z)%m3;
      t3++;
    }while(z!=1);
    ll ans=t1*t2/__gcd(t1,t2);
    ans=ans*t3/__gcd(ans,t3);
    cout<<ans<<endl;
  }
}