#include<iostream>
#include<cmath>
#include<algorithm>
using namespace std;
int gcd(int a, int b) {
	if (b == 0)return a;
	else return gcd(abs(a - b), min(a, b));
}
int lcm(int a, int b) { return a / gcd(a, b)*b; }
int main() {
	int a1, m1, a2, m2, a3, m3;
	while (cin >> a1 >> m1 >> a2 >> m2 >> a3 >> m3, a1 || a2 || a3 || m1 || m2 || m3) {
		int cnt1 = 0, cnt2 = 0, cnt3 = 0;
		int x = 1, y = 1, z = 1;
		do{
			x = (a1 * x) % m1;
			cnt1++;
		} while (x != 1);
		do {
			y = (a2 * y) % m2;
			cnt2++;
		} while (y != 1);
		do {
			z = (a3 * z) % m3;
			cnt3++;
		} while (z != 1);
		cout << lcm(lcm(cnt1, cnt2), cnt3) << endl;
	}
}