#include <iostream>
#define rep(i,n) for(int i=0;i<n;i++)
using namespace std;

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

long long lcm(long long m,long long n)
{
	if((m == 0) || (n == 0)) return 0;
	return ((m / gcd(m,n)) * n);
}


int main()
{
	long long a1,m1,a2,m2,a3,m3;
	while(cin >> a1 >> m1 >> a2 >> m2 >> a3 >> m3 && a1)
	{
		long long x = 1,y = 1,z = 1;
		
		
		long long count_x = 1;
		while(true)
		{
			if((a1*x)%m1 == 1)
			{
				break;
			}
			else
			{
				count_x++;
				x = (a1*x)%m1;
			}
		}

		long long count_y = 1;
		while(true)
		{
			if((a2*y)%m2 == 1)
			{
				break;
			}
			else
			{
				count_y++;
				y = (a2*y)%m2;
			}
		}

		long long count_z = 1;
		while(true)
		{
			if((a3*z)%m3 == 1)
			{
				break;
			}
			else
			{
				count_z++;
				z = (a3*z)%m3;
			}
		}

		long long ans = lcm(count_x,count_y);
		ans = lcm(ans,count_z);

		cout << ans << endl;
	}
	return 0;
}