#include<iostream>
#include<algorithm>
#define ll long long int
using namespace std;
ll rep(ll s,ll t){
	return (s/__gcd(s,t))*t;
}
int main(){
	ll a1,a2,a3,x,y,z,m1,m2,m3,now,a,b,c;
	while(cin>>a1>>m1>>a2>>m2>>a3>>m3,a1||m1||a2||m2||a3||m3){
		x=a1%m1;a=-1;b=-1;c=-1;
		y=a2%m2;now=1;
		z=a3%m3;
		while(a==-1||b==-1||c==-1){
			now++;
			x=(a1*x)%m1;
			if(x==1)
			a=now;
			y=(a2*y)%m2;
			if(y==1)
			b=now;
			z=(a3*z)%m3;
			if(z==1)
			c=now;
		}
		cout<<rep(rep(a,b),c)<<endl;
	}
	return 0;
}