#include <iostream>
#include <vector>

/*
unsigned long long order(unsigned long long n)
{
	std::vector<int> primes;
	int p = 2;
	while (n>1) {
		if (n % p == 0) {
			primes.push_back(p);
			do { n /= p; } while (n % p == 0);
		}
		p++;
	}
	
	unsigned long long res = 1;
	
	for (int i=0; i<primes.size(); i++) {
		res *= primes[i] - 1;
	}
	return res;
}
*/

unsigned long long order(unsigned long long a, unsigned long long m)
{
	unsigned long long x = 1;
	unsigned long long count = 0;
	do {
		x *= a;
		x %= m;
		count++;
	} while (x > 1);
	return count;
}
	
unsigned long long gcd(unsigned long long x, unsigned long long y) {
	if (x==0) return y;
	if (y==0) return x;
	if (x>y) return gcd(y, x%y);
	else return gcd(x, y%x);
}

unsigned long long lcm(unsigned long long x, unsigned long long y)
{
	return (x*y) / gcd(x, y);
}

unsigned long long lcm3(unsigned long long x, unsigned long long y, unsigned long long z)
{
	return lcm(x, lcm(y, z));
}

int main()
{
	unsigned long long a1, m1, a2, m2, a3, m3;
	while (true) {
		std::cin >> a1 >> m1 >> a2 >> m2 >> a3 >> m3;
		if (a1==0) break;
		//std::cout << lcm3(order(m1), order(m2), order(m3)) << std::endl;
		std::cout << lcm3(order(a1, m1), order(a2, m2), order(a3, m3)) << std::endl;
	}
	
	return 0;
}

