#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <cfloat>
#include <map>
#include <utility>
#include <set>
#include <iostream>
#include <memory>
#include <string>
#include <vector>
#include <algorithm>
#include <functional>
#include <sstream>
#include <complex>
#include <stack>
#include <queue>
#include <numeric>
#include <string.h>
#include <cassert>
 
#ifdef _DEBUG
#define typeof(X) std::identity<decltype(X)>::type //C++0x (for vs2010)
#else
#define typeof(X) __typeof__(X) // for gcc
#endif
 
#define sz(a)  int((a).size())
#define FOREACH(it, c) for (typeof((c).begin()) it=(c).begin(); it != (c).end(); ++it)
#define FOR(i,count) for (int i = 0; i < (count); i++)
 
using namespace std;
typedef long long ll;
const int MODULO = 1000000007;
 
typedef pair<int,int> Pii;
// begin

template<class T> T gcd(T a, T b) { return b ? gcd(b, a % b) : a; }
template<class T> T lcm(T a, T b) { return a * b / gcd(a,b); }

int get_n(int a,int m){
	int r = a;
	for (int i = 1; i < m; i++){
		if(r == 1) return i;
		r *= a;
		r %= m;
	}
	return -1;
}

int main()
{
	int a[3],m[3];
	while(cin>>a[0]>>m[0]>>a[1]>>m[1]>>a[2]>>m[2]){
		if(!(a[0]||a[1]||a[2]||m[0]||m[1]||m[2]))
			break;
		ll p[3];
		for (int i = 0; i < 3; i++){
			p[i] = get_n(a[i],m[i]);
		}
		ll l = lcm(lcm(p[0],p[1]),p[2]);
		cout << l << endl;
	}
    return 0;
}