import std.stdio, std.conv, std.string, std.algorithm,
       std.math, std.array, std.container, std.typecons;

void main() {
  long a, b, c, k;
  readf("%d %d %d %d\n", &a, &b, &c, &k);
  k = k-a-b;
  if(k<=0) writeln(a);
  else writeln(a-k);
}
