// #define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <stack>
#include <cmath>
#include <cstdlib>
#include <functional>
#include <locale>
#include <cctype>
#include <sstream>
using namespace std;

typedef long long LL;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef map<int, int> MAPII;
typedef vector<pair<int, int> > VPII;
typedef multimap<int, string, greater<int> > MuMIS;

#define MP make_pair
#define fastIO	cin.tie(0); ios::sync_with_stdio(false);
#define FOR(i,a,b) for(int i=(a);i<(b);i++)
//for gcc (未test)
// #define FOREACH_IT(it,c)	for(typeof(c)::iterator it=(c).begin(); it!=(c).end(); ++it)
//for Visual Studio
#define foreach_it(type,it,c) for(type::iterator it=c.begin(), c_end=c.end(); it!=c_end; ++it)

// ------------------- include, typedef, define END. -------------------

LL gcd(LL a, LL b){ return (b > 0) ? gcd(b, a%b) : a; }
LL lcm(LL a, LL b){ return a / gcd(a, b) * b; }

LL solve(LL a, LL now, LL m){
	return ((a*now) % m);
}

int main(){
	// 参考：http://d.hatena.ne.jp/otaks/20120301/1330610350
	// 参考：http://d.hatena.ne.jp/nanikaka/20110829/1314605899
	fastIO;
	LL a1, a2, a3, m1, m2, m3;
	while (cin >> a1 >> m1 >> a2 >> m2 >> a3 >> m3,a1,a2,a3,m1,m2,m3){
		LL nowx = 1, nowy = 1, nowz = 1;
		LL countx = 0, county = 0, countz = 0, ans = 0;
		while (1){
			nowx = solve(a1, nowx, m1);
			countx++;
			if (nowx == 1)	break;
		}
		while (1){
			nowy = solve(a2, nowy, m2);
			county++;
			if (nowy == 1)	break;
		}
		while (1){
			nowz = solve(a3, nowz, m3);
			countz++;
			if (nowz == 1)	break;
		}
		ans = lcm(lcm(countx, county), countz);
		cout << ans << endl;
	}
	return 0;
}