#include <iostream>
#include <algorithm>
#include <cstdio>

using namespace std;

#define rep(i,n) for(int i=0; i<int(n); i++)

namespace std {
    template<typename T>
    T lcm(T a,T b){
        return a/__gcd(a,b)*b;
    }
}
 
int main(){
    long long cyc[3], a[3], m[3];

#define in(i) ,&a[i],&m[i]
#define o(i) |a[i]|m[i]
    
    while(scanf("%lld%lld%lld%lld%lld%lld" in(0)in(1)in(2)),1|a[0]|a[1]|a[2]){
        rep(i,3){
            cyc[i]=0;
            int cnt=1,k=1;
            while(1){
                k*=a[i];
                k%=m[i];
                if(k==1){
                    cyc[i]=cnt; break;
                }
                cnt++;
            }
        }
        cout << lcm(lcm(cyc[0],cyc[1]),cyc[2]) << endl;
    }
}