#include <iostream>
using namespace std;

long long minCount( const long long a, const long long m )
{
	long long ret = 0, p = 1;
	do{
		++ret;
		p = ( p * a ) % m;
	}while( p != 1 );

	return ret;
}

long long lcm( const long long x, const long long y )
{
	long long p, q, tmp;
	p = x;
	q = y;
	while( ( tmp = p % q ) != 0 ){
		p = q;
		q = tmp;
	}
	return x / q * y;
}

int main()
{
	long long a[3], m[3];
	while( ( cin >> a[0] >> m[0] >> a[1] >> m[1] >> a[2] >> m[2] )
		&& !( !a[0] && !a[1] && !a[2] && !m[0] && !m[1] && !m[2] ) ){

		long long x, y, z;

		x = minCount( a[0], m[0] );
		y = minCount( a[1], m[1] );
		z = minCount( a[2], m[2] );

		cout << lcm( lcm( x, y ), z ) << endl;
	}
	return 0;
}