#include <iostream>

using namespace std;

typedef long long ll;

ll a, b, c, l, m, n;

/*
ll move(ll x, ll y, ll z) {
	ll nx = a * x % l;
	ll ny = b * y % m;
	ll nz = c * z % n;

	if (nx == 1 && ny == 1 && nz == 1) return 1;
	else return move(nx, ny, nz) + 1;
}
*/

ll gcd(ll a, ll b) {
	if (a == 0) return b;
	if (b == 0) return a;
	return gcd(b, a%b);
}

ll lcm(ll a, ll b) {
	ll g = gcd(a, b);
	return a / g * b;
}

int main() {
	while (cin >> a >> l >> b >> m >> c >> n, a || b || c || l || m || n) {
		ll times = 0, x, y, z, tx, ty, tz;

		x = y = z = 1;
		tx = ty = tz = 1;

		while ((x = a * x % l) != 1) ++tx;
		while ((y = b * y % m) != 1) ++ty;
		while ((z = c * z % n) != 1) ++tz;

		cout << lcm( lcm(tx, ty), tz) << endl;
	}
}