def f a, m
  x, n = 1, 1
  until (x = (a * x) % m) == 1
    n += 1
  end
  return n
end

loop do
  input = gets.split.map(&:to_i)
  if input == [0] * 6
    break
  end
  a1, m1, a2, m2, a3, m3 = *input
  nx = f a1, m1
  ny = f a2, m2
  nz = f a3, m3
  n = [nx, ny, nz].inject{|x, y|x.lcm y}
  puts n
end