#include<iostream>
#include<cstdio>

using namespace std;
typedef unsigned long long int ull;
ull a1,a2,a3,m1,m2,m3;
bool fin = false;


int rec(int r,ull A,ull M,ull cnt){
  ull foggy = (A*r)%M;
  if(foggy == 1){
    return cnt;
  }
  return rec(foggy,A,M,cnt+1);
}

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

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

int main(){
 ull X,Y,Z;  
  while(true){
    cin >> a1 >> m1 >> a2 >> m2 >> a3 >> m3;
    if(a1+a2+a3+m1+m2+m3 == 0)break;
    X = rec(1,a1,m1,1);
    Y = rec(1,a2,m2,1);
    Z = rec(1,a3,m3,1);
    ull XY = lcm(X,Y);
    cout << lcm(XY,Z) << endl;
    fin = false;
  }
  return 0;
}