#include <iostream>
#include <algorithm>
using namespace std;


typedef long long int lli;


int a[3], m[3];

int gcd(lli a, lli b) {
  if(b == 0) return a;
  return gcd(b, a%b);
}

int lcm(lli a, lli b) {
  return a / gcd(a, b) * b;
}

void solve() {
  int x = 1, y = 1, z = 1;
  lli nx = 0, ny = 0, nz = 0;
  for(lli i = 1; ; ++i) {
    if(nx == 0) {
      x = a[0]*x % m[0];
      if(x == 1) nx = i;
    }
    if(ny == 0) {
      y = a[1]*y % m[1];
      if(y == 1) ny = i;
    }
    if(nz == 0) {
      z = a[2]*z % m[2];
      if(z == 1) nz = i;
    }
    if(nx && ny && nz) {
      break;
    }
  }
  cout << lcm(lcm(nx, ny), nz) << endl;
}

main() {
  while(1) {
    bool allzero = true;
    for(int i = 0; i < 3; ++i) {
      cin >> a[i] >> m[i];
      if(a[i] || m[i]) allzero = false;
    }
    if(allzero) break;
    solve();
  }
}