import java.util.*;

class Main {
  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    int A = sc.nextInt();
    int B = sc.nextInt();
    int C = sc.nextInt();
    int K = sc.nextInt();
    int ans = 0;
    if (A >= K) {
      ans = K;
    } else if (A+B >= K){
      ans = A;
    } else {
      ans = A - (K-(A+B));
    }
    System.out.println(ans);
  }
} import java.util.*; util.* java class Main {
  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    int A = sc.nextInt();
    int B = sc.nextInt();
    int C = sc.nextInt();
    int K = sc.nextInt();
    int ans = 0;
    if (A >= K) {
      ans = K;
    } else if (A+B >= K){
      ans = A;
    } else {
      ans = A - (K-(A+B));
    }
    System.out.println(ans);
  }
} Main public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    int A = sc.nextInt();
    int B = sc.nextInt();
    int C = sc.nextInt();
    int K = sc.nextInt();
    int ans = 0;
    if (A >= K) {
      ans = K;
    } else if (A+B >= K){
      ans = A;
    } else {
      ans = A - (K-(A+B));
    }
    System.out.println(ans);
  } main {
    Scanner sc = new Scanner(System.in);
    int A = sc.nextInt();
    int B = sc.nextInt();
    int C = sc.nextInt();
    int K = sc.nextInt();
    int ans = 0;
    if (A >= K) {
      ans = K;
    } else if (A+B >= K){
      ans = A;
    } else {
      ans = A - (K-(A+B));
    }
    System.out.println(ans);
  } Scanner sc = new Scanner(System.in); sc new Scanner(System.in) int A = sc.nextInt(); A sc.nextInt() sc.nextInt sc int B = sc.nextInt(); B sc.nextInt() sc.nextInt sc int C = sc.nextInt(); C sc.nextInt() sc.nextInt sc int K = sc.nextInt(); K sc.nextInt() sc.nextInt sc int ans = 0; ans 0 if (A >= K) {
      ans = K;
    } else if (A+B >= K){
      ans = A;
    } else {
      ans = A - (K-(A+B));
    } A >= K A K {
      ans = K;
    } ans = K ans K if (A+B >= K){
      ans = A;
    } else {
      ans = A - (K-(A+B));
    } A+B >= K A+B A B K {
      ans = A;
    } ans = A ans A {
      ans = A - (K-(A+B));
    } ans = A - (K-(A+B)) ans A - (K-(A+B)) A (K-(A+B)) K (A+B) A B System.out.println(ans) System.out.println System.out System System.out ans String[] args args
import java.util.*;

class Main {
  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    int A = sc.nextInt();
    int B = sc.nextInt();
    int C = sc.nextInt();
    int K = sc.nextInt();
    int ans = 0;
    if (A >= K) {
      ans = K;
    } else if (A+B >= K){
      ans = A;
    } else {
      ans = A - (K-(A+B));
    }
    System.out.println(ans);
  }
}
