#include<iostream>
#include<algorithm>
using namespace std;
typedef long long ll;
ll lcm( ll a, ll b){
  return a / __gcd( a, b) * b;
}
int main(){
  ll a[3], m[3];
  while(cin >> a[0] >> m[0] >> a[1] >> m[1] >> a[2] >> m[2], a[0]){
    ll cnt[3] = {1, 1, 1}, jv[3];
    for(int i = 0 ; i < 3 ; i++ ){
      jv[i] = a[i] % m[i];
      while(jv[i] != 1){
        jv[i] = (jv[i] * a[i]) % m[i];
        cnt[i]++;
      }
    }
    cout << lcm(cnt[0],lcm(cnt[1],cnt[2])) << endl;
  }
}