#include <iostream>
#include <stdio.h>
using namespace std;

int gcd(int m, int n){
  if (m>n) return gcd(n,m);
  if (m==0) return n;
  return gcd(n-m, m);
}

long long int lcm(long int n, long int m){
  return n*m/gcd(n,m);
}

int main(void){
  int a[3], m[3], pos[3], t[3];

  while (1){
    for (int i=0; i<3; i++){
      scanf("%d%d", &a[i], &m[i]);
      pos[i] = 1;
      t[i] = 1;
    }
    if (a[0]==0) break;
    for (int i=0; i<3; i++){
      while (pos[i]*a[i] % m[i] != 1) {
	//cout << a[i] << m[i] << endl;
	pos[i] = pos[i]*a[i] % m[i];
	t[i]++;
      }
      //      cout << t[i] << endl;
    }
    cout << lcm(lcm(t[0], t[1]), t[2]) << endl;
  }

  return 0;
}
      