#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;
long long int gcd(long long  int m,long long int n )
{
  if ( ( 0 == m ) || ( 0 == n ) )
    return 0;  
  while( m != n )
    {
      if ( m > n ) m = m - n;
      else         n = n - m;
    }
  return m;
}
long long int lcm(long long  int m,long long int n )
{
  if ( ( 0 == m ) || ( 0 == n ) )
    return 0;
  
  return ((m / gcd(m, n)) * n); // lcm = m * n / gcd(m,n)
}


int main()
{
  int a1,m1,a2,m2,a3,m3;
  for(;cin>>a1>>m1>>a2>>m2>>a3>>m3,a1;)
    {
      long long int a=0,b=0,c=0,ans;
      long long int x=1;
      do{
	x=(a1*x)%m1;
	a++;
      }while(x!=1);
      do{
	x=(a2*x)%m2;
	b++;
      }while(x!=1);
      do{
	x=(a3*x)%m3;
	c++;
      }while(x!=1);
      //      cout<<a<<" "<<b<<" "<<c<<endl; 
      cout<<lcm(lcm(a,b),c)<<endl;
    }
}