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

  var maxVal = 0;
  maxVal += a;
  maxVal -= k - a - b

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