#include<iostream>
#include<iomanip>
#include<algorithm>
#include<array>
#include<bitset>
#include<cassert>
#include<cctype>
#include<cmath>
#include<cstdio>
#include<cstring>
#include<functional>
#include<limits>
#include<list>
#include<map>
#include<numeric>
#include<set>
#include<stack>
#include<string>
#include<sstream>
#include<unordered_map>
#include<queue>
#include<vector>
using namespace std;

#define int long long
using ll = long long;
using ull = unsigned long long;
using pii = pair<int, int>;

#define rep(i,a,b) for(int i=(a);i<(b);i++)
#define rrep(i,a,b) for(int i=(b)-1;i>=(a);i--)
#define all(a) (a).begin(),(a).end()
#define dump(o) {cerr<<#o<<" "<<o<<endl;}
#define dumpc(o) {cerr<<#o; for(auto &e:(o))cerr<<" "<<e;cerr<<endl;}
#define INF 0x3f3f3f3f
#define INFL 0x3f3f3f3f3f3f3f3fLL
const int MOD = 1e9 + 7;
//?????§??¬?´???°
int gcd(int x, int y) { return y ? gcd(y, x%y) : x; }
//????°???¬?????°
int lcm(int x, int y) { return x*y / gcd(x, y); }
signed main() {
	while (true) {
		int a[3], m[3];
		rep(i, 0, 3)cin >> a[i] >> m[i];
		if (a[0] == 0)break;
		int x = 1, y = 1, z = 1;
		x = (a[0] * x) % m[0];
		y = (a[1] * y) % m[1];
		z = (a[2] * z) % m[2];
		int cnt[3] = { 1,1,1 };
		while (x != 1) {
			cnt[0]++;
			x = (a[0] * x)%m[0];
		}
		while (y != 1) {
			cnt[1]++;
			y = (a[1] * y)%m[1];
		}
		while (z != 1) {
			cnt[2]++;
			z = (a[2] * z)%m[2];
		}
		int ans = lcm(lcm(cnt[0], cnt[1]), lcm(cnt[1], cnt[2]));
		cout << ans << endl;
	}
	return 0;
}