#include <stdio.h>          // printf(), scanf()
#include <stdbool.h>

long
gcd(long a, long b)
{
	if (b == 0)
		return a;

	return gcd(b, a % b);
}

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

int
main(int argc, char **argv)
{
	while (true)
	{
		int a1, a2, a3;
		int m1, m2, m3;
		int x, y, z;
		long c1, c2, c3;

		scanf("%d %d %d %d %d %d", &a1, &m1, &a2, &m2, &a3, &m3);

		if (a1 == 0 && m1 == 0 && a2 == 0 && m2 == 0 && a3 == 0 && m3 == 0)
			break;

		x = 1;
		c1 = 0;
		do
		{
			x = (a1 * x) % m1;
			c1++;
		} while (x != 1);

		y = 1;
		c2 = 0;
		do
		{
			y = (a2 * y) % m2;
			c2++;
		} while (y != 1);

		z = 1;
		c3 = 0;
		do
		{
			z = (a3 * z) % m3;
			c3++;
		} while (z != 1);

		printf("%ld\n", lcm(lcm(c1, c2), c3));
	}

	return 0;
}