#include<stdio.h>
#include<bitset>
template<class T>
T LCM(T x, T y)
{
	T z;
	while( (z=x%y) != 0)
	{
		x = y;
		y = z;
	}
	return y;
}
template<class T>
T GCD(T x,T y)
{
	return x/LCM(x,y)*y;
}
int main()
{
	int a1,a2,a3,m1,m2,m3,i,j;
	const int N = 32768;
	int euler[N];
	std::bitset<N> isPrime;

	for(i = 0; i < N; ++i)
		euler[i] = i;
	isPrime.set();
	isPrime[0] = isPrime[1] = false;
	for(i = 2; i < N; ++i)
	{
		if(isPrime[i])
		{
			euler[i] -= euler[i]/i;
			for(j = 2*i; j < N; j+=i)
			{
				isPrime[j] = false;
				euler[j] -= euler[j]/i;
			}
		}
	}
	


	while(scanf("%d%d%d%d%d%d",&a1,&m1,&a2,&m2,&a3,&m3), a1)
	{
		printf("%d\n",
			GCD( euler[m1], GCD(euler[m2], euler[m3]))
			);
	}
	return 0;
}