console.log(function(args){
  let [A,B,C,K] = args.trim().split(' ').map(BigInt);
  if ( K <= A ) return K.toString();
  if ( K <= A + B ) return A.toString();
  return ( A - ( K - A - B ) ).toString();
}(require('fs').readFileSync('/dev/stdin', 'utf8')));
