#include <iostream>
#include <cstdio>

using namespace std;

int count(int a, int m){
	int c = 1;
	int x = a;
	while(x != 1){
		x = (a * x) % m;
		++c;
	}
	return c;
}

int gcd(int a, int b){
	int c;
	while(b){
		c = a % b;
		a = b;
		b = c;
	}
	return a;
}

int lcm(int a, int b){
	return a / gcd(a, b) * b;
}

int main(){
	int a1, a2, a3, m1, m2, m3;
	int c1, c2, c3;
	while(scanf("%d %d %d %d %d %d", &a1, &m1, &a2, &m2, &a3, &m3)){
		if(!a1) break;
		c1 = c2 = c3 = 0;
		a1 = a1 % m1;
		a2 = a2 % m2;
		a3 = a3 % m3;
		c1 = count(a1, m1);
		c2 = count(a2, m2);
		c3 = count(a3, m3);
		printf("%d\n", lcm(lcm(c1, c2), c3));
	}
	return 0;
}