#include <iostream>
#include <algorithm>

using namespace std;

inline int lcm(int n, int m)
{
    return m * n / __gcd(n, m);
}

int main()
{
    int a[3], m[3];

    while (true) {
        int sum = 0;
        for (int i = 0; i < 3; i++) {
            cin >> a[i] >> m[i];
            sum += (a[i] + m[i]);
        }
        if (sum == 0)
            break;

        int x[3] = {1, 1, 1}, cnt = 0;
        int t[3] = {0};

        for (int i = 0; i < 3; i++) {
            while (true) {
                x[i] = (a[i] * x[i]) % m[i];

                t[i]++;
                if (x[i] == 1)
                    break;
            }
        }

        cout << lcm(lcm(t[0], t[1]), t[2]) << endl;
    }

    return 0;
}