#include <math.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef unsigned long long int ull;
typedef long long int ll;
typedef long double ld;

int main(void) {
  int scan; // scanf result
  ull answer; // to output 
  ull a; // to input 
  ull b; // to input 
  ull c; // to input 
  ull k; // to input 

  scan = scanf("%llu", &a);
  scan = scanf("%llu", &b);
  scan = scanf("%llu", &c);
  scan = scanf("%llu", &k);
  
  if (a <= k) {
    if (a + b >= k) {
      answer = a;
    } else {
      answer = a - (k - (a + b));
    }
  } else {
    answer = k;
  }

  printf("%llu", answer);
  return (0);
}
