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

ll gcd(ll x, ll y){
  if(!(x >= y)) swap(x, y);
  while(!(x == 0 || y == 0)){
    x = x % y;
    swap(x, y);
  }
  return max(x, y);
}

int main(void){
  ios::sync_with_stdio(false);
  cin.tie(0);
  int a[3], m[3];
  while(cin >> a[0] >> m[0] >> a[1] >> m[1] >> a[2] >> m[2], a[0] | m[0] | a[1] | m[1] | a[2] | m[2]){
    ll T[3];
    for(int t = 0; t < 3; t++){
      ll x = a[t];
      for(T[t] = 2;; T[t]++){
        x = (x * a[t]) % m[t];
        if(x == 1) break;
      }
    }

    ll ans = T[0] * T[1] / gcd(T[0], T[1]);
    ans = ans * T[2] / gcd(ans, T[2]);
    cout << ans << endl;
  }
  return 0;
}