func readInts() -> [Int] {
    return readLine()!.split(separator: " ").map { Int($0)! }
}

func main() {
    let line = readInts()
    let a = line[0]
    let b = line[1]
    let k = line[3]
    
    if k <= a {
        print(k)
    } else if k <= a + b {
        print(a)
    } else {
        print(a + a + b - k)
    }
}

main()
