#include <iostream>
using namespace std;

long long gcd(long long a, long long b) {
  return b != 0 ? gcd(b, a % b) : a;
}
long long lcm(long long a, long long b) {
  return a * b / gcd(a, b);
}

int calc(int a, int m)
{
	int cnt=1, p=a%m;
	while(p!=1)
	{
		p=(a*p)%m;
		cnt++;
	}
	
	return cnt;
}

int main()
{
	while(1)
	{
		int a[3],m[3],f[3];
		for(int i=0; i<3; i++)
			cin >> a[i] >> m[i];
			
		if(a[0]==0) break;
			
		for(int i=0; i<3; i++)
			f[i]=calc(a[i], m[i]);
		
		cout << lcm(f[0], lcm(f[1], f[2])) << endl;
	}
}