import java.util.*;

class Main {
  static Scanner sc = new Scanner(System.in);

  public static void main(String[] args) {
    int a = sc.nextInt();
    int b = sc.nextInt();
    int c = sc.nextInt();
    int k = sc.nextInt();
    if (k <= a) {
      System.out.println(k);
    } else if (k > a && k <= a + b) {
      System.out.println(a);
    } else {
      System.out.println(a - (k - a - b));
    }
  }
} import java.util.*; util.* java class Main {
  static Scanner sc = new Scanner(System.in);

  public static void main(String[] args) {
    int a = sc.nextInt();
    int b = sc.nextInt();
    int c = sc.nextInt();
    int k = sc.nextInt();
    if (k <= a) {
      System.out.println(k);
    } else if (k > a && k <= a + b) {
      System.out.println(a);
    } else {
      System.out.println(a - (k - a - b));
    }
  }
} Main static Scanner sc = new Scanner(System.in); sc new Scanner(System.in) public static void main(String[] args) {
    int a = sc.nextInt();
    int b = sc.nextInt();
    int c = sc.nextInt();
    int k = sc.nextInt();
    if (k <= a) {
      System.out.println(k);
    } else if (k > a && k <= a + b) {
      System.out.println(a);
    } else {
      System.out.println(a - (k - a - b));
    }
  } main {
    int a = sc.nextInt();
    int b = sc.nextInt();
    int c = sc.nextInt();
    int k = sc.nextInt();
    if (k <= a) {
      System.out.println(k);
    } else if (k > a && k <= a + b) {
      System.out.println(a);
    } else {
      System.out.println(a - (k - a - b));
    }
  } 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 if (k <= a) {
      System.out.println(k);
    } else if (k > a && k <= a + b) {
      System.out.println(a);
    } else {
      System.out.println(a - (k - a - b));
    } k <= a k a {
      System.out.println(k);
    } System.out.println(k) System.out.println System.out System System.out k if (k > a && k <= a + b) {
      System.out.println(a);
    } else {
      System.out.println(a - (k - a - b));
    } k > a && k <= a + b k > a k a k <= a + b k a + b a b {
      System.out.println(a);
    } System.out.println(a) System.out.println System.out System System.out a {
      System.out.println(a - (k - a - b));
    } System.out.println(a - (k - a - b)) System.out.println System.out System System.out a - (k - a - b) a (k - a - b) k - a - b k a b String[] args args
import java.util.*;

class Main {
  static Scanner sc = new Scanner(System.in);

  public static void main(String[] args) {
    int a = sc.nextInt();
    int b = sc.nextInt();
    int c = sc.nextInt();
    int k = sc.nextInt();
    if (k <= a) {
      System.out.println(k);
    } else if (k > a && k <= a + b) {
      System.out.println(a);
    } else {
      System.out.println(a - (k - a - b));
    }
  }
}