#include <iostream>

using namespace std;

unsigned int gcd( unsigned int x, unsigned int y )
{
	int t;

	while( y != 0 ){
		t = x % y; x = y; y = t;
	}

	return x;
}

int main(int argc, char const* argv[])
{
	unsigned int a1,m1,a2,m2,a3,m3;

	while( cin >> a1 >> m1 >> a2 >> m2 >> a3 >> m3 && a1 != 0 && m1 != 0 && a2 != 0 && m2 != 0 && a3 != 0 && m3 != 0 ){
		unsigned int count1,count2,count3;
		unsigned int hoge;

		count1 = count2 = count3 = 0;

		hoge = 1;
		do{
			hoge *= a1;
			hoge %= m1;
			count1++;
		}while( hoge != 1 );

		hoge = 1;
		do{
			hoge *= a2;
			hoge %= m2;
			count2++;
		}while( hoge != 1 );


		hoge = 1;
		do{
			hoge *= a3;
			hoge %= m3;
			count3++;
		}while( hoge != 1 );

		unsigned int count = count1 * count2 * count3 / gcd( count1, count2 ) / gcd( count2, count3 ) / gcd( count1, count3 ) * gcd( gcd( count1, count2 ), count3 );

		cout << count << endl;
	}

	return 0;
}