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

using ll = long long;

ll gcd(ll a, ll b) {
    if(b == 0) {
        return a;
    }
    return gcd(b, a % b);
}

ll lcm(ll a, ll b) {
    int g = gcd(a, b);
    a /= g;
    b /= g;
    return a * b * g;
}

int main() {
    vector<ll> a(3), m(3);
    while(true) {
        for(int i=0; i<3; ++i) {
            cin >> a[i] >> m[i];
        }
        if(m[0] == 0) {
            break;
        }
        vector<ll> cnt(3, 1);
        for(int i=0; i<3; ++i) {
            ll now = (a[i] % m[i]);
            while(now != 1) {
                cnt[i]++;
                (now *= a[i]) %= m[i];
            }
        }
        ll res = lcm(cnt[0], cnt[1]);
        res = lcm(res, cnt[2]);
        cout << res << endl;
    }
}
