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

long long int gcd(long long int a,long long int b){
	long long int result =a;
	long long int k=0;
	long long int 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%m1)*(x%m1)%m1;
			if(x==1)break;
		}
		while(1){
			cnt2++;
			y=(a2%m2)*(y%m2)%m2;
			if(y==1)break;
		}
		while(1){
			cnt3++;
			z=(a3%m3)*(z%m3)%m3;
			if(z==1)break;
		}
		calc=cnt1/gcd(cnt1,cnt2)*cnt2;
		ans=cnt3/gcd(calc,cnt3)*calc;
		cout << ans<<endl;
	}
	return 0;
}