#include <iostream>
using namespace std;
typedef long long ll;

ll gcd(ll a, ll b)
{
	if (a < b) return gcd(b,a);
	if (b==0) return a;
	if (a%b==0) return b;
	return gcd(b, a%b);
}

ll lcm(ll a, ll b)
{
	return a*b/gcd(a,b);
}

int a1, a2, a3, m1, m2, m3;
int main()
{
	while (cin >> a1 >> m1 >> a2 >> m2 >> a3 >> m3, a1||m1||a2||m2||a3||m3)
	{
		int t=1, c=0, cx, cy, cz;
		do
		{
			t = t*a1 % m1;
			c++;
		} while (t!=1);
		cx = c;
		c = 0;
		do
		{
			t = t*a2 % m2;
			c++;
		} while (t!=1);
		cy = c;
		c = 0;
		do
		{
			t = t*a3 % m3;
			c++;
		} while (t!=1);
		cz = c;

		cout << lcm(lcm(cx, cy), cz) << endl;
	}
	return 0;
}