#include <iostream>
using namespace std;
long long gcd(long long a,long long b)
{
  long long r = a % b;
  if(r == 0) return b;
  return gcd(b,r);
}
long long lcm(long long a,long long b)
{
  long long g = a > b ? gcd(a,b) : gcd(b,a);
  return a / g * b;
}
int main()
{
  int a1,m1,a2,m2,a3,m3,x,y,z;
  long long i,a,b,c;
  for(;;){
    cin >> a1 >> m1 >> a2 >> m2 >> a3 >> m3;
    if(a1==0) break;
    x = y = z = 1;
    a = b = c = 0;
    i = 0;
    for(;;){
      if(a == 0) x = a1 * x % m1;
      if(b == 0) y = a2 * y % m2;
      if(c == 0) z = a3 * z % m3;
      i++;
      if(a == 0 && x == 1) a = i;
      if(b == 0 && y == 1) b = i;
      if(c == 0 && z == 1) c = i;
      if (a != 0 && b != 0 && c != 0){
	cout << lcm(a,lcm(b,c)) << endl;
	break;
      }
    }
  }
  return 0;
}