import java.util.Scanner;

public class Main {
  public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in);
    long A = scanner.nextInt();
    long B = scanner.nextInt();
    long C = scanner.nextInt();
    long K = scanner.nextInt();
    long minus = A + B >= K ? 0 : K - A - B;
    long plus = A >= K ? K : A;
    System.out.println(plus - minus);
  }
} import java.util.Scanner; Scanner java.util public class Main {
  public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in);
    long A = scanner.nextInt();
    long B = scanner.nextInt();
    long C = scanner.nextInt();
    long K = scanner.nextInt();
    long minus = A + B >= K ? 0 : K - A - B;
    long plus = A >= K ? K : A;
    System.out.println(plus - minus);
  }
} Main public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in);
    long A = scanner.nextInt();
    long B = scanner.nextInt();
    long C = scanner.nextInt();
    long K = scanner.nextInt();
    long minus = A + B >= K ? 0 : K - A - B;
    long plus = A >= K ? K : A;
    System.out.println(plus - minus);
  } main {
    Scanner scanner = new Scanner(System.in);
    long A = scanner.nextInt();
    long B = scanner.nextInt();
    long C = scanner.nextInt();
    long K = scanner.nextInt();
    long minus = A + B >= K ? 0 : K - A - B;
    long plus = A >= K ? K : A;
    System.out.println(plus - minus);
  } Scanner scanner = new Scanner(System.in); scanner new Scanner(System.in) long A = scanner.nextInt(); A scanner.nextInt() scanner.nextInt scanner long B = scanner.nextInt(); B scanner.nextInt() scanner.nextInt scanner long C = scanner.nextInt(); C scanner.nextInt() scanner.nextInt scanner long K = scanner.nextInt(); K scanner.nextInt() scanner.nextInt scanner long minus = A + B >= K ? 0 : K - A - B; minus A + B >= K ? 0 : K - A - B A + B >= K A + B A B K 0 K - A - B K - A - B K A B long plus = A >= K ? K : A; plus A >= K ? K : A A >= K A K K A System.out.println(plus - minus) System.out.println System.out System System.out plus - minus plus minus String[] args args public class Main {
  public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in);
    long A = scanner.nextInt();
    long B = scanner.nextInt();
    long C = scanner.nextInt();
    long K = scanner.nextInt();
    long minus = A + B >= K ? 0 : K - A - B;
    long plus = A >= K ? K : A;
    System.out.println(plus - minus);
  }
} public class Main {
  public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in);
    long A = scanner.nextInt();
    long B = scanner.nextInt();
    long C = scanner.nextInt();
    long K = scanner.nextInt();
    long minus = A + B >= K ? 0 : K - A - B;
    long plus = A >= K ? K : A;
    System.out.println(plus - minus);
  }
} Main
import java.util.Scanner;

public class Main {
  public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in);
    long A = scanner.nextInt();
    long B = scanner.nextInt();
    long C = scanner.nextInt();
    long K = scanner.nextInt();
    long minus = A + B >= K ? 0 : K - A - B;
    long plus = A >= K ? K : A;
    System.out.println(plus - minus);
  }
}
