fun main(args: Array<String>) {
	val input1=readLine()!!.split(" ")
	var A:Long=input1[0].toLong()
	var B:Long=input1[1].toLong()
	var C:Long=input1[2].toLong()
	var K:Long=input1[3].toLong()
	if(K<=A){
		print(K)
	}
	else{
		if(A+B>=K){
			print(A)
		}
		else{
			print(A-(K-A-B))
		}
	}
	
	
}