#include<iostream>

typedef long long ll;

ll period( ll a, ll m )
{
	ll p = 1, cnt = 0;

	do
	{
		p = ( a * p ) % m;
		++cnt;
	}while( p != 1 );

	return cnt;
}

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

	return gcd( b, a % b );
}

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

ll a[3], m[3];

int main()
{
	while( true )
	{	
		for( int i = 0; i != 3; ++i )
			std::cin >> a[i] >> m[i];

		bool f = false;

		for( int i = 0; i != 3; ++i )
			f |= a[i] | m[i];

		if( !f )
			break;

		ll ans = 1;
		for( int i = 0; i != 3; ++i )
			ans = lcm( ans, period( a[i], m[i] ) );

		std::cout << ans << std::endl;
	}

	return 0;
}