#include<bits/stdc++.h>
#define rep(i,n)for(ll i=0;i<n;i++)
using namespace std;
typedef long long ll;

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