#include <iostream>
#include <cstdio>
#include <vector>
#include <list>
#include <algorithm>
#include <cmath>
#include <stack>
#include <map>
#include <numeric>
#include <sstream>
#include <queue>
#include <iomanip>
using namespace std;

#define REP(i,n) for(int (i)=0; (i)<(n); (i)++)
#define FOR(i,a,b) for(int (i)=(a); (i)<(b); (i)++)
#define PUSH(n,v) for(int i=0; i<(n); i++) {int j; cin >> j; v.push_back(j);}
#define ALL(v) v.begin(), v.end()
#define print(s) cout << (s) << endl

long gcd(int m, int n) {
    if (m % n == 0) return n;
    else return gcd(n, m % n);
}

long lcm(int m, int n) {
    long g = gcd(m,n);
    return (long)m / g * n;
}

int getMinimimTimes(int a, int b) {
    // find least n at which a^n % b == 1
    int k = a;
    int n = 1;
    while(1) {
        if (a % b == 1) break;
        a *= k;
        a %= b;
        n++;
    }
    return n;
}

int main() {
    int a,b,c,d,e,f;
    while (cin>>a>>b>>c>>d>>e>>f,a+b+c+d+e+f) {
        int p, q, r;
        p = getMinimimTimes(a,b);
        q = getMinimimTimes(c,d);
        r = getMinimimTimes(e,f);
        long s;
        s = lcm(p,q);
        s = lcm(s, r);
        cout << s << endl;
    }
    return 0;
}