let rec kn a k = 
        if a <= 0 || k <= 0 then 
                0
        else if a < k then
                a
        else
                k


let () = Scanf.scanf "%d %d %d %d" (
fun a b c k -> 
        let an = kn a k in
        let bn = kn b (k - an) in
        let cn = kn c (k - an - bn) in
        an - cn
) |> Printf.printf "%d\n"