#include <iostream>
#include <algorithm>
using namespace std;

int gcd(int a, int b){
    if( a < b ) swap(a, b);
    int c;
    while( a%b ){
        c = a%b;
        a = b;
        b = c;
    }
    return b;
}

int main(){
    int a[3], m[3];
    while( cin>>a[0]>>m[0]>>a[1]>>m[1]>>a[2]>>m[2],
           a[0]|m[0]|a[1]|m[1]|a[2]|m[2] ){
        int p[3], c[3];
        for(int i = 0; i < 3; i++){
            c[i] = 1;
            p[i] = a[i]%m[i];
            while( p[i]-1 ){
                p[i] = a[i]*p[i]%m[i];
                c[i]++;
            }
        }
        int LCM = c[0]*c[1]/gcd(c[0], c[1]);
        cout << LCM*c[2]/gcd(LCM, c[2]) << endl;
    }
    return 0;
}