function main(input) {
  const args = input.split('\n');
  const [A, B, C, K] = args[0].split(' ').map(n => parseInt(n, 10));

  console.log(
    K <= A + B
      ? Math.min(K, A)
      : A - (K - (A + B))
  );
}

main(require('fs').readFileSync('/dev/stdin', 'utf8'));
