#include <iostream>
#include <cstdio>
#include <map>
using namespace std;
typedef long long int ll;
ll m[3],a[3];
ll gcd(int a,int b){
  if(a<b)swap(a,b);
  if(b == 0)return a;
  else return gcd(b,a%b);
} 

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