#include <stdio.h>
#include <cmath>
#include <vector>
#include <iostream>
#include <sstream>
#include <iomanip>
#include <string>
using namespace std;

long gcd(long a,long b)
{
	int l=0,s=0,r=0;
	if (a>b){
		l=a;s=b;
	} else {
		l=b;s=a;
	}
	while(l%s != 0){
		r=l%s;
		l=s;
		s=r;
	}
	return s;
}

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

int main(void) {
	vector<long long> r;
	while (true)
	{
		long a[3]={0},m[3]={0},p[3]={1,1,1},re[3]={0};
		scanf("%ld %ld %ld %ld %ld %ld",&a[0],&m[0],&a[1],&m[1],&a[2],&m[2]);
		if (a[0]==0 && m[0]==0){
			break;
		}
		for (int i=0; i<3; i++)
		{
			int cnt=0;
			while (true)
			{
				p[i] = (p[i]*a[i])%m[i];
				cnt++;
				if (p[i]==1){break;}
			}
			re[i]=cnt;
		}
		long long result=0;
		result = lcm(lcm(re[0],re[1]),re[2]);
		r.push_back(result);
	}
	for(int i=0; i<r.size(); i++)
	{
		cout << r[i] << endl;
	}
	return 0;
}