#include <iostream>
using namespace std;

long long gcd(long long u, long long v){
	long long t;
	if(u > v){ t = u; u = v; v = t; }
	while(v > 0){
		u = u % v;
		t = u;
		u = v;
		v = t;
	}
	return u;
}

long long low(long long i, long long j){
	return i * j / gcd(i, j);
}

long long three_low(long long i, long long j, long long k){
	return low(i, low(j,k));
}

int main(){
	int a1, m1, a2, m2, a3, m3;
	while(cin >> a1 >> m1 >> a2 >> m2 >> a3 >> m3){
		if(!a1 && !m1 && !a2 && !m2 && !a3 && !m3) break;
		int x = 1, y = 1, z = 1;
		long long count1 = 0, count2 = 0, count3 = 0;
		do{
			x = a1 * x % m1;
			count1++;
		}while(x != 1);
		do{
			y = a2 * y % m2;
			count2++;
		}while(y != 1);
		do{
			z = a3 * z % m3;
			count3++;
		}while(z != 1);

		cout << three_low(count1, count2, count3) << endl;
	}
	return 0;
}