if let s = readLine() {
  let ss = s.split(separator: " ")
  let a = Int(ss[0])!
  let b = Int(ss[1])!
  let c = Int(ss[2])!
  let k = Int(ss[3])!
  switch k {
  case 1...a:      print(k)
  case a...a+b:    print(a)
  case a+b...a+b+c:print(a-(k-a-b))
  default: break
  }
}