
#include<iostream>
using namespace std;

long long int gcd(long long int a,long long int b);
long long int lcm(long long int a,long long int b);

long long int gcd(long long int a,long long int b){
  if(b==0)return a;
  return gcd(b,a%b);
}
long long int lcm(long long int a,long long int b){
  return a*b/gcd(a,b);
}

int main()
{
  long long int a1,a2,a3,m1,m2,m3;
  long long int x,y,z;
  long long int count;

  while (cin>>a1>>m1>>a2>>m2>>a3>>m3, a1||a2||a3||m1||m2||m3) {
    x = (1 * a1) % m1;
    y = (1 * a2) % m2;
    z = (1 * a3) % m3;
    count = 1;
    while (x!=1) {
      x = (x * a1) % m1;
      count ++;
    }
    x = count;
    count = 1;
    while (y!=1) {
      y = (y * a2) % m2;
      count ++;
    }
    y = count;
    count = 1;
    while (z!=1) {
      z = (z * a3) % m3;
      count ++;
    }
    z = count;
    cout<<lcm( x, lcm(y, z) )<<endl;
  }
  return 0;
}