#include <iostream>
#include <complex>
#include <sstream>
#include <string>
#include <algorithm>
#include <deque>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <vector>
#include <set>
#include <limits>
#include <cstdio>
#include <cctype>
#include <cmath>
#include <cstring>
#include <cstdlib>
#include <ctime>
using namespace std;
#define REP(i, j) for(int i = 0; i < (int)(j); ++i)
#define FOR(i, j, k) for(int i = (int)(j); i < (int)(k); ++i)
#define SORT(v) sort((v).begin(), (v).end())
#define REVERSE(v) reverse((v).begin(), (v).end())
typedef complex<double> P;
typedef long long int Int;
const int N = 3;

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

Int lcm(Int a, Int b){
  return a / gcd(a, b) * b;
}

int main() {
  Int A[N], M[N];
  while(true){
    REP(i, N) cin >>A[i] >>M[i];
    if(A[0] == 0) break;
    Int v[N], num[N];
    REP(i, N){
      Int cnt = 1;
      v[i] = A[i] % M[i];
      while(v[i] != 1){
        v[i] = A[i] * v[i] % M[i];
        ++cnt;
      }
      num[i] = cnt;
    }
    cout <<lcm(num[0], lcm(num[1], num[2])) <<endl;
  }
  return 0;
}