a,b,c,k=io.read("*n","*n","*n","*n")
if a>=k then
    print(k)
elseif a<k then
    if a+b>=k then
        print(a)
    elseif a+b<k then
        if a+b+c==k then
            print(a-c)
        elseif a+b+c>k then
            print(a-(k-a-b))
        end
    end
end
