#include <iostream>

using namespace std;

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

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

int lcm_3(int a, int b, int c) {
  int x;
  x = a * b / gcd(a, b);
  return x * c / gcd(x, c);
}

int main() {
  int a1, m1, a2, m2, a3, m3;
  while (cin >> a1 >> m1 >> a2 >> m2 >> a3 >> m3) {
    if (a1 == 0 && m1 == 0 && a2 == 0 && m2 == 0 && a3 == 0 && m3 == 0) break;
    int x = 1, y = 1, z = 1;
    int x_cycle = 0, y_cycle = 0, z_cycle = 0;
    while (true) {
      x_cycle++;
      x = a1 * x % m1;
      if (x == 1) break;
    }
    while (true) {
      y_cycle++;
      y = a2 * y % m2;
      if (y == 1) break;
    }
    while (true) {
      z_cycle++;
      z = a3 * z % m3;
      if (z == 1) break;
    }
    cout << lcm_3(x_cycle, y_cycle, z_cycle) << endl;
  }
  return 0;
}