#include <iostream>
#include <iomanip>
#include <sstream>
#include <stdio.h>
#include <list>
#include <vector>
#include <stack>
#include <queue>
#include <map>
#include <algorithm>
#include <math.h>
#include <utility>
#include <string>
#include <ctype.h>
#include <cstring>
#include <cstdio>
#include <sstream>
#include <functional>

using namespace std;

#define FOR(i,k,n) for(int i = (k); i < (n); i++)
#define REP(i,n) FOR(i,0,n)
#define INF 114514810
#define ll long long
//#define scanf scanf_s

ll lcm(int x, int y)
{
	ll X = x; ll Y = y;
	if (x < y) swap(x, y);
	while (1)
	{
		if (y == 0) break;
		ll tmpy = y;
		y = x%y;
		x = tmpy;
	}
	return X / x*Y;
}

int main()
{
	ll a[3]; ll m[3];
	while (1)
	{
		REP(i, 3) cin >> a[i] >> m[i];
		if (!a[0] && !a[1] && !a[2] && !m[0] && !m[1] && !m[2]) break;
		ll t[3];
		REP(i, 3)
		{
			ll x = 1;
			x = (a[i] * x % m[i]);
			for (t[i] = 1; x != 1; t[i]++)
			{
				x = (a[i] * x % m[i]);
			}
		}
		cout << lcm(t[0], lcm(t[1], t[2])) << endl;
	}
	return 0;
}