#include<iostream>
#include<algorithm>

using namespace std;

typedef long long lli;

lli solve(int& a, int& m){
  int x=1;
  lli cnt=1;
  while(1){
    x = (a*x)%m;
    if(x == 1) return cnt;
    ++cnt;
  }
}

int main(){
  int a1, m1, a2, m2, a3, m3;
  lli x, y, z, ans;
  while(1){
    cin >> a1 >> m1 >> a2 >> m2 >> a3 >> m3;
    if(!a1 && !m1 && !a2 && !m2 && !a3 && !m3) break;
    x = solve(a1, m1);
    y = solve(a2, m2);
    z = solve(a3, m3);
    ans = (x*y)/__gcd(x, y);
    ans = (ans*z)/__gcd(ans, z);
    cout << ans << endl;
  }
  return 0;
}