#include<iostream>
using namespace std;

long long int gcd(long long int a,long long int b){
  if(!b)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(){
  int a1,m1,a2,m2,a3,m3;

  while(1){
    cin >> a1 >> m1 >> a2 >> m2 >> a3 >> m3;
    if(!a1 && !m1 && !a2 && !m2 && !a3 && !m3)break;

    int x = 1,y = 1,z = 1;
    long long int ans1 = 0,ans2 = 0,ans3 = 0;
    do{
      ans1++;
      x = (a1*x)%m1;
    }while(x!=1);
    do{
      ans2++;
      y = (a2*y)%m2;
    }while(y!=1);
    do{
      ans3++;
      z = (a3*z)%m3;
    }while(z!=1);


    cout << lcm(ans1,lcm(ans2,ans3)) << endl;
  }
}