module AOJ_Volume0114;

import std.stdio,std.string,std.conv;

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

long lcm(long m,long n)
{
	if(m == 0 || n == 0) return 0;
	return (m/gcd(m,n))*n;
}

int main()
{
	while(true)
	{
		string[] s = readln.chomp.split(" ");
		long a[6];

		foreach(i;0..6) a[i] = s[i].to!int;

		if(a[0] == 0 && a[1] == 0 && a[2] == 0 && a[3] == 0 && a[4] == 0 && a[5] == 0) break;

		long x = 1,y = 1,z = 1,count_x = 1,count_y = 1,count_z = 1;

		while(true)
		{
			if((a[0]*x)%a[1] == 1) break;
			else
			{
				count_x++;
				x = (a[0]*x)%a[1];
			}
		}

		while(true)
		{
			if((a[2]*y)%a[3] == 1) break;
			else
			{
				count_y++;
				y = (a[2]*y)%a[3];
			}
		}

		while(true)
		{
			if((a[4]*z)%a[5] == 1) break;
			else
			{
				count_z++;
				z = (a[4]*z)%a[5];
			}
		}

		long ans = lcm(count_x,count_y);
		ans = lcm(ans,count_z);

		ans.writeln;
	}
	return 0;
}