import java.util.Scanner;

public class Main {

	public static void main(String[] args) {

		Scanner scan = new Scanner(System.in);
		int a = scan.nextInt();
		int b = scan.nextInt();
		int c = scan.nextInt();
		int k = scan.nextInt();
		int max = 0;

		if (k <= a) {
			max = k;
		} else if (k <= (a + b)) {
			max = a;
		} else {
//			max = a - (k - a - b);
			k = k - a;
			k = k - b;
			max = a - k;
		}

//		if (0 < max) {
			System.out.print(max);
//		} else {
//			System.out.print(0);
//		}

	}
} import java.util.Scanner; Scanner java.util public class Main {

	public static void main(String[] args) {

		Scanner scan = new Scanner(System.in);
		int a = scan.nextInt();
		int b = scan.nextInt();
		int c = scan.nextInt();
		int k = scan.nextInt();
		int max = 0;

		if (k <= a) {
			max = k;
		} else if (k <= (a + b)) {
			max = a;
		} else {
//			max = a - (k - a - b);
			k = k - a;
			k = k - b;
			max = a - k;
		}

//		if (0 < max) {
			System.out.print(max);
//		} else {
//			System.out.print(0);
//		}

	}
} Main public static void main(String[] args) {

		Scanner scan = new Scanner(System.in);
		int a = scan.nextInt();
		int b = scan.nextInt();
		int c = scan.nextInt();
		int k = scan.nextInt();
		int max = 0;

		if (k <= a) {
			max = k;
		} else if (k <= (a + b)) {
			max = a;
		} else {
//			max = a - (k - a - b);
			k = k - a;
			k = k - b;
			max = a - k;
		}

//		if (0 < max) {
			System.out.print(max);
//		} else {
//			System.out.print(0);
//		}

	} main {

		Scanner scan = new Scanner(System.in);
		int a = scan.nextInt();
		int b = scan.nextInt();
		int c = scan.nextInt();
		int k = scan.nextInt();
		int max = 0;

		if (k <= a) {
			max = k;
		} else if (k <= (a + b)) {
			max = a;
		} else {
//			max = a - (k - a - b);
			k = k - a;
			k = k - b;
			max = a - k;
		}

//		if (0 < max) {
			System.out.print(max);
//		} else {
//			System.out.print(0);
//		}

	} Scanner scan = new Scanner(System.in); scan new Scanner(System.in) int a = scan.nextInt(); a scan.nextInt() scan.nextInt scan int b = scan.nextInt(); b scan.nextInt() scan.nextInt scan int c = scan.nextInt(); c scan.nextInt() scan.nextInt scan int k = scan.nextInt(); k scan.nextInt() scan.nextInt scan int max = 0; max 0 if (k <= a) {
			max = k;
		} else if (k <= (a + b)) {
			max = a;
		} else {
//			max = a - (k - a - b);
			k = k - a;
			k = k - b;
			max = a - k;
		} k <= a k a {
			max = k;
		} max = k max k if (k <= (a + b)) {
			max = a;
		} else {
//			max = a - (k - a - b);
			k = k - a;
			k = k - b;
			max = a - k;
		} k <= (a + b) k (a + b) a b {
			max = a;
		} max = a max a {
//			max = a - (k - a - b);
			k = k - a;
			k = k - b;
			max = a - k;
		} k = k - a k k - a k a k = k - b k k - b k b max = a - k max a - k a k System.out.print(max) System.out.print System.out System System.out max String[] args args public class Main {

	public static void main(String[] args) {

		Scanner scan = new Scanner(System.in);
		int a = scan.nextInt();
		int b = scan.nextInt();
		int c = scan.nextInt();
		int k = scan.nextInt();
		int max = 0;

		if (k <= a) {
			max = k;
		} else if (k <= (a + b)) {
			max = a;
		} else {
//			max = a - (k - a - b);
			k = k - a;
			k = k - b;
			max = a - k;
		}

//		if (0 < max) {
			System.out.print(max);
//		} else {
//			System.out.print(0);
//		}

	}
} public class Main {

	public static void main(String[] args) {

		Scanner scan = new Scanner(System.in);
		int a = scan.nextInt();
		int b = scan.nextInt();
		int c = scan.nextInt();
		int k = scan.nextInt();
		int max = 0;

		if (k <= a) {
			max = k;
		} else if (k <= (a + b)) {
			max = a;
		} else {
//			max = a - (k - a - b);
			k = k - a;
			k = k - b;
			max = a - k;
		}

//		if (0 < max) {
			System.out.print(max);
//		} else {
//			System.out.print(0);
//		}

	}
} Main
import java.util.Scanner;

public class Main {

	public static void main(String[] args) {

		Scanner scan = new Scanner(System.in);
		int a = scan.nextInt();
		int b = scan.nextInt();
		int c = scan.nextInt();
		int k = scan.nextInt();
		int max = 0;

		if (k <= a) {
			max = k;
		} else if (k <= (a + b)) {
			max = a;
		} else {
//			max = a - (k - a - b);
			k = k - a;
			k = k - b;
			max = a - k;
		}

//		if (0 < max) {
			System.out.print(max);
//		} else {
//			System.out.print(0);
//		}

	}
}