#include <map>
#include <set>
#include <list>
#include <cmath>
#include <queue>
#include <stack>
#include <cstdio>
#include <string>
#include <vector>
#include <complex>
#include <cstdlib>
#include <cstring>
#include <numeric>
#include <sstream>
#include <iostream>
#include <algorithm>
#include <functional>

#define mp       make_pair
#define pb       push_back
#define all(x)   (x).begin(),(x).end()
#define rep(i,n) for(int i=0;i<(n);i++)
#define repi(i,a,b) for(int i = (a); i < (b); i++)

using namespace std;

typedef    long long          ll;
typedef    unsigned long long ull;
typedef    vector<bool>       vb;
typedef    vector<int>        vi;
typedef    vector<vb>         vvb;
typedef    vector<vi>         vvi;
typedef    pair<int,int>      pii;

const int INF=1<<29;
const double EPS=1e-9;

const int dx[]={1,0,-1,0},dy[]={0,-1,0,1};
ll gcd(ll x,ll y){
	if (y >0){
		gcd(y,x%y);
	}else{
		return x;
	}
}
ll lcm (ll x,ll y){
	return x/gcd(x,y)*y;
}


int main(){
	ll a1,a2,a3,m1,m2,m3;
	while(cin>>a1>>m1>>a2>>m2>>a3>>m3,a1||a2||a3||m1||m2||m3){
		ll x,y,z,a,b,c;
		a =b = c = x = y = z = 1;
		x =(a1*x)%m1;
		y =(a2*y)%m2;
		z =(a3*z)%m3;
		for(;x !=1;a++){
			x =(x*a1)%m1;
		}
		for(;y !=1;b++){
			y =(y*a2)%m2;
		}
		for(; z!=1;c++){
			z =(z*a3)%m3;
		}
		ll cnt =lcm(lcm(a,b),c);
		cout <<cnt<<endl;
	}
	return 0;
}