#include <iostream>
using namespace std;

long long lcm(long long m, long long n)
{
	return (n==0?m:lcm(n,m%n));
}

int main()
{
	long long c,x,y,z,ax,mx,ay,my,az,mz;
	while(cin >> ax >> mx >> ay >> my >> az >> mz, ax||mx||ay||my||az||mz)
	{
		x=0; c=1;
		do { x++; c=(c*ax)%mx; } while(c!=1);
		y=0; c=1;
		do { y++; c=(c*ay)%my; } while(c!=1);
		z=0; c=1;
		do { z++; c=(c*az)%mz; } while(c!=1);

		x=x/lcm(x,y)*y;
		x=x/lcm(x,z)*z;
		cout << x << endl;
	}
}