let a, b, c, k = Scanf.scanf "%d %d %d %d\n" @@ fun a b c d -> a, b, c, d
let ans = if k <= a then k else if k <= a + b then a else if k <= a + b + c then a - (k - a) else a + c
let () = print_int ans