#include <bits/stdc++.h>
using namespace std;
const int dx[]={1,0,-1,0,1,-1,-1,1,2,0,-2,0};
const int dy[]={0,1,0,-1,1,1,-1,-1,0,2,0,-2};
const int INF = 1<<30;
const double EPS = 1e-8;
#define PB push_back
#define mk make_pair
#define fr first
#define sc second
#define ll long long
#define reps(i,j,k) for(int i = (j); i < (k); i++)
#define rep(i,j) reps(i,0,j)
#define MOD 1000000007
#define lengthof(x) (sizeof(x) / sizeof(*(x)))
typedef pair<int,int> Pii;
typedef pair<Pii,int> P;
typedef vector<int> vi;
typedef vector<vi> vvi;
long long LCM(ll a, ll b){
	return a*b/__gcd(a,b);
}
int main(){
	while(true){
		ll a,b;
		ll ans = 1;
		ll n = 1;
		rep(i,3){
			ll cnt = 0;
			cin >> a >> b;
			if( a == 0 && b == 0 )goto end;
			n = a*n % b;
			while(n!=1){
				n = a*n % b;
				cnt++;
			}
			ans = LCM(cnt+1,ans);
		}
		cout << ans << "\n";
	}
	end:;
    return 0;
}