#include<bits/stdc++.h>
using namespace std;
#define int long long

int a[3],m[3];

int gcd(int a,int b){
    return b?gcd(b,a%b):a;
}
int lcm(int a,int b){
    return a/gcd(a,b)*b;
}

void solve(){
    int ans=1;
    for(int i=0;i<3;i++){
        int cnt=0;
        int p=1;
        do{
            p=(p*a[i])%m[i];
            cnt++;
        }while(p!=1);
        ans=lcm(ans,cnt);
    }

    cout<<ans<<endl;
}

signed main(){
    while(true){
        bool flag=false;
        for(int i=0;i<3;i++){
            cin>>a[i]>>m[i];
            flag|=a[i]|m[i];
        }

        if(flag)solve();
        else break;
    }
    return 0;
}