#include <iostream>
#include <algorithm>

using namespace std;

#define ll long long

ll gcd(ll a, ll b){
	if(a < b) swap(a,b);
	if(b == 0) return a;
	
	return gcd(b, a%b);
}

ll lcm(ll a, ll b){
	ll g;
	
	g = gcd(a,b);
	
	return a * b / g;
}

int main(void){
	ll a,b,c,d,e,f,g;
	long long ret;
	ll tmp = 1;
	
	while(cin >> a >> b >> c >> d >> e >> f){
		if((a|b|c|d|e|f|g) == 0) break;
		
		int i=0,j=0,k=0;
		
		tmp = 1;
		do{
			tmp = a * tmp % b;
			i++;
		}while(tmp != 1);
		tmp = 1;
		do{
			tmp = c * tmp % d;
			j++;
		}while(tmp != 1);
		tmp = 1;
		do{
			tmp = e * tmp % f;
			k++;
		}while(tmp != 1);
			
		ret = lcm(i, lcm(j, k));
		cout << ret << endl;
	}
}