import std.stdio;
import std.algorithm;
import std.math;
import std.conv;
import std.string;

T readNum(T)(){
  return readStr.to!T;
}

T[] readNums(T)(){
  return readStr.split.to!(T[]);
}

string readStr(){
  return readln.chomp;
}

void main(){
  auto abck = readNums!int;
  int qty, ret;

    if(abck[0] > 0 && qty != abck[3]){
      ret += min(abck[3] - qty, abck[0]);
      qty += min(abck[3] - qty, abck[0]);
    }

    if(abck[1] > 0 && qty != abck[3]){
      qty += min(abck[3] - qty, abck[1]);
    }

    if(abck[2] > 0 && qty != abck[3]){
      ret -= min(abck[3] - qty, abck[2]);
      qty += min(abck[3] - qty, abck[2]);
    }
  writeln(ret);
}
