#include<iostream>
using namespace std;

unsigned int GCD(unsigned int a, unsigned int b){
  while( true ){
    unsigned int r = a % b;
    if( r == 0 ) return b;
    else{
      a = b;
      b = r;
    }
  }
}
unsigned int LCM(unsigned int a, unsigned int b){
  unsigned int gcd = GCD( a, b );
  return (a / gcd) * b;
}

int main(void){
  while( true ){
    unsigned int moves = 0;
    int x = 1, y = 1, z = 1;
    unsigned int a1,m1,a2,m2,a3,m3;
    unsigned int lcm1 = 0, lcm2 = 0, lcm3 = 0;
    
    cin >> a1 >> m1 >> a2 >> m2 >> a3 >> m3;
    if( a1 == 0 && m1 == 0 && a2 == 0 && m2 == 0 && a3 == 0 && m3 == 0 ) break;
    while( true ){ x = (a1 * x) % m1; lcm1++; if( x == 1 ) break; }
    while( true ){ y = (a2 * y) % m2; lcm2++; if( y == 1 ) break; }
    while( true ){ z = (a3 * z) % m3; lcm3++; if( z == 1 ) break; }
    moves = LCM( lcm1, LCM( lcm2, lcm3 ) );
    cout << moves << '\n';
  }
  return 0;
}