#include <iostream>
using namespace std;

int gcd(int x,int y){
	if(x < y) swap(x,y);

	while(y != 0){
		x = x % y;
		swap(x,y);
	}
	return x;
}

int lcm(int x,int y){
	return x/gcd(x,y)*y;
}

int solve(int a[],int m[]){
	int x[3]={1,1,1};
	int count[3]={0,0,0};

	for(int i=0;i<3;i++){
		do{
			x[i] = (a[i] * x[i]) % m[i];
			count[i]++;
		}while(x[i]!=1);
	}
	return lcm(lcm(count[0],count[1]),count[2]);
}

int main(void){
	int a[3],m[3];

	while(1){
		for(int i=0;i<3;i++){
			cin>>a[i]>>m[i];
			if(!a[i]) return 0;
		}
		
		cout<<solve(a,m)<<endl;
	}

  return 0;
}