#include<iostream>
using namespace std;

#define rep(i, n) for(int i=0; i<n; ++i)

typedef long long ll;

ll gcd(ll m, ll n) {
	if(m<n) return gcd(n, m);
	ll r = m%n;
	return (!r ? n : gcd(n, r));
}

int main() {
	ll a[3], m[3], c[3];
	while(1) {
		rep(i, 3) cin >> a[i] >> m[i];
		if(!a[0] && !m[0] &&!a[1] && !m[1] && !a[2] && !m[2]) break;
		rep(i, 3) {
			ll p = a[i] % m[i]; c[i] = 1;
			while(p != 1) p = (p * a[i]) % m[i], ++c[i];
		}
		ll g01 = gcd(c[0], c[1]), g12 = gcd(c[1], c[2]), g20 = gcd(c[2], c[0]);
		ll g = gcd(c[0], g12);
		cout << ( (c[0]*c[1]/g01) * c[2] / g12 / g20 * g) << endl;
	}
	return 0;
}