import java.util.Scanner;

public class Main {

  public static void main(String[] args) {
    final Scanner scanner = new Scanner(System.in);

    final int a = scanner.nextInt();
    final int b = scanner.nextInt();
    final int c = scanner.nextInt();
    int k = scanner.nextInt();

    int sum = 0;
    if (k >= a) {
      k -= a;
      sum += a;
    } else {
      sum += k;
      k = 0;
    }

    if (k >= b) {
      k -= b;
    } else {
      k = 0;
    }

    sum -= k;

    System.out.println(sum);
  }
} import java.util.Scanner; Scanner java.util public class Main {

  public static void main(String[] args) {
    final Scanner scanner = new Scanner(System.in);

    final int a = scanner.nextInt();
    final int b = scanner.nextInt();
    final int c = scanner.nextInt();
    int k = scanner.nextInt();

    int sum = 0;
    if (k >= a) {
      k -= a;
      sum += a;
    } else {
      sum += k;
      k = 0;
    }

    if (k >= b) {
      k -= b;
    } else {
      k = 0;
    }

    sum -= k;

    System.out.println(sum);
  }
} Main public static void main(String[] args) {
    final Scanner scanner = new Scanner(System.in);

    final int a = scanner.nextInt();
    final int b = scanner.nextInt();
    final int c = scanner.nextInt();
    int k = scanner.nextInt();

    int sum = 0;
    if (k >= a) {
      k -= a;
      sum += a;
    } else {
      sum += k;
      k = 0;
    }

    if (k >= b) {
      k -= b;
    } else {
      k = 0;
    }

    sum -= k;

    System.out.println(sum);
  } main {
    final Scanner scanner = new Scanner(System.in);

    final int a = scanner.nextInt();
    final int b = scanner.nextInt();
    final int c = scanner.nextInt();
    int k = scanner.nextInt();

    int sum = 0;
    if (k >= a) {
      k -= a;
      sum += a;
    } else {
      sum += k;
      k = 0;
    }

    if (k >= b) {
      k -= b;
    } else {
      k = 0;
    }

    sum -= k;

    System.out.println(sum);
  } final Scanner scanner = new Scanner(System.in); scanner new Scanner(System.in) final int a = scanner.nextInt(); a scanner.nextInt() scanner.nextInt scanner final int b = scanner.nextInt(); b scanner.nextInt() scanner.nextInt scanner final int c = scanner.nextInt(); c scanner.nextInt() scanner.nextInt scanner int k = scanner.nextInt(); k scanner.nextInt() scanner.nextInt scanner int sum = 0; sum 0 if (k >= a) {
      k -= a;
      sum += a;
    } else {
      sum += k;
      k = 0;
    } k >= a k a {
      k -= a;
      sum += a;
    } k -= a k a sum += a sum a {
      sum += k;
      k = 0;
    } sum += k sum k k = 0 k 0 if (k >= b) {
      k -= b;
    } else {
      k = 0;
    } k >= b k b {
      k -= b;
    } k -= b k b {
      k = 0;
    } k = 0 k 0 sum -= k sum k System.out.println(sum) System.out.println System.out System System.out sum String[] args args public class Main {

  public static void main(String[] args) {
    final Scanner scanner = new Scanner(System.in);

    final int a = scanner.nextInt();
    final int b = scanner.nextInt();
    final int c = scanner.nextInt();
    int k = scanner.nextInt();

    int sum = 0;
    if (k >= a) {
      k -= a;
      sum += a;
    } else {
      sum += k;
      k = 0;
    }

    if (k >= b) {
      k -= b;
    } else {
      k = 0;
    }

    sum -= k;

    System.out.println(sum);
  }
} public class Main {

  public static void main(String[] args) {
    final Scanner scanner = new Scanner(System.in);

    final int a = scanner.nextInt();
    final int b = scanner.nextInt();
    final int c = scanner.nextInt();
    int k = scanner.nextInt();

    int sum = 0;
    if (k >= a) {
      k -= a;
      sum += a;
    } else {
      sum += k;
      k = 0;
    }

    if (k >= b) {
      k -= b;
    } else {
      k = 0;
    }

    sum -= k;

    System.out.println(sum);
  }
} Main
import java.util.Scanner;

public class Main {

  public static void main(String[] args) {
    final Scanner scanner = new Scanner(System.in);

    final int a = scanner.nextInt();
    final int b = scanner.nextInt();
    final int c = scanner.nextInt();
    int k = scanner.nextInt();

    int sum = 0;
    if (k >= a) {
      k -= a;
      sum += a;
    } else {
      sum += k;
      k = 0;
    }

    if (k >= b) {
      k -= b;
    } else {
      k = 0;
    }

    sum -= k;

    System.out.println(sum);
  }
}