#include <iostream>
#include <sstream>
#include <iomanip>
#include <algorithm>
#include <cmath>
#include <string>
#include <vector>
#include <list>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <bitset>
#include <numeric>
#include <climits>
#include <cfloat>
using namespace std;

long long gcd(long long a, long long b){ // Ååöñ
    if(a == 0 || b == 0)
        return 0;
    if(b > a)
        swap(a, b);
    long long tmp;
    while((tmp = a % b) != 0){
        a = b;
        b = tmp;
    }
    return b;
}

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

int main()
{
    for(;;){
        vector<int> a(3), m(3);
        for(int i=0; i<3; ++i)
            cin >> a[i] >> m[i];
        if(a[0] == 0)
            return 0;

        long long ret = 1;
        for(int i=0; i<3; ++i){
            int x = 1;
            int tmp = 0;
            do{
                x *= a[i];
                x %= m[i];
                ++ tmp;
            }while(x != 1);
            ret = lcm(ret, tmp);
        }
        cout << ret << endl;
    }
}