#include <iostream>
#include <sstream>
#include <vector>
#include <list>
#include <string>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <algorithm>
#include <numeric>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <climits>
using namespace std;

long long gcd( long long x, long long y ) {
    if ( x < y ) swap( x, y );
    return y == 0 ? x : gcd( y, x % y );
}

long long lcm( long long x, long long y ) {
    return x * y / gcd( x, y );
}

long long calc( long long a, long long m ) {
    long long t = 0;
    long long x = 1;
    do {
        x = ( a * x ) % m;
        t ++;
    } while ( x != 1 );
    return t;
}

int main() {
    long long a[3];
    long long m[3];
    long long t[3];
    while ( true ) {
        for ( int i = 0; i < 3; ++ i ) {
            cin >> a[i] >> m[i];
        }
        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 ) {
            t[i] = calc( a[i], m[i] );
        }
        cout << lcm( t[0], lcm( t[1], t[2] ) ) << endl;
    }
    return 0;
}