#include<iostream>
using namespace std;
#define NUM 3

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

int main()
{
  int a[NUM],m[NUM],n[NUM],x;
  while(1){
    int ok=0;
    for(int i=0;i<NUM;i++){
      cin>>a[i]>>m[i];
      ok+=a[i]+m[i];
      n[i]=1;
    }
    if(!ok)break;

    for(int i=0;i<NUM;i++){
      for(x=a[i]%m[i];x!=1;x=(a[i]*x)%m[i])n[i]++;
    }
    cout<<lcm( lcm(n[0],n[1]), n[2] )<<endl;
  }
}