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

long long solve(long long a, long long m) {
  long long count;
  long long x = 1;

  x =(a*x) % m;
  for (count=1; x!=1; count++) {
    x = (a*x) % m;
  }

  return count;
}

long long gcd(long long x1, long long x2) {
  long long m = max(x1, x2);
  long long n = min(x1, x2);

  while (n != 0) {
    long long tmp = n;
    n = m%n;
    m = tmp;
  }
  return m;
}

long long lcm(long long x1, long long x2) {
  long long n_gcd = gcd(x1, x2);
  return (x1/n_gcd)*x2;
}

int main() {
  int a1, a2, a3;
  int m1, m2, m3;
  
  while(scanf("%d %d %d %d %d %d",&a1,&m1,&a2,&m2,&a3,&m3),
	a1||a2||a3||m1||m2||m3) {
    long long count1 = solve((long long)a1, (long long)m1);
    long long count2 = solve((long long)a2, (long long)m2);
    long long count3 = solve((long long)a3, (long long)m3);
    
    printf("%lld\n", lcm(lcm(count1, count2), count3)); 
  }

}