#define _CRT_SECURE_NO_WARNINGS
#include"bits/stdc++.h"
#ifdef _DEBUG
#define DBG(n) n
#else
#define DBG(n)
#endif
#define INF         1e9
#define INFLL       1e18
#define EPS         1e-9
#define REP(i,n)    for(ll i=0,i##_len=(n);i<i##_len;++i)
#define REP1(i,n)   for(ll i=1,i##_len=(n);i<=i##_len;++i)
#define REPR(i,n)   for(ll i=(n)-1;i>=0;--i)
#define REPR1(i,n)  for(ll i=(n);i>0;--i)
#define REPC(i,obj) for(auto i:obj)
#define ALL(obj)    (obj).begin(),(obj).end()
#define SETP(n)     cout<<fixed<<setprecision(n)
using namespace std;
using ll = long long;
template<typename T = ll>inline T in() { T ret; cin >> ret; return ret; }

ll gcd(ll x, ll y)
{
	return y == 0 ? x : gcd(y, x % y);
}
ll lcm(ll x, ll y)
{
	return x / gcd(x, y) * y;
}
signed main()
{
	ll a[3], m[3];
	while (cin >> a[0] >> m[0] >> a[1] >> m[1] >> a[2] >> m[2], a[0]) {
		ll cood[3] = { 1,1,1 };
		ll cnt[3];
		REP(i, 3) {
			ll foo = 0;
			while (true) {
				cood[i] = a[i] * cood[i] % m[i];
				++foo;
				if (cood[i] == 1) {
					cnt[i] = foo;
					break;
				}
			}
		}
		cout << lcm(lcm(cnt[0], cnt[1]), cnt[2]) << endl;
	}
}
