#define _CRT_SECURE_NO_WARNINGS
#define _USE_MATH_DEFINES

#include <iostream>
#include <algorithm>
#include <functional>
#include <string>
#include <queue>
#include <cmath>
#include <stack>
#include <numeric>
#include <ctime>
#include <algorithm>
#include <set>
#include <stdio.h>
#include <map>

using namespace std;
typedef long long int lint;

lint LCM(lint a, lint b)
{
	lint na, nb, buf;
	na = a;
	nb = b;
	while (a % b != 0){
		a = a % b;
		buf = a;
		a = b;
		b = buf;
	}
	return (na * nb) / b;
}

lint electro_fly(lint a, lint m)
{
	lint ret = 0;
	lint x = 1;
	do{
		x = a * x % m;
		ret++;
	} while (x != 1);

	return ret;
}

lint solve(lint a1, lint m1, lint a2, lint m2, lint a3, lint m3)
{
	return LCM(electro_fly(a1, m1), LCM(electro_fly(a2, m2), electro_fly(a3, m3)));
}

int main()
{
	lint a1, m1, a2, m2, a3, m3;
	while (1){
		cin >> a1 >> m1 >> a2 >> m2 >> a3 >> m3;
		if (a1 == 0) break;
		cout << solve(a1, m1, a2, m2, a3, m3) << endl;
	}
	return 0;
}