#include <iostream>
using namespace std;
bool flag;
unsigned long long a1,a2,a3,m1,m2,m3;
unsigned long long x,y,z,ans,calc,cnt1,cnt2,cnt3;

unsigned long long  gcd(unsigned long long a,unsigned long long b){
	unsigned long long result =a;
	unsigned long long k=0;
	unsigned long long n=b;
	do{
		k=result %n;
		result=n;
		n=k;
	}while(k!=0);
	return result;
}
int main(){
	while(cin >> a1 >>m1 >>a2 >>m2 >>a3 >>m3){
		x=y=z=1;
		cnt1=cnt2=cnt3=0;
		while(1){
			cnt1++;
			x=a1*x%m1;
			if(x==1)break;
		}
		while(1){
			cnt2++;
			y=a2*y%m2;
			if(y==1)break;
		}
		while(1){
			cnt3++;
			z=a3*z%m3;
			if(z==1)break;
		}
		calc=cnt1/gcd(cnt1,cnt2)*cnt2;
		ans=cnt3/gcd(calc,cnt3)*calc;
		cout << ans<<endl;
	}
	return 0;
}