#include<iostream>
using namespace std;
long long gcd(long long a,long long b){
  return b?gcd(b,a%b):a;
}
long long lcm(long long m,long long n){
  return m/gcd(m,n)*n;
}

int main(){
  int a,p;
  long long count[3];
  while(true){
    cin>>a>>p;
    if(!a&&!p)break;
    for(int i=0;i<3;i++){
      if(i)cin>>a>>p;
      int k=1;
      count[i] = 0;
      do{
	k*=a;
	k%=p;
	count[i]++;
      }while(k!=1);
    }
    cout<<lcm(lcm(count[0],count[1]),count[2])<<endl;
  }
  return 0;
}