import std.stdio;
import std.algorithm;
import std.string;
import std.range;
import std.array;
import std.conv;
import std.complex;
import std.math;
import std.ascii;
import std.bigint;
import std.container;


int nReturn(int a, int m){
	int ret = 1;
	int p = a%m;
	while(p > 1){
		p = (p*a)%m;
		++ret;
	}
	return ret;
}
long gcd(long a, long b) {
	if(a < b)
		swap(a,b);
	if(a%b == 0)
		return b;
	return gcd(b, a%b);
}
long lcm(long a, long b) {
	long g = gcd(a,b);
	return (a/g)*(b/g)*g;
}

int sum(int[] a) {
	int s;
	foreach(b; a)
		s += b;
	return s;
}

void main() {
	auto s = array(map!(a=>to!int(a))(readln().strip().split()));
	while(sum(s) > 0) {
		long n1 = nReturn(s[0], s[1]);
		long n2 = nReturn(s[2], s[3]);
		long n3 = nReturn(s[4], s[5]);
		writeln(lcm(lcm(n1, n2),n3));

		s = array(map!(a=>to!int(a))(readln().strip().split()));
	}
}