import java.util.*

fun main(args: Array<String>) {
    val scan = Scanner(System.`in`)
        var (a,b,c,k) = scan.nextLine().split(" ").map{ it.trim().toInt() }.toTypedArray()
        var ans : Long= 0
        if(k<=a)
        {
            ans+=k
            k-=a
        }
         else if(k<=a+b)
        {
             ans+=a
             k-=a
         }
        else if(k<=a+b+c)
        {
            ans+=a
            var z = k-(a+b)
            ans-=z
        }
    println(ans)
}
