#include <stdio.h>

long long int gcd(long long int m,long long int n){
	return ((n == 0) ? m : gcd(n,m % n));
}

long long int lcm(long long int m,long long int n){
	long long int g = gcd(m,n);
	return m / g * n;
}

int count(int a,int m){
	int num = 1;
	int count = 0;
	
	do{
		num = (num*a)%m;
		count++;
	}while(num != 1);
	
	return count;
}

int main(int argc,char** argv){
	int a1,a2,a3,m1,m2,m3;
	long long int x1,x2,x3,l;
	
	for(;;){
		scanf("%d %d %d %d %d %d",&a1,&m1,&a2,&m2,&a3,&m3);
		
		if(a1 == 0 || m1 == 0 || a2 == 0 || m2 == 0 || a3 == 0 || m3 == 0){
			break;
		}
		
		printf("%lld\n",lcm(count(a1,m1),lcm(count(a2,m2),count(a3,m3))));
	}
	
	return 0;
}