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

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;
}

int get(int a,int m){
    int c=0,x=1;
    do{
        x=x*a%m;
        c++;
    }while(x!=1);

    return c;
}

signed main(){
    int a,b,c,l,m,n;
    while(cin>>a>>l>>b>>m>>c>>n){
        if(!a&&!b&&!c&&!l&&!m&&!n)break;
        cout<<lcm(get(a,l),lcm(get(b,m),get(c,n)))<<endl;
    }
    return 0;
}