#include <iostream>
#include <vector>  

using namespace std;

long long gcd(int a, int b) {
    if (b > a) swap(a, b);
    int r = a % b;
    if ( r == 0 ) return b;
    return gcd(b, r);
}

long long lcm(int a, int b) {
    int x = a * b;
    int g = gcd(a, b);
    return x/g;
}

int count(int a, int m) {
    int n = ( a * 1 ) % m;
    int cnt = 1;
    while(1) {
        if (n == 1) break;
        cnt++;
        n = ( a * n ) % m;
    }
    return cnt;
}

int main() {
    int a, b, c;
    int l, m, n;
    int x = 1;
    int y = 1;
    int z = 1;

    while(cin>>a>>l>>b>>m>>c>>n, a||b||c||l||m||n) {
        int nx = count(a, l);
        int ny = count(b, m);
        int nz = count(c, n);
        int ans = 0;
        int tmp = lcm(nx, ny);
        ans = lcm(tmp , nz);
        cout<<ans<<endl;
    }

    return 0;
}