#include <iostream>
#include <cstdio>

using namespace std;
bool flag;
unsigned long long a1,a2,a3,m1,m2,m3;
unsigned long long x,y,z,ans,calc,cnt1,cnt2,cnt3;
 
unsigned long long  gcd(unsigned long long a,unsigned long long b){
    unsigned long long result =a;
    unsigned long long k=0;
    unsigned long long n=b;
    do{
        k=result %n;
        result=n;
        n=k;
    }while(k!=0);
    return result;
}
int main(){
  while(scanf("%lld %lld %lld %lld %lld %lld", &a1,&m1,&a2,&m2,&a3,&m3),
	a1||a2||a3||m1||m2||m3){
        x=y=z=1;
        cnt1=cnt2=cnt3=0;
        while(1){
            cnt1++;
            x=(a1%m1)*(x%m1)%m1;
            if(x==1)break;
        }
        while(1){
            cnt2++;
            y=(a2%m2)*(y%m2)%m2;
            if(y==1)break;
        }
        while(1){
            cnt3++;
            z=(a3%m3)*(z%m3)%m3;
            if(z==1)break;
        }
        calc=cnt1/gcd(cnt1,cnt2)*cnt2;
        ans=cnt3/gcd(calc,cnt3)*calc;
        printf("%lld\n", ans);
    }
    return 0;
}