program ABC167_B
  integer(8)::A,B,C,K
  read(*,*)A,B,C,K
  if(K<=A) then
     write(*,*)K
  else if(A<K .and. K<=A+B) then
     write(*,*)A
  else
     write(*,*)A-(K-A-B)
  end if
end program ABC167_B