#include <cstdio>
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long ll;
ll a[3],m[3],c[3];

ll gcd(ll x,ll y){
  if(!y) return x;
  return gcd(y,x%y);
}

ll lcm(ll x,ll y){
  if(x < y) swap(x,y);
  return x*y/gcd(x,y);
}
int main(void){
  while(cin >> a[0] >> m[0] >> a[1] >> m[1] >> a[2] >> m[2] && a[0]){
    for(int i = 0; i < 3; i++){
      ll x = 1;
      c[i] = 0;
      do{
	x = (x*a[i])%m[i];
	c[i]++;
      }while(x!=1);
    }
    cout << lcm(c[0],lcm(c[1],c[2])) << endl;
  }
}