function Main(input) {
    tmp = input.split(" ");
    a = parseInt(tmp[0], 10);
    b = parseInt(tmp[1], 10);
    c = parseInt(tmp[2], 10);
    k = parseInt(tmp[3], 10);
  
    var result = 0;
    var count = 0;
    var nokori = k;


  while(nokori > count){
    if (nokori >= a) {
      result += (a*1);
      count += a;
      nokori -= a;
    } else{
      result += (nokori*1);
      count += nokori;
      nokori -= nokori;
    }

    if (nokori >= b) {
      result += (0);
      count += b;
      nokori -= b;
    } else{
      result += (0);
      count += nokori;
      nokori -= nokori;
    }
    if (nokori >= c) {
      result += (c * -1);
      count += c;
      nokori -= c;
    } else{
      result += (nokori * -1);
      count += nokori;
      nokori -= nokori;
    }
  }
  

    console.log(result);
    
}
Main(require("fs").readFileSync("/dev/stdin", "utf8"));