#include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
#include <complex>
#include <cstring>
#include <cstdlib>
using namespace std;

#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()

typedef long long ll;

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

int main() {
  ll  a[3],m[3];
  while(cin >>a[0]>>m[0]>>a[1]>>m[1]>>a[2]>>m[2],a[0]!=0) {
    ll ans[3] = {0};
    REP(k,3) {
      ll x=1;
      do {
        x = a[k]*x % m[k];
//      printf("[%d,%d,%d]\n",x,y,z);
        ans[k]++;
      } while(x!=1);
    }

    ll hoge = ans[0]*ans[1]/gcd(ans[0],ans[1]);

    cout << hoge*ans[2]/gcd(hoge,ans[2]) << endl;
  }
}