#include "bits/stdc++.h"
using namespace std;
long long int gcd(long long int l, long long int r) {
	assert(l > 0 && r > 0);
	if (l > r)return gcd(r, l);
	else {
		const long long int num = r%l;
		if (num) {
			return gcd(l, num);
		}
		else {
			return l;
		}
	}
}
long long int lca(long long int l, long long int r) {
	return l / gcd(l, r)*r;
}

long long int gcd(vector<long long int>nums) {
	if (nums.size() == 1)return nums[0];
	long long int num = nums[0];
	
	for (int i = 1; i < nums.size(); ++i) {
		num = gcd(num, nums[i]);
	}
	return num;
}

long long int lca(const vector<long long int>&nums) {
	if (nums.size() == 1)return nums[0];
	else {
		int num = nums[0];
		for (int i = 1; i < nums.size(); ++i) {
			num = lca(num, nums[i]);
		}
		return num;
	}
}
int main() {
	while (1) {
		vector<long long int>as(3), ms(3),backs(3);
		for (int i = 0; i < 3; ++i)cin >> as[i] >> ms[i];
		if (!as[0])break;
		for (int i = 0; i < 3; ++i) {
			long long  int now = 1;
			while (backs[i]++,true) {
				now = (now*as[i]) % ms[i];
				if (now == 1)break;
			}
		}
		long long int ans = lca(backs);
		cout << ans << endl;
	}
	return 0;
}