#include <iostream>
#include <algorithm>

using namespace std;

typedef long long int lli;

lli gcd(lli x, lli y)
{
  if(y == 0) return x;
  return gcd(y, x%y);
}

lli lcm(lli x, lli y)
{
  return x*y/gcd(x, y);
}

lli func(lli c[3])
{
  lli res = 1;
  for(int i = 0; i < 3; ++i)
    res = lcm(res, c[i]);

  return res;
}

int main()
{
  lli s, t, u, a[3], m[3], c[3];

  while(1){
    t = 0;
    for(int i = 0; i < 3; ++i){
      cin >> a[i] >> m[i];
      t += (a[i]+m[i]);
      c[i] = 1;
    }
    if(t == 0) break;

    for(int i = 0; i < 3; ++i){
      t = a[i]%m[i];
      while(t != 1){
	c[i]++;
	t = (t*a[i])%m[i];
      }
    }

    cout << func(c) << endl;
  }

  return 0;
}