readlines.map(&:chomp).each{|line|
  break if line == "0 0"
  n, m = line.split.map(&:to_i)
  arr = Array.new(n){|i| i}
  arr[0] = n
  p = m
  loop do
    break if arr.size == 1
    while p < arr.size
      arr[p] = -1
      p += m
    end
    p -= arr.size
    arr -= [-1]
  end
  puts arr[0]
}