#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(){
    int a[3],m[3];
    int cyc[3]={};

#define iin(i) ,&a[i],&m[i]

    while(scanf("%d%d%d%d%d%d" iin(0)iin(1)iin(2)),a[0]|m[0]|a[1]|m[1]|a[2]|m[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;
    }
}