#include<iostream>
using namespace std;

long long int niche(long long int a, long long int m) {
  long long int w = a, n = 1;
  while (w % m != 1) {
    n++;
    w = (w % m) * a;
  }
  return n;
}

long long int gcd(long long int a, long long int b) {
  return (b == 0) ? a:gcd(b, a % b);
}

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

long long int lcm(long long int a, long long int b, long long int c) {
  return lcm( lcm(a, b), c);
}

int main() {
  long long int a1, m1, a2, m2, a3, m3;

  while (cin >> a1 >> m1 >> a2 >> m2 >> a3 >> m3) {
    if (a1 * m1 * a2 * m2 * a3 * m3 == 0)
      break;

    cout << lcm(niche(a1, m1), niche(a2, m2), niche(a3, m3)) << endl;
  }
}