import std.stdio;
import std.algorithm;
import std.string;
import std.functional;
import std.array;
import std.conv;
import std.math;
import std.typecons;
import std.regex;
import std.range;

long gd(long x,long y){
    return y ? gd(y,x % y) : x;
}

void main(){

    while(true){
	long x,y,z;
	long x1,y1,z1;
	long l,m,n;
	auto s = readln().split().to!(long[]);
	if(s[0] == 0 && s[1] == 0 && s[2] == 0 && s[3] == 0 && s[4] == 0 && s[5] == 0 ) break;
	x1 = s[0];
	l = s[1];
	y1 = s[2];
	m = s[3];
	z1 = s[4];
	n = s[5];
	x = x1 % l;
	y = y1 % m;
	z = z1 % n;
	long ans;
	long a,b,c;
	a = 1;
	b = 1;
	c = 1;
	while(x != 1){
	    x = (x1 * x) % l;
	    a++;
	}
	while(y != 1){
	    y = (y1 * y) % m;
	    b++;
	}
	while(z != 1){
	    z = (z1 * z) % n;
	    c++;
	}
	ans =  (a * b) / gd(a,b);
	ans = (ans * c) / gd(ans,c);
	writeln(ans);
    }
}