loop do
  n, m = gets.split.map(&:to_i)
  break if n.zero? and m.zero?
  xs = Array.new n
  c = 0
  i = 0
  while c < n
    j = 0
    while j < m
      i = (i + 1) % n
      i = (i + 1) % n while xs[i]
      j += 1
    end
    xs[i] = c
    c += 1
  end
  x = xs.find_index n - 1
  p x.zero? ? n : x
end