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

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