#include <cstdio>
using namespace std;
typedef long long int LLI;

LLI gcd(LLI a, LLI b) {
  int c, d, t;
  c = a;
  d = b;
  while (1) {
    t = c % d;
    if (!t) break;
    c = d;
    d = t;
  }
  return d;
}

LLI lcm(int a, int b) {
  int c = gcd(a, b);
  return (LLI) a * b / c;
}

int main() {
  LLI a[3], m[3], b[3];
  LLI c, s, x;
  while (1) {
    scanf("%lld %lld %lld %lld %lld %lld", &a[0], &m[0], &a[1], &m[1], &a[2], &m[2]);
    if (!a[0] && !m[0] && !a[1] && !m[1] && !a[2] && !m[2]) break;
    for (int i=0; i<3; i++) {
      s = 1;
      c = 0;
      do {
        s *= a[i];
        s %= m[i];
        ++c;
      } while (s!=1);
      b[i] = c;
    }
    x = lcm(b[0], b[1]);
    x = lcm(b[2], x);
    printf("%lld\n", x);
  }
  return 0;
}