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
var card = k - a
if card <= 0 {
  ans += k
} else if card > 0 {
  ans += a
  card -= b
  if card > 0 {
    ans -= card
  }
}
print(ans)