#include <iostream>
#include <iomanip>
#include <algorithm>
#include <stack>
#include <queue>
#include <vector>
#include <map>
#include <bitset>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <climits>

using namespace std;

typedef long long ll;

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

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

int main() {
	ll a1, m1, a2, m2, a3, m3;
	while (1) {
		cin >> a1 >> m1 >> a2 >> m2 >> a3 >> m3;
		if (!(a1 | m1 | a2 | m2 | a3 | m3))
			break;
		ll a, b, c, x, y, z;
		a = b = c = x = y = z = 1;
		x = a1 * x % m1;
		y = a2 * y % m2;
		z = a3 * z % m3;
		for (; x != 1; a++)
			x = a1 * x % m1;
		for (; y != 1; b++)
			y = a2 * y % m2;
		for (; z != 1; c++)
			z = a3 * z % m3;
		cout << lcm(lcm(a, b), c) << endl;
	}
	
	return 0;
}