#include <iostream>
#include <string>
#include <algorithm>
#include <math.h>
#include <vector>
#include <set>

using namespace std;
typedef long long LL;
static const double EPS = 1e-9;

#define FOR(i,k,n) for (int i=(k); i<(int)(n); ++i)
#define REP(i,n) FOR(i,0,n) 
int mod(int a, int m){
  int c=0,x=1;
  do{
    x = a*x % m;
    c++;
  }while(x!=1);
  return c;
}
int gcd(int m,int n){
  return (n==0)?m:gcd(n,m%n);
}
int lcm(int m, int n){
  return m*n/gcd(m,n);
}
int main(void){
  int a,b,c,l,m,n;
  while(cin>>a>>l>>b>>m>>c>>n){
    if(!a) break;
    cout << lcm(mod(a,l),lcm(mod(b,m),mod(c,n))) << endl;
  }
  return 0;
}