 var numArr = readLine()!.split(separator: " ").map{Int($0)!}

    var i: Int = 0
    var r : Int = 0
    var t: Int = 0

    while numArr[3] != t {

      while numArr[i] != 0 {
        
        if i == 0 && numArr[i] != 0 {

          r += 1
          numArr[i] -= 1
          t += 1

          if t == numArr[3] {
            break

          }

        }
        else if i == 1 && numArr[i] != 0 {
          
          r += 0
          numArr[i] -= 1
          t += 1
          
          if t == numArr[3] {
            break
            
          }
        }  else if i == 2 && numArr[i] != 0 {
          
          r += -1
          numArr[i] -= 1
          t += 1
          
          if t == numArr[3] {
            break
            
          }
        }
      }
      
       if t == numArr[3] {
        print(r)
        break
      }
      else {
        i += 1
      }
    }