'use strict';
(function(input){
  
  const compose=Symbol();
  
  Function.prototype[compose]=function(outerFunc){
    const that=this;
    return function(I){
        return outerFunc(that(I));
      }
  }
  
  function func0(Input){
    return input.
    split(/\s+/).
    map(x=>x-0);
  }
  
  function func1(ABCK){
    const [A,B,C,K]=ABCK;
    const BCKS=[B,C,Math.max(K-A,0),Math.min(A,K)];
    return BCKS;
  }
  
  function func2(BCKS){
    const [B,C,K,S]=BCKS;
    const CKS=[C,Math.max(K-B,0),S];
    return CKS;
  }
  
  function func3(CKS){
    const [C,K,S]=CKS;
    const result=S-K;
    return result;
  }
  
  console.log(
    func0[compose](
      func1
    )[compose](
      func2
    )[compose](
      func3
    )(input);
  );
})(require('fs').readFileSync('/dev/stdin','utf8'));