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);
  if(k-a<=0) writeln(k);
  else if(k-a-b<=0) writeln(a);
  else writeln(a-(k-a-b));
}
