// ABC167 - B
function main(input) {
  var inputs = input.split('\n');
  var line1 = inputs[0].split(' ');
  var a = parseInt(line1[0], 10);
  var b = parseInt(line1[1], 10);
  var c = parseInt(line1[2], 10);
  var k = parseInt(line1[3], 10);

  var maxVal = 0;
  maxVal += k >= a ? a : k;
  if (k - a - b > 0) maxVal -= k - a - b;

  console.log(maxVal);
}
main(require('fs').readFileSync('/dev/stdin', 'utf8'));
