#include <iostream>
#include <algorithm>
using namespace std;

long long int Gcd(long long int a, long long int b)
{
	if (a < b) swap(a, b);
	if (b == 0) return a;
	return Gcd(a % b, b);
}

long long int Lcm(long long int a, long long int b)
{
	return a / Gcd(a, b) * b;
}

int main()
{
	int a[3];
	int m[3];
	while (true)
	{
		for (int i = 0; i < 3; i++) cin >> a[i] >> m[i];
		if (a[0] == 0 && a[1] == 0 && a[2] == 0 && m[0] == 0 && m[1] == 0 && m[2] == 0) return 0;

		int x = 1;
		int y = 1;
		int z = 1;
		long long int countX = 0;
		long long int countY = 0;
		long long int countZ = 0;

		while (true)
		{
			countX++;
			x = (a[0] * x) % m[0];
			if (x == 1) break;
		}
		while (true)
		{
			countY++;
			y = (a[1] * y) % m[1];
			if (y == 1) break;
		}
		while (true)
		{
			countZ++;
			z = (a[2] * z) % m[2];
			if (z == 1) break;
		}

		long long int lcm = Lcm(countX, countY);
		lcm = Lcm(lcm, countZ);
		cout << lcm << endl;
	}

	return 0;
}