import java.util.Scanner;

public 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 sum = 0;

		while (k > 0) {
			if (a != 0) {
				a--;
				k--;
				sum += 1;
			} else if (b != 0) {
				b--;
				k--;
				sum += 0;
			} else if (c != 0) {
				c--;
				k--;
				sum += -1;
			}
		}

		System.out.println(sum);
		sc.close();

	}
} import java.util.Scanner; Scanner java.util public 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 sum = 0;

		while (k > 0) {
			if (a != 0) {
				a--;
				k--;
				sum += 1;
			} else if (b != 0) {
				b--;
				k--;
				sum += 0;
			} else if (c != 0) {
				c--;
				k--;
				sum += -1;
			}
		}

		System.out.println(sum);
		sc.close();

	}
} 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 sum = 0;

		while (k > 0) {
			if (a != 0) {
				a--;
				k--;
				sum += 1;
			} else if (b != 0) {
				b--;
				k--;
				sum += 0;
			} else if (c != 0) {
				c--;
				k--;
				sum += -1;
			}
		}

		System.out.println(sum);
		sc.close();

	} main {
		Scanner sc = new Scanner(System.in);

		int a = sc.nextInt();
		int b = sc.nextInt();
		int c = sc.nextInt();
		int k = sc.nextInt();

		int sum = 0;

		while (k > 0) {
			if (a != 0) {
				a--;
				k--;
				sum += 1;
			} else if (b != 0) {
				b--;
				k--;
				sum += 0;
			} else if (c != 0) {
				c--;
				k--;
				sum += -1;
			}
		}

		System.out.println(sum);
		sc.close();

	} 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 sum = 0; sum 0 while (k > 0) {
			if (a != 0) {
				a--;
				k--;
				sum += 1;
			} else if (b != 0) {
				b--;
				k--;
				sum += 0;
			} else if (c != 0) {
				c--;
				k--;
				sum += -1;
			}
		} k > 0 k 0 {
			if (a != 0) {
				a--;
				k--;
				sum += 1;
			} else if (b != 0) {
				b--;
				k--;
				sum += 0;
			} else if (c != 0) {
				c--;
				k--;
				sum += -1;
			}
		} if (a != 0) {
				a--;
				k--;
				sum += 1;
			} else if (b != 0) {
				b--;
				k--;
				sum += 0;
			} else if (c != 0) {
				c--;
				k--;
				sum += -1;
			} a != 0 a 0 {
				a--;
				k--;
				sum += 1;
			} a-- a k-- k sum += 1 sum 1 if (b != 0) {
				b--;
				k--;
				sum += 0;
			} else if (c != 0) {
				c--;
				k--;
				sum += -1;
			} b != 0 b 0 {
				b--;
				k--;
				sum += 0;
			} b-- b k-- k sum += 0 sum 0 if (c != 0) {
				c--;
				k--;
				sum += -1;
			} c != 0 c 0 {
				c--;
				k--;
				sum += -1;
			} c-- c k-- k sum += -1 sum -1 1 System.out.println(sum) System.out.println System.out System System.out sum sc.close() sc.close sc String[] args args public 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 sum = 0;

		while (k > 0) {
			if (a != 0) {
				a--;
				k--;
				sum += 1;
			} else if (b != 0) {
				b--;
				k--;
				sum += 0;
			} else if (c != 0) {
				c--;
				k--;
				sum += -1;
			}
		}

		System.out.println(sum);
		sc.close();

	}
} public 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 sum = 0;

		while (k > 0) {
			if (a != 0) {
				a--;
				k--;
				sum += 1;
			} else if (b != 0) {
				b--;
				k--;
				sum += 0;
			} else if (c != 0) {
				c--;
				k--;
				sum += -1;
			}
		}

		System.out.println(sum);
		sc.close();

	}
} Main
import java.util.Scanner;

public 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 sum = 0;

		while (k > 0) {
			if (a != 0) {
				a--;
				k--;
				sum += 1;
			} else if (b != 0) {
				b--;
				k--;
				sum += 0;
			} else if (c != 0) {
				c--;
				k--;
				sum += -1;
			}
		}

		System.out.println(sum);
		sc.close();

	}
}