#include <cstdio>

long long gcd(long long x, long long y){
  if(y == 0) return x;
  return gcd(y, x%y);
}

long long lcm(long long x, long long y){
  return (x*y)/gcd(x, y);
}

main(){
  long long a1, m1, a2, m2, a3, m3;
  while(1){
    scanf("%lld %lld %lld %lld %lld %lld", &a1, &m1, &a2, &m2, &a3, &m3);
    if(a1 == 0 && m1 == 0 && a2 == 0 && m2 == 0 && a3 == 0 && m3 == 0) break;
    long long a=0, b=0, c=0;
    long long x=a1*1%m1, y=a2*1%m2, z=a3*1%m3;
    while(1){
      a++;
      if(x == 1) break;
      x = a1*x%m1;
    }
    while(1){
      b++;
      if(y == 1) break;
      y = a2*y%m2;
    }
    while(1){
      c++;
      if(z == 1) break;
      z = a3*z%m3;
    }
    printf("%lld\n", lcm(a, lcm(b, c)));
  }
}