#include <iostream>
#include <vector>
#include <algorithm>
//#include <fstream>

using namespace std;

int gcd (int a, int b)
{
	if (a > b){
		swap(a,b);
	} // end if
	while (b % a != 0){
		b = b % a;
		if (a > b){
			swap(a,b);
		} // end if
	} // end while

	return a;
}
long long lcm (int a, int b)
{
	long long res = (long long)( a/gcd(a,b) );
 	res *= (long long)b;

	return res;
}

int calc_count (int a, int m )
{
	int x = 1;
	int res = 0;

	do{
		x = a*x % m;
		++res;
	}while (x != 1 );

	return res;
}

int main()
{
//	cut here before submit 
//	freopen ("testcase.electro-fly", "r", stdin );

	vector <int> a (3, 0 ), m (3, 0 );

	while (1){
		for (int i = 0; i < 3; ++i){
			cin >> a[i] >> m[i];
		} // end for

		if (!a[0] && !a[1] && !a[2] && !m[0] && !m[1] && !m[2] ){
			break;
		} // end if
		
		vector <int> c (3, 0 );

		for (int i = 0; i < 3; ++i){
			c[i] = calc_count (a[i], m[i] );
		} // end for

		sort (c.begin(), c.end(), greater<int>() );

		long long res = lcm (c[0], lcm (c[1], c[2] ) );

		cout << res << endl;

	} // end loop
		
	return 0;
}