#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <functional>
#include <numeric>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <utility>
#include <sstream>
#include <complex>

using namespace std;

#define FOR(i,a,b) for(long long i=(a);i<(b);i++)
#define REP(i,N) for(long long i=0;i<(N);i++)
#define ALL(s) (s).begin(),(s).end()
#define fi first
#define se second

#define PI acos(-1.0)
#define INF 1000000007

typedef long long ll;
typedef pair<ll, ll> P;
typedef pair<double, double> PD;
typedef pair<string, ll> PS;
typedef vector<ll> V;

ll gcd(ll p, ll q){
	if (q == 0){
		return p;
	}
	 return gcd(q, p%q);
}

ll a[10], m[10], co[10], L;

int main(){
	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){
			co[i] = 1;
			int k = a[i];
			while (1){
				if (k == 1)break;
				co[i]++;
				k = k*a[i] % m[i];
			}
		}
		L = co[2] * co[1] / gcd(co[2],co[1]);
		L = L*co[0] / gcd(L, co[0]);
		cout << L << endl;
	}
}