#include <stdio.h>
#include <vector>
#include <algorithm>
#include <stack>

using namespace std;

long long calc(long long x,long long y){
	if(y == 0){
		return x;
	}else{
		return calc(y,x%y);
	}
}

int main(){
	long long int a1,a2,a3,m1,m2,m3,min_x,min_y,min_z,x,y,z,pre,ans;

	while(true){
		scanf("%lld %lld %lld %lld %lld %lld",&a1,&m1,&a2,&m2,&a3,&m3);
		if(a1 == 0 && m1 == 0 && a2 == 0 && m2 == 0 && m3 == 0 && m3 == 0)break;

		x = 1;
		for(min_x = 1; min_x <= m1;min_x++){
			x = a1*x%m1;
			if(x == 1)break;
		}

		y = 1;
		for(min_y = 1; min_y <= m2; min_y++){
			y = a2*y%m2;
			if(y == 1)break;
		}

		z = 1;
		for(min_z = 1; min_z <= m3; min_z++){
			z = a3*z%m3;
			if(z == 1)break;
		}

		if(min_x > min_y){
			pre = ((min_x/(calc(min_x,min_y)))*min_y);
		}else{
			pre = ((min_y/(calc(min_y,min_x)))*min_x);
		}

		if(pre > min_z){
			ans = ((pre/(calc(pre,min_z)))*min_z);
		}else{
			ans = ((min_z/(calc(min_z,pre)))*pre);
		}
		printf("%lld\n",ans);
	}
    return 0;
}