#include<iostream>
using namespace std;

long long gcd(long long x,long long y) {
    if(y == 0) return x;
    return gcd(y, x%y);
}

long long lcm(long long x,long long y) {
    return x*y/gcd(x,y);
}

int main() {
    int a[3],m[3];
    while(cin>>a[0]>>m[0]>>a[1]>>m[1]>>a[2]>>m[2], a[0]) {
        int res[3];
        for(int i=0; i<3; ++i) {
            int p = a[i]%m[i], cc = 1;
            while(p != 1) {
                p = (a[i]*p)%m[i];
                cc++;
            }
            res[i] = cc;
        }
        cout<<lcm(res[0], lcm(res[1], res[2]))<<endl;
    }
}