def calc(a, m)
  i = 1
  1.step do |co|
    i = a * i % m
    return co if i == 1
  end
end

until (a = $<.gets.split.map(&:to_i)) == [0] * 6
  cx, cy, cz = a.each_slice(2).map {|a, m| calc(a, m)}
  puts cx.lcm(cy).lcm(cz)
end
