#include <bits/stdc++.h>

using namespace std;
using ll = long long;
using pii = pair<int, int>;

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

int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);
  cout << fixed << setprecision(10);
  
  int a[3], m[3];
  while (cin >> a[0] >> m[0]) {
    for (int i = 1; i < 3; i++) {
      cin >> a[i] >> m[i];
    }
    if (!a[0]) break;
    int cnt[3];
    for (int i = 0; i < 3; i++) {
      int p = a[i] % m[i];
      cnt[i] = 1;
      while (p != 1) {
        (p *= a[i]) %= m[i];
        cnt[i]++;
      }
    }
    cout << lcm(cnt[0], lcm(cnt[1], cnt[2])) << endl;
  }

  return 0;
}
