#include <stdio.h>
#pragma warning(disable : 4996)
int a1, m1, a2, m2, a3, m3;
int gcd(int x, int y) {
	if (x == 0) return y;
	if (y == 0) return x;
	if (x > y) return gcd(y, x % y);
	return gcd(x, y % x);
}
long long lcm(long long x, long long y) {
	return 1LL * x / gcd(x, y) * y;
}
long long lcm(int a, int b, int c) {
	return lcm(lcm(a, b), lcm(a, c));
}
int solve(int x, int m) {
	int ret = 1, s = x;
	while (s != 1) s = s * x % m, ret++;
	return ret;
}
int main() {
	while (true) {
		scanf("%d%d%d%d%d%d", &a1, &m1, &a2, &m2, &a3, &m3);
		if (!a1 && !m1 && !a2 && !m2 && !a3 && !m3) break;
		printf("%lld\n", lcm(solve(a1, m1), solve(a2, m2), solve(a3, m3)));
	}
	return 0;
}