#include <iostream>
#include <vector>
#include <string>
#include <map>
#include <algorithm>

#define REP(i,n) for(int i=0;i<(int)(n);i++)

using namespace std;

int64_t calc(int64_t a, int64_t m) {
  int64_t r = 1;
  for (int64_t i = 1; i < m * 2; ++i) {
    r *= a;
    r %= m;
    if (r == 1) return i;
  }
  return 0;
}

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

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

int main() {
  while(1){
    int a[3],m[3];
    scanf("%d%d%d%d%d%d",a,m,a+1,m+1,a+2,m+2);
    if(!a[0])break;
    int64_t k[3];
    REP(i,3){
      k[i]=calc(a[i],m[i]);
    }
    cout << lcm(k[0],lcm(k[1],k[2])) << endl;
  }
  return 0;
}