#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;

//4 % 6 6,4
//6 % 4 4,2
//4 % 2 = 0 2
//(4 / 2) * (6 / 2)  * 2;
//(4 + 6) / 2 * 2;
//(c[0] / gcd) * (c[0] / gcd)
//(4 / 2) * (6 / 2) * (12 / 2) * 2;
//1 * 3 * 3 * 2 = 12

//a = a'm
//b = b'm
//c = c'm
//a',b',c'ªf
//a'b'c' / m ^ 2
//2 3 6
long long int gcd(long long int a, long long int b){
	if(b == 0)return a;
	return gcd(b,a % b);
}

//4 6 2 * (4 / 2) * (6 / 2)
//=2 * 2 * 3 = 12
long long int lcm(long long int a, long long int b){
	return a * b / gcd(a,b);
}

int main(){
	long long int a[3], m[3], p[3] = {1,1,1}, c[3] = {0};
	while(1){
		memset(c,0,sizeof(c));
		cin >> a[0] >> m[0] >> a[1] >> m[1] >> a[2] >> m[2];
		if(!a[0])break;
		for(int i=0;i<3;i++){
			do{
			p[i] = (a[i] * p[i]) % m[i];
			//cout << "Turn " << c[i] << ": " << p[i] << endl;
			c[i] += 1;
			}while(p[i] != 1);
		}
		//cout << c[0] << c[1] << c[2] << endl;
		//int gcd = rec(c[0],rec(c[1],c[2])),
		//lcm = rec(c[0],gcd) * rec(c[1],gcd) * rec(c[2],gcd) * gcd;
			//cout << gcd << " " << lcm << endl;
		cout << lcm(lcm(c[0],c[1]),c[2]) << endl;
	}
	//2 * 1 % 5 = 2;
	//2 * 2 % 5 = 4;
	//2 * 4 % 5 = 3;3ñÚÅÍÈ¢©D
	//2 * 3 % 5 = 1;
	//3 * 1 % 7 = 3;
	//3 * 3 % 7 = 2;
	//3 * 2 % 7 = 6;
	//3 * 6 % 7 = 4;
	//3 * 4 % 7 = 5;
	//3 * 5 % 7 = 1;6ñÚ
	return 0;
}