#include<iostream>
#include<string>
#include<algorithm>
#include<vector>
#include<map>
#include<cstring>
using namespace std;

#define rep(i,n) for(int i=0;i<n;i++)
#define REP(n) rep(i,n)
#define all(n) n.begin(),n.end()

long long gcd(long long x, long long y)
{
    if(x < y ) swap(x, y);
    if(y == 0) return x;
    x = x % y;
    return gcd(x, y);
}

const int MAX = 3;

long long m[MAX], a[MAX], jump[MAX];

int main()
{
    long long px, nx;
    while(cin >> a[0] >> m[0] >> a[1] >> m[1] >> a[2] >> m[2] && m[0])
    {
        REP(3)
        {
            jump[i] = 0;
            px = 1; nx = 0;
            while(nx != 1)
            {
                nx = a[i] * px % m[i];
                jump[i]++;
                px = nx;
            }
        }
        long long lcm1 = jump[0] * jump[1] / gcd(jump[0], jump[1]);
        long long ans = lcm1 * jump[2] / gcd(lcm1, jump[2]);
        cout << ans << endl;

    }
    
    return 0;
}