#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <iomanip>
#include <map>
#include <fstream>
#include <sstream>
#include <math.h>
#include <queue>
#include <stack>

using namespace std;

long long gcd( long long m, long long n ){
    if((0==m)||(0==n))return 0;
	
	// ユークリッドの方法
	while(m!=n){
		if (m>n) swap(m, n);
		n=n-m;
	}
	return m;
}

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

int a1, m1, a2, m2, a3, m3;

int main(){

    
    while(cin>>a1 >>m1 >>a2 >>m2 >>a3 >>m3){
        if(a1==0 && m1==0 && a2==0 && m2==0 && a3==0 && m3==0)break;
        
        long long int x,y,z,c1,c2,c3;
        x=1;
        y=1;
        z=1;
        c1=0;
        c2=0;
        c3=0;
        
        while(!(c1!=0&&x==1)){
            c1++;
            x=a1*x%m1;
        }
        while(!(c2!=0&&y==1)){
            c2++;
            y=a2*y%m2;
        }
        while(!(c3!=0&&z==1)){
            c3++;
            z=a3*z%m3;
        }
        
        //cout<<c1<<c2<<c3<<endl;
        cout<<lcm(lcm(c1,c2),c3)<<endl;
        
    }
}