open System

let a, b, c, k =
    let line = Console.ReadLine().Split(" ") |> Array.map int
    line.[0], line.[1], line.[2], line.[3]

let max =
    let mutable max = 0
    if a + b >= k then max <- min k a else max <- a - (k - (a + b))
    max

max |> Console.WriteLine
