#include <iostream>
using namespace std;
int gcd2(long long a,long long b){
	int w;
	while(b!=0){
		w=a%b;
		a=b;
		b=w;
	}
	return a;
}

int lcm(long long a,long long b){
	return a*b/(long long)gcd2(a,b);
}
int count(long long a,long long m){
	long long x=a%m,i;
	for(i=1;x!=1;++i){
		x=a*x%m;
	}
	return i;
}
int main(){
	int a1,a2,a3,m1,m2,m3;
	while(cin>>a1>>m1>>a2>>m2>>a3>>m3){
		if(a1==0&&a2==0&&a3==0&&m1==0&&m2==0&&m3==0)break;
		int xc,yc,zc;
		xc=count(a1,m1);
		yc=count(a2,m2);
		zc=count(a3,m3);
		cout<<lcm(lcm(xc,yc),zc)<<"\n";
	}
	return 0;
}