#include <iostream>
#include <cstdio>
#include <algorithm>

using namespace std;

long long int LCM(long long int a, long long int b){
	long long int dummy1, dummy2, dummy;
	dummy1 = max(a, b);
	dummy2 = min(a, b);
	while(true){
		dummy = dummy1 % dummy2;
		dummy1 = dummy2;
		dummy2 = dummy;
		if(dummy == 0){
			break;
		}
	}
	return (a / dummy1) * (b / dummy1) * dummy1;
}

int main(){
	
	long long int a[3], m[3], span[3];
	
	for(int i = 0; i < 3; i++){
		cin >> a[i] >> m[i];
	}
	
	while(a[0] != 0){
		
		long long int dummy;
		
		for(int i = 0; i < 3; i++){
			span[i] = 1;
			dummy = a[i] % m[i];
			for(int loop = 1; loop < 1000000; loop++){
				if(dummy == 1){
					span[i] = loop;
					break;
				}
				dummy = (dummy * a[i]) % m[i];
			}
		}
		/*
		for(int i = 0; i < 3; i++){
			cout << span[i] << " ";
		}
		
		cout << endl;
		*/
		long long int ans;
		
		ans = LCM(LCM(span[0], span[1]), span[2]);
		
		cout << ans << endl;
		
		for(int i = 0; i < 3; i++){
			cin >> a[i] >> m[i];
		}
	}
	return 0;
}