#include <cstdio>
#include <cstring>

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

#define l(a,b) a*b/g(a,b)

int main(){
	int a[3], m[3], p[3] = {1,1,1}, c[3];
	while(scanf("%d%d%d%d%d%d",a,m,a+1,m+1,a+2,m+2),*a){
		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",l(l(c[0],c[1]),c[2]));
	}
	return 0;
}