#include <cstdio>
#include <vector>
#include <algorithm>
#include <cstring>
#include <string>
#include <cctype>
#include <sstream>
#include <cmath>
#include <climits>
#include <set>
#include <iostream>
#include <map>
#include <functional>
#include <cstdlib>
#include <numeric>
#include <queue>
#include <complex>
#include <sstream>
#include <stack>

using namespace std;

#define reep(i,f,n) for(int i=f; i<int(n); ++i)
#define rep(i,n) reep(i,0,n)

typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> pii;
typedef complex<double> Point;

int gcd(int a, int b)
{
	if(a<b)
		swap(a, b);
	
	int c;
	while(b){
		c = b;
		b = a % b;
		a = c;
	}
	
	return a;
}

int main()
{
	int cnt[3];
	for(;;){
		rep(i, 3){
			int a, m;
			scanf("%d%d", &a, &m);
			if(m == 0)
				return 0;
			
			int pos = 1;
			for(cnt[i]=1;; ++cnt[i]){
				pos = a * pos % m;
				if(pos == 1)
					break;
			}
		}
		
		int g = cnt[0]/gcd(cnt[0], cnt[1])*cnt[1];
		printf("%d\n", g/gcd(g, cnt[2])*cnt[2]);
	}
	
	return 0;
}