#include <bits/stdc++.h>
using namespace std;

int lcm(int a,int b){ return a*b/__gcd(a, b); }

int main(){
  int a1, m1, a2, m2, a3, m3;
  
  while(cin >> a1 >> m1 >> a2 >> m2 >> a3 >> m3, a1|m1|a2|m2|a3|m3){
    
    int x, y, z;
    x = a1 % m1;
    y = a2 % m2;
    z = a3 % m3;
    
    int X = 1, Y = 1, Z = 1;
    while(x != 1) x = x*a1 % m1, X++;
    while(y != 1) y = y*a2 % m2, Y++;
    while(z != 1) z = z*a3 % m3, Z++;
    
    cout << lcm(X, lcm(Y, Z)) << endl;
  }
  return 0;
}