#include <iostream>
#include <sstream>
#include <string>
#include <algorithm>
#include <vector>
#include <iomanip>
#include <queue>
#include <set>
#include <map>
#include <cstdlib>
#include <cstdio>
#include <cmath>
#include <bitset>
using namespace std;
  
const double EPS = 1e-9;

int check(int &a, int &m){
	int x = 1;
	int cnt = 0;
	do{
		cnt++;
		x = (a*x)%m;
	}while(x != 1);
	return cnt;
}

int getGCD(int m, int n){
	if(n == 0){
		return m;
	}
	return getGCD(n, m%n);
}
  
int main(){
	int x, y, z;
	int a1, m1, a2, m2, a3, m3;
	while(cin >> a1 >> m1 >> a2 >> m2 >> a3 >> m3, a1){
		long long cntx = 0, cnty = 0, cntz = 0;
		cntx = check(a1, m1);
		cnty = check(a2, m2);
		cntz = check(a3, m3);

		if(cntx < cnty){
			swap(cntx, cnty);
		}
		long long LCMxy, LCM;
		LCMxy = cntx*cnty/getGCD(cntx, cnty);
		if(LCMxy < cntz){
			swap(LCMxy, cntz);
		}
		LCM = LCMxy*cntz/getGCD(LCMxy, cntz);
		cout << LCM << endl;
	}
  
    return 0;
}