def ascan; gets.split.map(&:to_i); end
def scan; gets.to_i; end

while cin = gets
    n,k=cin.split.map(&:to_i)

    if k == 0
        p 0
    elsif n == 1
        if k == 1
            p 0
        else
            p 1
        end
    elsif n.odd? && (n+1)/2 == k
        p n-1
    elsif n<k
        p 0
    else
        p n-2
    end
end