#include<iostream>

using namespace std;

int x,y,z;
int m[3],a[3];

int GCD(int a,int b){
  if(b == 0) return a;
  return GCD(b,a%b);
}

int main(){

  while(1){
    int sum = 0;

    for(int i = 0; i < 3; i++){
      cin >> a[i] >> m[i];
      sum += a[i]+m[i];
    }

    if(sum == 0) break;
 
    int pos[3] = {1,1,1};
    int t[3] = {0,0,0};

    for(int i = 0; i < 3; i++){
      while(1){
	pos[i] = (pos[i]*a[i])%m[i];
	t[i]++;
	if(pos[i] == 1) break;
      }
    }
    int tmp = (t[0]*t[1])/GCD(t[1],t[0]);
    int ans = (tmp*t[2])/GCD(tmp,t[2]);
    cout << ans << endl;
  }
  return 0;
}