#include <iostream>
#include <algorithm>
using namespace std;
#define rep(i,n) for(int i=0;i<n;i++)
typedef long long ll;

ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}

ll lcm(ll a,ll b){
	return a/gcd(a,b)*b;
}
int main(){
	long long a[3]={0},m[3];
	while(cin >> a[0] >> m[0] , a[0]){
		rep(i,2)cin >> a[1+i] >> m[1+i];
		
		int d[3] = {0};
		
		rep(i,3){
			int pos = 1;
			do{
				pos = (a[i] * pos) % m[i];
				d[i]++;
			}while(pos != 1);
		}
		cout << (long long)lcm(lcm(d[0],d[1]),d[2]) << endl;
	}
}
	