def num(a,m)
	t = a
	n = 1
	while t != 1
		t = t*a%m
		n+=1
	end
	n
end

while gets
	a1,m1,a2,m2,a3,m3 = $_.split.map(&:to_i)
	break if [a1,m1,a2,m2,a3,m3] == [0,0,0,0,0,0]
	x,y,z = [num(a1,m1),num(a2,m2),num(a3,m3)]
	p x.lcm(y).lcm(z)
end