#include <iostream>
using namespace std;


using LLint = long long int;


LLint gcd(LLint a, LLint b){
	return (b > 0) ? gcd(b, a%b) : a;
}


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


int main(){
	LLint a1, m1, a2, m2, a3, m3;
	
	while (cin >> a1 >> m1 >> a2 >> m2 >> a3 >> m3, a1||m1||a2||m2||a3||m3){
		LLint x = 1, y = 1, z = 1, a = 1, b = 1, c = 1;

		x = (x*a1) % m1;
		y = (y*a2) % m2;
		z = (z*a3) % m3;

		for (; x != 1; a++){ x = (x*a1) % m1; }
		for (; y != 1; b++){ y = (y*a2) % m2; }
		for (; z != 1; c++){ z = (z*a3) % m3; }

		LLint ans = lcm(lcm(a, b), c);
		cout << ans << endl;
	}

	return 0;
}