#include <iostream>
#include <algorithm>
using namespace std;
#define lcm(a,b) (a/__gcd(a,b)*b)
int get(int a,int m){
	int n = 1;
	for(int i = 1 ; i <= m ; i++){	
		n = (n*a) % m;
		if(n==1) return i;
	}
}
int main(){
	int a,b,c,d,e,f;
	while(cin >> a >> b >> c >> d >> e >> f && f){
		cout << lcm(get(e,f),lcm(get(a,b),get(c,d))) << endl;
	}
}