#include<iostream>
#include<algorithm>
#include<vector>
#include<stack>
#include<queue>
#include<cstdio>
#include<climits>
#include<cmath>
#include<cstring>
#include<string>
#include<complex>

#define f first
#define s second
#define mp make_pair

#define REP(i,n) for(int i=0; i<(int)(n); i++)
#define REPS(i,s,n) for(int i=(s); 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()

using namespace std;

typedef unsigned int uint;
typedef long long ll;
typedef complex<double> P;

ll gcd(ll a, ll b){
  if(a > b) swap(a, b);
  if(a == 0) return b;
  return gcd(a, b%a);
}

int main(){
  int a[3], m[3];
  while(true){
    int c[3] = {0};
    REP(i,3) scanf("%d%d",&a[i],&m[i]);
    if(a[0] == 0) return 0;

    REP(i,3){
      int x = 1;
      do{
	c[i]++;
	x = (a[i] * x) % m[i];
      }while(x!=1);
    }

    ll ans = (ll)c[0] * c[1] / gcd(c[0], c[1]);
    ans = ans * c[2] / gcd(ans, c[2]);
    cout << ans << endl;
  }
}