#include<bits/stdc++.h>
using namespace std;
typedef long long int lld;

lld gcd( lld m, lld n ){
  if ( ( 0 == m ) || ( 0 == n ) ) return 0;
  while( m != n ){
    if ( m > n ) m = m - n;
    else         n = n - m;
  }
  return m;
}

lld lcm( lld m, lld n ){
  if ( ( 0 == m ) || ( 0 == n ) ) return 0;
  return ((m / gcd(m, n)) * n);
}

int main(){
  lld a[3],m[3];
  while(cin >> a[0] >> m[0] >> a[1] >> m[1] >> a[2] >> m[2] , a[0]){
    int now[3],cnt[3] = {1,1,1};
    for(int i=0;i<3;i++){
      now[i] = a[i] % m[i];
      while(now[i] != 1){
	now[i] = (a[i]*now[i])%m[i];
	cnt[i]++;
      }
    }
    cout << lcm(cnt[0],lcm(cnt[1],cnt[2])) << endl;
  }
}