#include <iostream>
#include <algorithm>
#include <vector>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <sys/time.h>

using namespace std;

typedef long long ll;

vector<ll> createPrime ( const ll num )
{
	vector<ll> v;
	bool isPrime[num+1];
	
	memset(isPrime, true, sizeof(isPrime));
	
	for (ll i = 2; (i*i) <= num; ++i)
	{
		if (isPrime[i])
		{
			for (ll j = i+i; j <= num; j += i)
			{
				isPrime[j] = false;
			}
		}
	}
	for (ll i = 2; i <= num; ++i)
	{
		if (isPrime[i]) { v.push_back(i); }
	}
	return v;
}

ll LCM ( const vector<ll> &primes, const vector<ll> &nums )
{
	ll lcm = 1;
	for (int i = primes.size()-1; i >= 0; --i)
	{
		ll p = primes[i];
		ll count = 0;
		
		for (int j = 0, jlen = nums.size(); j < jlen; ++j)
		{
			ll n = nums[j], cnt = 0;
			
			while ((n % p) == 0)
			{
				n /= p;
				++cnt;
			}
			count = max(count, cnt);
		}
		
		lcm *= (ll)pow(p, count);
	}
	return lcm;
}

int main ( void )
{
	const int N = 3;
	vector<ll> prime;
	ll a[N], m[N];
	
	prime = createPrime(pow(2, 15)+1);

	while (cin >> a[0] >> m[0] >> a[1] >> m[1] >> a[2] >> m[2], 
		a[0] | m[0] | a[1] | m[1] | a[2] | m[2])
	{
		vector<ll> count(N, 0);

		for (int i = 0; i < N; ++i)
		{
			ll xyz = 1;
			do
			{
				xyz = a[i] * xyz % m[i];
				++count[i];
			} while (xyz != 1);
		}

		ll answer = LCM(prime, count);

		cout << answer << endl;
	}
	return 0;
}