#include<bits/stdc++.h>
using namespace std;
//????°???¬?????°
long long koubaisu(long long a,long long b){
  long long x=a*b;
  /* ?????¶??° a > b ????¢?????????\??? */ 
  if(a<b){ 
    long long tmp=a; 
    a=b; 
    b=tmp; 
  }
  /* ?????????????????????????????? */ 
  long long r=a%b; 
  while(r!=0){ 
    a=b; 
    b=r; 
    r=a%b; 
  } 
  /* ????°???¬?????°????????? */ 
  return x/b; 
}
int main(){
  long long a[3],m[3];
  while(cin>>a[0]>>m[0]>>a[1]>>m[1]>>a[2]>>m[2],a[0]+a[1]+a[2]+m[0]+m[1]+m[2])
    {
      long long x=a[0]%m[0],y=a[1]%m[1],z=a[2]%m[2];
      long long cnt=0;
      long long indx,indy,indz;
      if(x==1)indx=1;
      else
	for(int i=0;i<m[0]-1;i++){
	  if(x==1){
	    indx=i+1;
	    break;
	  }
	  x=(a[0]*x)%m[0];
	}
      if(y==1)indy=1;
      else
	for(int i=0;i<m[1]-1;i++){
	  if(y==1){
	    indy=i+1;
	    break;
	  }
	  y=(a[1]*y)%m[1];
	}
      if(z==1)indz=1;
      else      
	for(int i=0;i<m[2]-1;i++){
	  if(z==1){
	    indz=i+1;
	    break;
	  }
	  z=(a[2]*z)%m[2];
	}
      long long ind;
      ind=koubaisu(indx,indy);
      cnt=koubaisu(ind,indz);
      cout<<cnt<<endl;
    }
  return 0;
}