#include <iostream>
using namespace std;

typedef long long ll;

ll gcd(ll a, ll b) {
	return a % b ? gcd(b, a % b) : b;
}
ll lcm(ll a, ll b) {
	return a * b / gcd(a, b);
}

int main() {
	while(1) {
		int a[3][2];
		int flg = 0;
		for(int i = 0; i < 3; i++) {
			for(int j = 0; j < 2; j++) {
				cin >> a[i][j];
				flg += a[i][j];
			}
		}
		if(!flg)
			break;
		ll res = 1;
		for(int i = 0; i < 3; i++) {
			ll b, j;
			j = 1, b = a[i][0];
			while(b != 1) {
				b = (b * a[i][0]) % a[i][1];
				j++;
			}
			res = lcm(res, j);
		}
		cout << res << endl;
	}
}