#include <iostream>
#define int long long

using namespace std;

int cou(int a, int m);
int ggd(int number1, int number2);
int lcm(int number1, int number2);

signed main() {
	int a1, a2, a3, m1, m2, m3;
	int x, y, z;
	int count;
	int countx, county, countz;
	while (cin >> a1 >> m1 >> a2 >> m2 >> a3 >> m3, a1 || a2 || a3 || m1 || m2 || m3) {
		x = 1;
		y = 1;
		z = 1;
		count = 0;

		countx = cou(a1, m1);
		county = cou(a2, m2);
		countz = cou(a3, m3);

		cout << lcm(lcm(countx, county), countz) << endl;
	}
	return 0;
}

int cou(int a, int m) {
	int x = 1;
	int count = 0;
	do {
		count++;
		x = (a * x) % m;
	} while (x != 1);
	return count;
}

int ggd(int number1, int number2) {
	int m = number1;
	int n = number2;

	if (number2 > number1) {
		m = number2;
		n = number1;
	}

	while (m != n) {
		int temp = n;
		n = m - n;
		m = temp;
	}

	return m;
}

int lcm(int number1, int number2) {
	return number1 * number2 / ggd(number1, number2);
}