#include<iostream>
#include<math.h>
#include<vector>
#include<algorithm>
#include<cstdio>
using namespace std;

long long a[3], m[3], k[3];
long long gcd(int a, int b) {
    if (b == 0) { return a; }
    return gcd(b, a%b);
}
int main() {
    while (true) {
        cin >> a[0] >> m[0] >> a[1] >> m[1] >> a[2] >> m[2];
        if (a[0] == 0 && m[0] == 0 && a[1] == 0 && m[1] == 0 && a[2] == 0 && m[2] == 0) { break; }
        for (int i = 0; i < 3; i++) {
            long long cnt = 0, x = 1;
            while (true) {
                if (x == 1 && cnt >= 1) { break; }
                x *= a[i]; x %= m[i]; cnt++;
            }
            k[i] = cnt;
        }
        int LCM = k[0] * k[1] / gcd(k[0], k[1]);
        cout << LCM*k[2] / gcd(LCM, k[2]) << endl;
    }
    return 0;
}
