let line = readLine()!.split(separator: " ").map { Int($0)! }
var a = line[0], b = line[1], c = line[2]
let k = line[3]

var ans = 0
for _ in 0..<k {
  if a - 1 >= 0 {
    a -= 1
    ans += 1
  } else if b - 1 >= 0 {
    b -= 1
    ans += 0
  } else if c - 1 >= 0 {
    c -= 1
    ans -= 1
  }
}
print(ans)