#include <iostream>
#include <algorithm>
#include <cstdlib>
#include <set>
#include <vector>
#include <map>
#include <cstring>
#include <climits>
#define int long long
using namespace std;

int gcd(int a,int b)
{
	if(b>a)swap(a,b);
	if(a%b==0)return b;
	else return gcd(b,a%b);
}

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

main()
{
	int a[3],m[3];
	
	while(cin>>a[0]>>m[0]>>a[1]>>m[1]>>a[2]>>m[2]&&a[0])
	{
		int b[3]={};
		for(int i=0;i<3;i++)
		{
			int p[3]={1,1,1};
			do
			{
				p[i]*=a[i];
				p[i]%=m[i];
				b[i]++;
			}while(p[i]!=1);
		}
		cout<<lcm(lcm(b[0],b[1]),b[2])<<endl;
	}
}