object Main {
    def main(args: Array[String]) : Unit = {
        val sc = new java.util.Scanner(System.in)
        val a, b, c, d  = sc.nextInt
        println(calc(a, b, c, d))
    }
    def calc(a: Int, b: Int, c: Int, d: Int) : Int = {
        var ans = max(b - c, 0)
        if (ans > 0 && b - d > 0) {
            ans -= b - d
        }
        ans
    }
    def max(a: Int, b: Int) : Int = { if (a > b) a else b }
}
