readlines.each do |l|
  n,m = l.split.map(&:to_i)
  break if n==0 && m==0
  i = n
  p = (0..(n-1)).to_a
  while p.size>1
    p.delete_at(i=((i+m) % p.size))
    i = (i-1) % p.size
  end
  puts p.first
end