#include <cstdio>
typedef long long ll;
ll gcd(ll a,ll b){
	if(b == 0){
		return a;
	}
	return gcd(b,a%b);
}
ll lcm(ll a,ll b){
	return a*b/gcd(a,b);
}

int main(){
	int a[3],m[3];
	while(scanf("%d%d%d%d%d%d",&a[0],&m[0],&a[1],&m[1],&a[2],&m[2]), a[0] != 0){
		int t[3];
		for(int i = 0; i < 3; i++){
			t[i] = 1;
			int x = 1;
			while(x = (a[i]*x)%m[i],x != 1){
				t[i]++;
			}
		}
		printf("%lld\n",lcm(t[0],lcm(t[1],t[2])));
	}
	return 0;
}

