#!/bin/bash
read num1
read num0
read num
read card
max=0
if [ $num1 -gt $card ]; then
	echo $(($card*1))
else
	if [ $num0 -gt $(($card - $num1)) ]; then
    	echo $(($num1*1))
    else
    	echo $(($num1 * 1  + ($card - $num1 - $num0) * -1))
        fi
fi