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

using namespace std;

int gcd(int a, int b) {
	if ((a == 0) || (b == 0))
		return 0;
	while (a != b){
		if (a > b) a = a-b;
		else 	   b = b-a;
	}
		return a; 
}

int lcm(int a, int b){
	if ((a == 0) || (b == 0))
		return 0;
	return ((a/gcd(a, b))*b);
}

int main(){
	int x, y, z, flag, a[3], m[3];
	while (1){
		int x = 1, y = 1, z = 1, flag = 1;
		for (int i = 0; i < 3; i++){
			scanf("%d%d", &a[i], &m[i]);
			if (a[i] != 0 || m[i] != 0) flag = 0;
		}
		if (flag) break;
		int countx = 0, county = 0, countz = 0;
		while (1){
			x = (a[0]*x)%m[0];
			countx++;
			if (x == 1) break;
		}
		while (1){
			y = (a[1]*y)%m[1];
			county++;
			if (y == 1) break;
		}
		while (1){
			z = (a[2]*z)%m[2];
			countz++;
			if (z == 1) break;
		}
		int re = lcm(countx, county);
		re = lcm(re, countz);
		printf("%d\n", re);
		
	}
	return 0;
}