#include <cstdio>
#include <cstring>

long long gcd(long long a, long long b){
	return b?gcd(b,a % b):a;
}

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

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