#include<iostream>
#include<cmath>
#include<algorithm>
using namespace std;

int main(void){
  long long a[3],m[3];
  while(cin>>a[0]>>m[0],a[0]+m[0]){
    for(int i = 1 ; i  < 3 ; i ++){
      cin>>a[i]>>m[i];
    }
    for(int i = 0 ; i < 3 ; i ++){
      long long co = 0,d = 1;
      while(true){
	co ++;
	d = (a[i] * d) % m[i];
	if(d==1)break;
      }
      a[i]=co;
    }
    long long d = a[1]*a[2]/__gcd(a[1],a[2]);
    cout<<d*a[0]/__gcd(a[0],d)<<endl;

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