#!/bin/sh
# 1 2 7 5 = 1 - 2 (2= 5-1-2)
# 0 1 6 1 = 0
# 0 0 6 1 = -1

read a b c k

if [ $a -eq 0 -a $b -eq 0 ]; then
        echo $((0 - $k))
        exit
fi

if [ $(($a + $b)) -ge $k ]; then
        echo $a
else
        minus=$(($k -$a -$b))
        echo $(($a  - $minus))
fi