unittest
{
	assert( [ "2 1 1 3" ].parse.expand.solve == 2 );
	assert( [ "1 2 3 4" ].parse.expand.solve == 0 );
	assert( [ "2000000000 0 0 2000000000" ].parse.expand.solve == 2000000000 );
}



import std.conv;
import std.range;
import std.stdio;
import std.typecons;

void main()
{
	stdin.byLineCopy.parse.expand.solve.writeln;
}

auto parse( Range )( Range input )
if( isInputRange!Range && is( ElementType!Range == string ) )
{
	auto abck = input.front.split.to!( long[] );
	return tuple( abck[ 0 ], abck[ 1 ], abck[ 2 ], abck[ 3 ] );
}

auto solve( long a, long b, long c, long k )
{
	if(      k <= a     ) return k;
	else if( k <= a + b ) return a;
	else                  return a - ( k - a - b );
}
