#include <iostream>
#include <algorithm>
using namespace std;

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

	for(int i = 0; i < 3; i++)
		cin >> a[i] >> m[i];

	while(a[0] + a[1] + a[2] + m[0] + m[1] + m[2]) {
		int b[3];
		int hoge;

		for(int i = 0; i < 3; i++) {
			int tmp = 0;
			for(int j = 1; !tmp || j != 1; j = j * a[i] % m[i]) tmp++;
			b[i] = tmp;
		}

		int p, q, r;
		p = max(b[0], b[1]); q = min(b[0], b[1]);
		while(p % q) {
			r = p % q;
			p = q;
			q = r;
		}

		hoge = q = b[0] * (b[1] / q);

		p = b[2];
		if(q > p) swap(p, q);
		while(p % q) {
			r = p % q;
			p = q;
			q = r;
		}

		cout << (long)hoge * (b[2] / q) << endl;

		for(int i = 0; i < 3; i++)
			cin >> a[i] >> m[i];
	}
}