while gets
    n, m = $_.split.map &:to_i
    break if n == 0
    a = (1..n).to_a
    until a.size == 1
        i = (m-1) % a.size
        a = a[i+1..-1] + (i == 0 ? [] : a[0..i-1])
    end
    p a[0]
end