#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

#define rep(i,n) for(int i=0;i<n;i++)
typedef long long ll;

ll gcd(ll a,ll b){
	if(a>b) swap(a,b);
	if(a==0) return b;
	return gcd(b%a,a);
}


int main(){
	ll a[3],m[3],x[3];
	while(cin>>a[0]>>m[0]>>a[1]>>m[1]>>a[2]>>m[2],a[0]||a[1]||a[2]||m[0]||m[1]||m[2]){
		rep(i,3) x[i]=1;
		ll c[3]={0};
		rep(i,3){
			do{
				c[i]++;
				x[i]=a[i]*x[i]%m[i];
			}while(x[i]!=1);
			// cout<<c[i]<<endl;
		}
		ll ans=c[0]*c[1]/gcd(c[0],c[1]);
		// cout<<ans<<endl;
		ans=ans*c[2]/gcd(ans,c[2]);
		cout<<ans<<endl;
	}
}