#include <iostream>
#include <algorithm>
using namespace std;
#define LL long long

LL GCD(LL x ,LL y){//最大公約数
	LL r;
	while(r=x%y){
		x=y;
		y=r;
	}
	return y;
}

LL LCM(LL x,LL y){
	return (x*y/GCD(x,y));
}

int cnter(int a, int m){
	int t=0;
	int x=1;
	while(true){
		t++;
		x=a*x%m;
		if(x==1)break;
	}
	return t;
}

int main(){
	int a1,m1,a2,m2,a3,m3;
	while(cin>>a1>>m1>>a2>>m2>>a3>>m3){
		if(a1==0)break;
		int t1=cnter(a1,m1),t2=cnter(a2,m2),t3=cnter(a3,m3);
		cout<<LCM(LCM(t1,t2),t3)<<endl;
	}
	
	return 0;
}
