#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<cassert>
#include<iostream>
#include<sstream>
#include<string>
#include<vector>
#include<queue>
#include<set>
#include<map>
#include<utility>
#include<numeric>
#include<algorithm>
#include<bitset>
#include<complex>

using namespace std;

typedef long long Int;
typedef vector<int> vint;
typedef pair<int,int> pint;
#define mp make_pair

template<class T> void pv(T a, T b) { for (T i = a; i != b; ++i) cout << *i << " "; cout << endl; }
template<class T> void chmin(T &t, T f) { if (t > f) t = f; }
template<class T> void chmax(T &t, T f) { if (t < f) t = f; }
int in() { int x; scanf("%d", &x); return x; }

Int gcm(Int a,Int b){
	if(b==0)return a;
	else return gcm(b,a%b);
}

Int lcm(Int a,Int b){
	return a*b/gcm(a,b);
}

int main() {
	int a[3],m[3];
	while(cin>>a[0]>>m[0]>>a[1]>>m[1]>>a[2]>>m[2],a[0]){
		int x[3];
		x[0]=a[0];x[1]=a[1];x[2]=a[2];
		int res[3]={1,1,1};
		int i;
		for(i=0;i<3;i++){
			while(x[i]!=1){
				x[i]=a[i]*x[i]%m[i];
				res[i]++;
			}
		}
		cout<<lcm(lcm(res[0],res[1]),res[2])<<endl;
	}
	return 0;
}