import std;

auto input()
{
    return readln().chomp();
}

void main()
{
    // long[] ary = to!(long[])(split(readln()));
    auto ary = readln().split().to!(long[])();
    // writeln(join(to!(string[])(ary), " "));
    long a = ary[0];
    long b = ary[1];
    long c = ary[2];
    long k = ary[3];
    if (a >= k)
    {
        writeln(k);
    }
    else if (a + b >= k)
    {

        writeln(a);
    }
    else
    {
        writeln(a - (k - a - b));
    }
}
