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

#define rep(i, n) REP(i, 0, n)
#define ALL(v) v.begin(), v.end()
#define MSG(a) cout << #a << " " << a << endl;
#define REP(i, x, n) for (int i = x; i < n; i++)
#define OP(m) cout << m << endl
typedef long long ll;
typedef unsigned long long ull;

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

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

int main()
{
    ll a1, m1, a2, m2, a3, m3;
    while (cin >> a1 >> m1 >> a2 >> m2 >> a3 >> m3, a1)
    {
        ll c1, c2, c3;
        ll sum = 1;
        for (int i = 1; i < m1; i++)
        {
            sum = (sum * a1) % m1;
            if (sum == 1)
            {
                c1 = i;
                break;
            }
        }
        sum = 1;
        for (int i = 1; i < m2; i++)
        {
            sum = (sum * a2) % m2;
            if (sum == 1)
            {
                c2 = i;
                break;
            }
        }
        sum = 1;
        for (int i = 1; i < m3; i++)
        {
            sum = (sum * a3) % m3;
            if (sum == 1)
            {
                c3 = i;
                break;
            }
        }
        OP(lcm(lcm(c1, c2), c3));
    }

    return 0;
}
