#include <iostream>
using namespace std;

int lcm(int a,int b) {
	if (a % b==0) return b;
	else return lcm(b, a % b);
	}
long long gcd(long long a,long long b) {
	int lc;
	lc=lcm(a,b);
    return a*b/lc;
}

int main() {
int a[3],b[3],c[3],i,j,k;
while(true) {
	for (i=0;i<3;i++) cin >> a[i] >> b[i];
	if (a[0]==0) break;
	for (i=0;i<3;i++) {
		c[i]=0; j=1;
		do { j=a[i]*j % b[i]; c[i]++;} while (j!=1);
	}
    cout << gcd(gcd(c[0],c[1]),gcd(c[1],c[2])) << endl;
}
return 0;
}