#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef pair<ll,ll> pii;
#define rep(i,n) for(ll i=0;i<(ll)(n);i++)
#define all(a)  (a).begin(),(a).end()
#define pb push_back
#define INF 999999999
#define eps 1e-9

ll a1,b1,a2,b2,a3,b3;
int main(){
    while(cin>>a1>>b1>>a2>>b2>>a3>>b3&&a1&&b1&&a2&&b2&&a3&&b3){
        ll a=0,b=0,c=0;
        ll x=1,y=1,z=1;
        while(1){
            a++;
            x=(a1*x)%b1;
            if(x==1)break;
        }
        while(1){
            b++;
            y=(a2*y)%b2;
            if(y==1)break;
        }
        while(1){
            c++;
            z=(a3*z)%b3;
            if(z==1)break;
        }
        ll m=b/__gcd(a,b)*a;
        cout<<c/__gcd(m,c)*m<<endl;
    }
}