#include<bits/stdc++.h>
using namespace std;

typedef long long int ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<pair<int, int> > vii;
#define rrep(i, m, n) for(int (i)=(m); (i)<(n);  (i)++)
#define erep(i, m, n) for(int (i)=(m); (i)<=(n); (i)++)
#define  rep(i, n)    for(int (i)=0; (i)<(n);  (i)++)
#define rrev(i, m, n) for(int (i)=(n)-1; (i)>=(m); (i)--)
#define erev(i, m, n) for(int (i)=(n); (i)>=(m); (i)--)
#define  rev(i, n)    for(int (i)=(n)-1; (i)>=0; (i)--)
#define vrep(i, c)    for(__typeof((c).begin())i=(c).begin(); i!=(c).end(); i++)
#define  ALL(v)       (v).begin(), (v).end()
#define mp            make_pair
#define pb            push_back
template<class T, class S> inline bool minup(T& m, S x){ return m>(T)x ? (m=(T)x, true) : false; }
template<class T, class S> inline bool maxup(T& m, S x){ return m<(T)x ? (m=(T)x, true) : false; }

const int    INF = 1000000000;
const ll     MOD = 1000000007LL;
const double EPS = 1E-12;

template<typename T> T extgcd(T a, T b, T& x, T& y)
{
  T d = a;
  if(b != (T)0){
    d = extgcd(b, a % b, y, x);
    y -= (a / b) * x;
  }
  else{
    x = (T)1;
    y = (T)0;
  }
  return d;
}

template<typename T> T modInverse(T a, T m)
{
  T x, y;
  extgcd(a, m, x, y);
  return (m + x % m) % m;
}

template<class T> T gcd(T a, T b)
{
  T tmp;

  while(b){
    tmp = a;
    a = b;
    b = tmp%b;
  }

  return a;
}

template<class T> inline T lcm(T a, T b){ return a * (b / gcd(a, b)); }

ll a[3], m[3];

int main()
{
  while((cin >> a[0] >> m[0] >> a[1] >> m[1] >> a[2] >> m[2])){
    if(!a[0]) break;

    ll cnt[3], h[3];
    rep(i, 3) cnt[i] = 0LL;
    rep(i, 3) h[i] = a[i];
    rep(i, 3){
      while(h[i] > 1){
        cnt[i] += 1LL;
        h[i] = (h[i] * a[i]) % m[i];
      }
    }

    ll res = 1LL;
    rep(i, 3) res = lcm(res, cnt[i] + 1);
    cout << res << endl;
  }

  return 0;
}