#include <iostream>
#define int long long
using namespace std;

int gcd(int a,int b){
	if(a < b) swap(a,b);
	while(b){
		a %= b;
		swap(a,b);
	}
	return a;
}

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

signed main(){
	int a,b,c,d,e,f;
	while(cin >> a >> b >> c >> d >> e >> f,a){
		int x = 1,y = 1,z = 1,pos;
		pos = a % b;
		while(pos != 1){
			pos = pos * a % b;
			x++;
		}
		pos = c % d;
		while(pos != 1){
			pos = pos * c % d;
			y++;
		}
		pos = e % f;
		while(pos != 1){
			pos = pos * e % f;
			z++;
		}
		cout << lcm(x,lcm(y,z)) << endl;
	}
	return 0;
}