#include <iostream>
#include <string>
#include <vector>
using namespace std;

long long gcd(long long a, long long b) {
    return (a == 0 ? b : gcd(b % a, a));
}

long long lcm(long long a, long long b) {
    return a / gcd(a, b) * b;
}

int main() {
    long long a[3], m[3], p[3], x;
    while (cin >> a[0] >> m[0] >> a[1] >> m[1] >> a[2] >> m[2], a[0]) {
        for (int i=0; i<3; ++i) {
            x = 1;
            for (int j=1; ; ++j) {
                x = (a[i] * x) % m[i];
                if (x == 1) {
                    p[i] = j;
                    break;
                }
            }
        }
        cout << lcm(p[0], lcm(p[1], p[2])) << endl;
    }
    return 0;
}