#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<ll> VL;
typedef vector<VL> VVL;
typedef pair<int, int> PII;

#define FOR(i, a, n) for (ll i = (ll)a; i < (ll)n; ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(x) x.begin(), x.end()
#define MP make_pair
#define PB push_back
#define MOD 1000000007
#define INF (1LL<<30)
#define LLINF (1LL<<60)
#define PI 3.14159265359
#define EPS 1e-12
#define int ll

int gcd(int a, int b) {
  return b != 0 ? gcd(b, a%b) : a;
}
int lcm(int a, int b) {
  return a*b/gcd(a, b);
}

signed main(void)
{
  int a[3], m[3];
  while(cin>>a[0]>>m[0]>>a[1]>>m[1]>>a[2]>>m[2]) {
    if(!a[0] && !a[1] && !a[2] && !m[0] && !m[1] && !m[2]) break;
    int cnt[3] = {0}, x = 1, y = 1, z = 1;
    while(true) {
      x = (a[0]*x) % m[0];
      cnt[0]++;
      if(x == 1) break;
    }
    while(true) {
      y = (a[1]*y) % m[1];
      cnt[1]++;
      if(y == 1) break;
    }
    while(true) {
      z = (a[2]*z) % m[2];
      cnt[2]++;
      if(z == 1) break;
    }
    int tmp = lcm(cnt[0], cnt[1]);
    cout << lcm(tmp, cnt[2]) << endl;
  }

  return 0;
}