#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 <cstring>
#include <sstream>
#include <cassert>
#include <list>
#include <fstream>
using namespace std;
static const double EPS = 1e-5;
typedef long long ll;
typedef pair<int,int> PI;
typedef vector<int> vi;
#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif
#define rep(i,n) for(int i=0;i<(int)n;++i)
#define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)
#define ALL(c) (c).begin(), (c).end()
#define mp(a,b) make_pair(a,b)
#define pb(a) push_back(a)
#define F first
#define S second


int dx[]={0,1,0,-1};
int dy[]={1,0,-1,0};

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

main(){
  ll a[3],m[3];
  ll x[3];
  while(true){
    rep(i,3){
      cin>>a[i]>>m[i];
      x[i]=1;
    }
    if(a[0]==0)break;
    

    int n[3];
    rep(i,3){
      n[i]=1;
      x[i]=x[i]*a[i]%m[i];
      while(x[i]!=1){
	n[i]++;
	x[i]=x[i]*a[i]%m[i];
      }
    }
    cout<<lcm(lcm(n[0],n[1]),n[2])<<endl;
  }
}