import Foundation

var num = readLine()!.split(separator: " ").map{Int($0)!}
var p = 0
if(num[0] >= num[3]){
  p += num[3]
  num[3] = 0
}else{
  p += num[0]
  num[3] -= num[0] + num[1]
}
if(num[3] > 0){
  p -= num[3]
}
print(p)