#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <sstream>
#include <string>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <utility>
#include <cctype>
using namespace std;

#define rep(i,n) for(int (i)=0; (i)<(int)(n); ++(i))
#define foreach(c,i) for(__typeof((c).begin()) i=(c).begin();i!=(c).end();i++)
typedef long long ll;

vector<int> primes;
bool sieve[40000];

int main() {
  fill(sieve, sieve + 40000, true);
  sieve[0] = sieve[1] = false;
  for (int i = 2; i < 40000; ++i) {
    primes.push_back(i);
    for (int j = i+i; j < 40000; j+=i) 
      sieve[j] = false;
  }
  
  vector<int> a(3), m(3), lens(3);
  while (cin >> a[0] >> m[0], a[0]) {
    rep(i,2) cin >> a[i+1] >> m[i+1];
    rep(i,3) {
      int len = 0, cur = 1;
      do {
        cur = cur * a[i] % m[i];
        ++len;
      } while (cur != 1);
      lens[i] = len;
    }
    vector<vector<int> > prime_count(3, vector<int>(40000, 0));
    for (int i = 0; i < 3; ++i) {
      int value = lens[i];
      // printf("[%d]\n", value);
      for (int j = 0; j < primes.size() && value > 0; ++j) {
        while (value % primes[j] == 0) {
          value /= primes[j];
          ++prime_count[i][ primes[j] ];
          // printf(" %d\n", primes[j]);
        }
      }
    }

    ll ans = 1;
    for (int i = 0; i < primes.size(); ++i) {
      int MaxC = -1;
      rep(j,3) MaxC = max(prime_count[j][ primes[i] ], MaxC);
      rep(j,MaxC) ans *= primes[i];
    }
    printf("%lld\n", ans);
  }
  return 0;
}