#include<iostream>
using namespace std;
long long a[3], m[3], k[3];
long double gcd(int a, int b) {
	if (b == 0) { return a; }
	return gcd(b, a%b);
}
int main() {
	while (cin >> a[0] >> m[0] >> a[1] >> m[1] >> a[2] >> m[2]) {
		for (int i = 0; i < 3; i++) {
			long long cnt = 0, x = 1;
			while (true) {
				if (x == 1 && cnt >= 1) { break; }
				x *= a[i]; x %= m[i]; cnt++;
			}
			k[i] = cnt;
		}
		int LCM = k[0] * k[1] / gcd(k[0], k[1]);
		cout << LCM*k[2] / gcd(LCM, k[2]) << endl;
	}
}