#include <iostream>
using namespace std;

typedef long long LL;

LL lcm(LL a, LL b) {
    LL ret = a * b;

    if (a < b) swap(a, b);
    while (b > 0) {
        LL t = b;
        b = a%b;
        a = t;
    }

    return ret / a;
}

int main() {
    int a1, a2, a3, m1, m2, m3;
    int a[3], m[3];

    while (cin >> a[0] >> m[0] >> a[1] >> m[1] >> a[2] >> m[2]) {
        if ((a[0]|a[1]|a[2]|m[0]|m[1]|m[2]) == 0) {
            break;
        }

        LL ret = 1;

        for (int j = 0; j < 3; ++j) {
            int x = a[j]%m[j], cycle = 1;
            while (x > 1) {
                ++cycle;
                x = (a[j]*x)%m[j];
            }

            ret = lcm(ret, cycle);
        }

        cout << ret << endl;
    } 
    return 0;
}