#include <iostream>

#define rep(X, Y) for( int X = 0; X < Y; ++X )

typedef long long ll;

ll GCM( ll x, ll y ){
	while( 1 ){
		if( x > y ) x -= y;
		else if( x < y ) y -= x;
		else return x;
	}
}

ll LCM( ll x, ll y ){
	return x / GCM( x, y ) * y;
}

int main(){
	ll p[3], cnt[3], a[3], m[3], sum;

	while( 1 ){
		sum = 0;

		rep(i, 3){
			p[i] = 1, cnt[i] = 0;
			std::cin >> a[i] >> m[i];
			sum += a[i] + m[i]; 
		}

		if( sum == 0 ) break;

		rep(i, 3){
			do{
				cnt[i]++;
				p[i] = (p[i] % m[i] * a[i] % m[i]) % m[i];
			}while( p[i] != 1 );
		}

		ll lcm = 1;
		rep(i, 3) lcm = LCM( cnt[i], lcm );

		std::cout << lcm << std::endl;
	}

	return 0;
}