import std.stdio, std.conv, std.algorithm, std.string, std.array, std.numeric;

long seq(int a, int m) {
  int cnt = 1;
  int x = 1;
  while(true) {
    x = (a * x) % m;
    if (x == 1) break;
    else
      cnt++;
  }
  return cnt.to!long;
}

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

long lcms(long[] arr) {
  long res = arr[0];
  for(int i = 1; i < arr.length; i++) {
    res = lcm(res, arr[i]);
  }
  return res;
}

void main() {
  while(true) {
    int[] a = readln.chomp.split(" ").map!(to!int).array;
    if (a[0] == 0) break;
    lcms([seq(a[0], a[1]), seq(a[2], a[3]), seq(a[4], a[5])]).writeln;
  }
}