#include <iostream>
#include <algorithm>

using namespace std;

int gcd(int a, int b)
{
	if(a % b == 0)
	{
		return(b);
	}
	else
	{
		gcd(b, a % b);
	}
}

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

void solve()
{
	int a[3], m[3];
	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])
	{
		int count[3] = {0, 0, 0};
		int x[3] = {1, 1, 1};
		for(int i = 0; i < 3; ++i)
		{
			while(true)
			{
				count[i]++;
				x[i] = (x[i] * a[i]) % m[i];
				if(x[i] == 1)
				{
					break;
				}
			}
		}
		sort(count, count + 3);
		int d = lcm(count[1], count[0]);
		int Answer;
		d > count[2] ? Answer = lcm(d, count[2]) : Answer = lcm(count[2], d);
		cout << Answer << endl;
	}
}

int main()
{
	solve();
	return(0);
}