import std;

long calc(long a, long b, long c, long k) {
    long ans = 0;
    long x = min(a, k);
    ans += x;
    k -= x;
    k -= min(b, k);
    long y = min(c, k);
    ans += -1 * y;
    return ans;
}

void main() {
    long a, b, c, k; scan(a, b, c, k);
    writeln(calc(a, b, c, k));
}

void scan(T...)(ref T a) {
    string[] ss = readln.split;
    foreach (i, t; T) a[i] = ss[i].to!t;
}
T read(T=string)() { return readln.chomp.to!T; }
T[] reads(T)() { return readln.split.to!(T[]); }
alias readints = reads!int;
