#include<iostream>
#include<algorithm>
using namespace std;
int main(){
	int a1,m1,a2,m2,a3,m3;
	while(cin>>a1>>m1>>a2>>m2>>a3>>m3,a1|m1|a2|m2|a3|m3){
		int x=1,y=1,z=1;
		long long n=0,m=0,k=0,s,t;
		do{
			x=a1*x%m1;
			n++;
		}while(x!=1);
		do{
			y=a2*y%m2;
			m++;
		}while(y!=1);
		do{
			z=a3*z%m3;
			k++;
		}while(z!=1);
		s=n/__gcd(n,m)*m;
		t=s/__gcd(s,k)*k;
		cout<<t<<endl;
	}
	return 0;
}