#include <vector>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <algorithm>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <string>
#include <cstring>
#include <complex>
#include <ctime>
#include <cstdlib>

using namespace std;

inline int to_int(string s) {int v; istringstream sin(s); sin >> v; return v;}
template<class T> inline string to_str(T x) {ostringstream sout; sout << x; return sout.str();}

typedef long long ll;

int find_cycle(int a, int m)
{
	int f = 0, x = 1;
	while(1)
	{
		++f;
		x = (a*x)%m;
		if(x == 1)
		{
			break;
		}
	}
	return f;
}
ll gcd(ll a, ll b)
{
	if( b == 0 )
	{
		return a;
	}
	else
	{
		return gcd(b, a%b);
	}
}

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

int main()
{
	int a1, a2, a3, m1, m2, m3;
	while(scanf("%d%d%d%d%d%d", &a1, &m1, &a2, &m2, &a3, &m3) != EOF)
	{
		if(a1 == 0)
		{
			break;
		}
		ll fx = find_cycle(a1, m1), fy = find_cycle(a2, m2), fz = find_cycle(a3, m3);

		cout << lcm(fx, lcm(fy, fz)) << endl;
	}
}