#include <cstdio>
#include <cstring>
#include <algorithm>
#include <string>
#include <cmath>
#include <stack>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <list>
#include <iostream>
#include <climits>
#include <cfloat>

typedef long long ll;

using namespace std;

int gcd(int a, int b)
{
	if (a < b)
		return gcd(b, a);
	if (b == 0)
		return a;
	return gcd(b, a % b);
}
int main()
{
	int a[3], m[3];
	while (scanf("%d %d %d %d %d %d", a, m, a+1, m+1, a+2, m+2), a[0])
	{
		int x[3];
		int c[3];
		for (int i = 0; i < 3; ++i)
		{
			x[i] = 1;
			for (int j = 1; ; ++j)
			{
				x[i] = a[i] * x[i] % m[i];
				if (x[i] == 1)
				{
					c[i] = j;
					break;
				}
			}
		}
		ll t = (ll)c[0]*c[1] / gcd(c[0], c[1]);
		printf("%lld\n", t*c[2] / gcd(t, c[2]));
	}

	return 0;
}