#include <iostream>
#include <map>
#include <string>
#include <cstdio>
using namespace std;

int GCD(int a,int b){
	int less = min(a,b);
	int gcd = 1;
	for(int i=1;i<=less;++i){
		if( a%i==0 && b%i==0 )gcd = i;
	}
	return gcd;
}

int LCM(int a,int b){
	return a*b/GCD(a,b);
}

int LCM(int a,int b,int c){
	return LCM(a,LCM(b,c));
}

int main() {
	int a1,a2,a3,m1,m2,m3;
	int x,y,z;
	int cnt;
	int l,m,n;
	
	while(cin>>a1>>m1>>a2>>m2>>a3>>m3, a1|a2|a3|m1|m2|m3){
		x=y=z=1;
		l=m=n=0;
		
		do{
			x = (a1*x)%m1;
			++l;
		}while(x-1);
		do{
			y = (a2*y)%m2;
			++m;
		}while(y-1);
		do{
			z = (a3*z)%m3;
			++n;
		}while(z-1);
		
		cout << LCM(l,m,n) << endl;
	}
	return 0;
}