import java.util.*;

public class Main {
  public static void main(String... args) {
    Scanner input = new Scanner(System.in);
    
    long a = input.nextLong();
    long b = input.nextLong();
    long c = input.nextLong();
    long k = input.nextLong();
    
    int ans = 0;
    if (k <= a) {
      System.out.println(k);
      return;
    }
    
    ans += a;
    k -= a;
    if (k <= b) {
      System.out.println(ans);
      return;
    }
    k -= b;
    ans -= k;
    
    System.out.println(ans);
  }
} import java.util.*; util.* java public class Main {
  public static void main(String... args) {
    Scanner input = new Scanner(System.in);
    
    long a = input.nextLong();
    long b = input.nextLong();
    long c = input.nextLong();
    long k = input.nextLong();
    
    int ans = 0;
    if (k <= a) {
      System.out.println(k);
      return;
    }
    
    ans += a;
    k -= a;
    if (k <= b) {
      System.out.println(ans);
      return;
    }
    k -= b;
    ans -= k;
    
    System.out.println(ans);
  }
} Main public static void main(String... args) {
    Scanner input = new Scanner(System.in);
    
    long a = input.nextLong();
    long b = input.nextLong();
    long c = input.nextLong();
    long k = input.nextLong();
    
    int ans = 0;
    if (k <= a) {
      System.out.println(k);
      return;
    }
    
    ans += a;
    k -= a;
    if (k <= b) {
      System.out.println(ans);
      return;
    }
    k -= b;
    ans -= k;
    
    System.out.println(ans);
  } main {
    Scanner input = new Scanner(System.in);
    
    long a = input.nextLong();
    long b = input.nextLong();
    long c = input.nextLong();
    long k = input.nextLong();
    
    int ans = 0;
    if (k <= a) {
      System.out.println(k);
      return;
    }
    
    ans += a;
    k -= a;
    if (k <= b) {
      System.out.println(ans);
      return;
    }
    k -= b;
    ans -= k;
    
    System.out.println(ans);
  } Scanner input = new Scanner(System.in); input new Scanner(System.in) long a = input.nextLong(); a input.nextLong() input.nextLong input long b = input.nextLong(); b input.nextLong() input.nextLong input long c = input.nextLong(); c input.nextLong() input.nextLong input long k = input.nextLong(); k input.nextLong() input.nextLong input int ans = 0; ans 0 if (k <= a) {
      System.out.println(k);
      return;
    } k <= a k a {
      System.out.println(k);
      return;
    } System.out.println(k) System.out.println System.out System System.out k return; ans += a ans a k -= a k a if (k <= b) {
      System.out.println(ans);
      return;
    } k <= b k b {
      System.out.println(ans);
      return;
    } System.out.println(ans) System.out.println System.out System System.out ans return; k -= b k b ans -= k ans k System.out.println(ans) System.out.println System.out System System.out ans String... args args public class Main {
  public static void main(String... args) {
    Scanner input = new Scanner(System.in);
    
    long a = input.nextLong();
    long b = input.nextLong();
    long c = input.nextLong();
    long k = input.nextLong();
    
    int ans = 0;
    if (k <= a) {
      System.out.println(k);
      return;
    }
    
    ans += a;
    k -= a;
    if (k <= b) {
      System.out.println(ans);
      return;
    }
    k -= b;
    ans -= k;
    
    System.out.println(ans);
  }
} public class Main {
  public static void main(String... args) {
    Scanner input = new Scanner(System.in);
    
    long a = input.nextLong();
    long b = input.nextLong();
    long c = input.nextLong();
    long k = input.nextLong();
    
    int ans = 0;
    if (k <= a) {
      System.out.println(k);
      return;
    }
    
    ans += a;
    k -= a;
    if (k <= b) {
      System.out.println(ans);
      return;
    }
    k -= b;
    ans -= k;
    
    System.out.println(ans);
  }
} Main
import java.util.*;

public class Main {
  public static void main(String... args) {
    Scanner input = new Scanner(System.in);
    
    long a = input.nextLong();
    long b = input.nextLong();
    long c = input.nextLong();
    long k = input.nextLong();
    
    int ans = 0;
    if (k <= a) {
      System.out.println(k);
      return;
    }
    
    ans += a;
    k -= a;
    if (k <= b) {
      System.out.println(ans);
      return;
    }
    k -= b;
    ans -= k;
    
    System.out.println(ans);
  }
}
