#include <bits/stdc++.h>

using namespace std;

int A[3], M[3];
long long int Count[3];

void calculation() {
	long long int ans;

	for (int index = 0; index < 3; index++) {
		ans = A[index] % M[index];
		while (ans != 1) {
			Count[index]++;
			ans = (A[index] * ans) % M[index];
		}
	}

}

long long int findLeastCommonMultiple(long long int a, long long int b) {
	long long int c, a1, b1;

	a1 = a;
	b1 = b;
	c = a % b;
	while (c != 0) {
		a = b;
		b = c;
		c = a % b;
	}
	return (b1 / b) *a1;
}


int main() {

	while (1) {
		for (int i = 0; i < 3; i++) {
			cin >> A[i] >> M[i];
			Count[i] = 1;
		}
		if (A[0] == 0) {
			break;
		}

		calculation();
		cout << findLeastCommonMultiple(findLeastCommonMultiple(Count[0],Count[1]),Count[2]) << endl;
	}

	return 0;
}
