#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <complex>
#include <string>
#include <sstream>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <functional>
#include <iostream>
#include <map>
#include <set>
using namespace std;
typedef pair<int,int> P;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vll;
#define pu push
#define pb push_back
#define mp make_pair
#define eps 1e-9
#define INF 2000000000
#define sz(x) ((int)(x).size())
#define fi first
#define sec second
#define SORT(x) sort((x).begin(),(x).end())
#define all(x) (x).begin(),(x).end()
#define EQ(a,b) (abs((a)-(b))<EPS)
int main()
{
	int a1,a2,a3,m1,m2,m3;
	while(1)
	{
		cin >> a1 >> m1 >> a2 >> m2 >> a3 >> m3;
		if(a1==0&&m1==0&&a2==0&&m2==0&&a3==0&&m3==0)break;
		int c1=1,c2=1,c3=1;
		int res=a1;
		while(res!=1)
		{
			c1++;
			res=res*a1%m1;
		}
		res=a2;
		while(res!=1)
		{
			c2++;
			res=res*a2%m2;
		}
		res=a3;
		while(res!=1)
		{
			c3++;
			res=res*a3%m3;
		}
		int tmp=c1/__gcd(c1,c2)*c2;
		int ans=tmp/__gcd(tmp,c3)*c3;
		cout << ans << endl;
	}
	return 0;
}