#include<iostream>
using namespace std;
typedef long long LL;

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

	return(b / gcd(a, b))*a;
}

int main() {
	int a, b, c, d, e, f;
	while (cin >> a >> b >> c >> d >> e >> f, a, b, c, d, e, f) {

		LL cx=0, cy=0, cz=0;

		int x = 1, y = 1, z = 1;

		do {
			x = (a*x) % b;
			cx++;
		} while (x != 1);

			do {
				y = (c*y) % d;
				cy++;
			} while (y != 1);

			do {
				z = (e*z) % f;
				cz++;
			} while (z != 1);

			cout << lcm(lcm(cy, cx), cz) << endl;
	}
	return 0;
}