#include <iostream>
#include <vector>
#include <string>
#include <stack>
#include <map>
#include <algorithm>
#include <iomanip>
using namespace std;

#define MAXV 1000000000
using ll = long long;

bool operator<(const pair<int, int>& a, const pair<int, int>& b) {
	if (a.first == b.first) {
		return a.second < b.second;
	}
	return a.first < b.first;
}

ll min_cost(ll n, ll a1, ll m1) {
	if (n == 1) return 1;
	return 1 + min_cost(n*a1%m1,a1, m1);
}

ll gcd(ll a, ll b) {
	if (b == 0) return a;
	return gcd(b, a%b);
}

int main() {
	
	
	ll a1, m1, a2, m2, a3, m3;
	while (cin >> a1 >> m1 >> a2 >> m2 >> a3 >> m3) {
		if (a1 == 0 && m1 == 0 && a2 == 0 && m2 == 0 && a3 == 0 && m3 == 0) break;

		ll l1 = min_cost(a1%m1, a1, m1);
		ll l2 = min_cost(a2%m2, a2, m2);
		ll l3 = min_cost(a3%m3, a3, m3);

		ll L1 = l1/gcd(l1, l2)*l2;

		cout << L1 * l3 / gcd(L1, l3) << endl;
	}
	int a; cin >> a;
}
