#include <iostream>
#include <algorithm>

using namespace std;

typedef long long ll;

ll lcm(ll x, ll y)
{
    return x * y / __gcd(x, y);
}

int count_step(int a, int m)
{
    ll x = 1, step = 0;
    do {
        x = (a * x) % m;
        step++;
    } while(x != 1);
    return step;
}

int main()
{
    int a1,m1,a2,m2,a3,m3;
    while(cin >> a1 >> m1 >> a2 >> m2 >> a3 >> m3 && a1) {
        cout << lcm(count_step(a1, m1), lcm(count_step(a2, m2), count_step(a3, m3))) << endl;
    }
}