#include <iostream>
using namespace std;
int a[3],m[3];
int ans = 0;

int freq(int a1, int m1, int x) {
  x = (a1*x)%m1;
  if(x == 1) return 1;
  else return freq(a1,m1,x)+1;
}

int gcd(int x,int y) {
  if(y == 0) return x;
  else return gcd(y ,x % y);
}

int main() {
  while(cin >> a[0] >> m[0] >> a[1] >> m[1] >> a[2] >> m[2]) {
    if(a[0] == 0 && m[0] == 0 && a[1] == 0 && m[1] == 0 && a[2] == 0 && m[2] == 0) break;
    int fa = freq(a[0],m[0],1);
    int fb = freq(a[1],m[1],1);
    int fc = freq(a[2],m[2],1);
    int LCMab = fa*fb/gcd(fa,fb);
    int LCMbc = fb*fc/gcd(fb,fc);
    long long int LCMabc = (long long int)LCMab*(long long int)LCMbc/(long long int)gcd(LCMab,LCMbc);
    cout << LCMabc << endl;
  }
}