#include "bits/stdc++.h"
using namespace std;
typedef long long ll;

ll gcd(ll aa, ll bb){
	return (bb > 0)? gcd( bb , aa%bb ) : aa ;
}

ll lcm(ll aa, ll bb){
	return aa / gcd(aa,bb) * bb;
}

int main() {
	 ll a, b, c, d, e, f;

	while (cin >> a >> b >> c >> d >> e >> f) {
		ll x = 1, y = 1, z = 1;
		ll xcnt = 0, ycnt = 0, zcnt = 0;
		if(a==0&&b==0&&c==0&&d==0&&e==0&&f==0)break;
		while (1) {
			x = (a * x) % b;
			xcnt++;
			if (x == 1)
				break;
		}
		while (1) {
			y = (c * y) % d;
			ycnt++;
			if (y == 1)
				break;
		}
		while (1) {
			z = (e * z) % f;
			zcnt++;
			if (z == 1)
				break;
		}
		ll ans = lcm( lcm(xcnt,ycnt) , zcnt);
				cout << ans << endl;

	}
}