unittest{
	assert(solve([2L, 1L, 1L], 3L) == 2L);
	assert(solve([1L, 2L, 3L], 4L) == 0L);
	assert(solve([2000000000, 0, 0], 2000000000) == 2000000000);
}

long solve(long[3] v, long k){
	long[3] w = [1, 0, -1];
	long ans;

	foreach(i, elm; v){
		if(elm <= k){
			ans += elm*w[i];
			k -= elm;
		}else{
			ans += k*w[i];
			break;
		}
	}
	return ans;
}

void main(){
	long[3] v;
	long k;
	readf("%d %d %d %d\n", &v[0], &v[1], &v[2], &k);

	writeln( solve(v, k) );
}
import std.stdio, std.conv, std.algorithm, std.numeric, std.string, std.math, std.format;

// 1要素のみの入力
T _scan(T= int)(){
	return to!(T)( readln().chomp() );
}

// 1行に同一型の複数入力
T[] _scanln(T = int)(){
	T[] ln;
	foreach(string elm; readln().chomp().split()){
		ln ~= elm.to!T();
	}
	return ln;
}
