#include <cstdio>
typedef long long ll;
int a[3],m[3];
ll cnt[3];

int gcd(int a,int b){
	if(b==0)return a;
	return gcd(b,a%b);
}

int main(void){
	while(1){
		for(int i=0;i<3;i++){
			scanf("%d %d",&a[i],&m[i]);
		}
		if(a[0]==0)break;
		for(int i=0;i<3;i++){
			int x=a[i]%m[i];
			cnt[i]=1;
			while(x!=1){
				x=x*a[i]%m[i];
				cnt[i]++;
			}
		}
		ll k=cnt[0]*cnt[1]/gcd(cnt[0],cnt[1]);
		printf("%lld\n",k*cnt[2]/gcd(k,cnt[2]));
	}
	return 0;
}