"use strict";

const main = arg => {
    const inputList = arg.trim().split("\n");
    // 回答を記述
    const A = parseInt(inputList[0].split(" ")[0]);
    const B = parseInt(inputList[0].split(" ")[1]);
    const C = parseInt(inputList[0].split(" ")[2]);
    const K = parseInt(inputList[0].split(" ")[3]);
  
    if (K <= (A + B)) {
      console.log(A)
    } else {
      // A + B <= K <= A + B + C
      const minus = K - (A + B);
      console.log(A - minus)
    } 
  
  }

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