#include<iostream>
#include<string.h>
#include<vector>
#include<list>
#include<stdio.h>
#include<math.h>
#include<iomanip>
#include<map>
#include<stack>
#include<queue>
#include<algorithm>
#define range(i,a,b) for(int i = (a); i < (b); i++)
#define rep(i,b) range(i,0,b)
#define debug(x) cout << "debug " << x << endl;
using namespace std;

int ggd(int x, int y){
    while(x != y){
        if(x < y){
            int temp = x;
            x = y;
            y = temp;
        }
        int num = y;
        y = x - y;
        x = num;
    }
    return x;
}

int lcm(int x, int y){
    return x * y / ggd(x, y);
}

int main(){
    int a1, a2, a3, m1, m2, m3;
    int x, y ,z;
    x = y = z = 1;
    long long i, j, k;
    while(cin >> a1 >> m1 >> a2 >> m2 >> a3 >> m3, a1||a2||a3||m1||m2||m3){
        long long count = 0;
        bool xc, yc, zc;
        xc = yc = zc = true;
        while(true){
            count++;
            if(xc){
                x = a1 * x % m1;
                if(x == 1){
                    i = count;
                    xc = false;
                }
            }
            if(yc){
                y = a2 * y % m2;
                if(y == 1){
                    j = count;
                    yc = false;
                }
            }
            if(zc){
                z = a3 * z % m3;
                if(z == 1){
                    k = count;
                    zc = false;
                }
            }
            if(!(xc||yc||zc)) break;
        }
        cout << lcm(lcm(i,j),k) << endl;
    }
}