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

int main() {
  ll a[3],m[3];
  while(1) {
    for(int i=0; i<3; i++) cin >> a[i] >> m[i];
    if(!a[0]) break;
    ll ans=1;
    for(int i=0; i<3; i++) {
      ll x=a[i],c=1;
      while(x!=1) {
        x=x*a[i]%m[i];
        c++;
      }
      ans=c/__gcd(ans,c)*ans;
    }
    cout << ans << endl;
  }
  return 0;
}
