
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 ans = 0;

		int point1 = Math.min(A, K);
		int point0 = 0;
		int pointMinus1 = 0;


		if(K - point1 > 0) {
			point0 = Math.min(B, K-point1);

			if(K - point1 - point0 > 0) {
				pointMinus1 = K - point1 - point0;
			}

		}

		ans = point1 * 1 + point0 * 0 - pointMinus1 * 1;

		System.out.println(ans);
		scan.close();

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

		int point1 = Math.min(A, K);
		int point0 = 0;
		int pointMinus1 = 0;


		if(K - point1 > 0) {
			point0 = Math.min(B, K-point1);

			if(K - point1 - point0 > 0) {
				pointMinus1 = K - point1 - point0;
			}

		}

		ans = point1 * 1 + point0 * 0 - pointMinus1 * 1;

		System.out.println(ans);
		scan.close();

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

		int point1 = Math.min(A, K);
		int point0 = 0;
		int pointMinus1 = 0;


		if(K - point1 > 0) {
			point0 = Math.min(B, K-point1);

			if(K - point1 - point0 > 0) {
				pointMinus1 = K - point1 - point0;
			}

		}

		ans = point1 * 1 + point0 * 0 - pointMinus1 * 1;

		System.out.println(ans);
		scan.close();

	} main {
		Scanner scan = new Scanner(System.in);
		int A = scan.nextInt();
		int B = scan.nextInt();
		int C = scan.nextInt();
		int K = scan.nextInt();
		int ans = 0;

		int point1 = Math.min(A, K);
		int point0 = 0;
		int pointMinus1 = 0;


		if(K - point1 > 0) {
			point0 = Math.min(B, K-point1);

			if(K - point1 - point0 > 0) {
				pointMinus1 = K - point1 - point0;
			}

		}

		ans = point1 * 1 + point0 * 0 - pointMinus1 * 1;

		System.out.println(ans);
		scan.close();

	} 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 ans = 0; ans 0 int point1 = Math.min(A, K); point1 Math.min(A, K) Math.min Math A K int point0 = 0; point0 0 int pointMinus1 = 0; pointMinus1 0 if(K - point1 > 0) {
			point0 = Math.min(B, K-point1);

			if(K - point1 - point0 > 0) {
				pointMinus1 = K - point1 - point0;
			}

		} K - point1 > 0 K - point1 K point1 0 {
			point0 = Math.min(B, K-point1);

			if(K - point1 - point0 > 0) {
				pointMinus1 = K - point1 - point0;
			}

		} point0 = Math.min(B, K-point1) point0 Math.min(B, K-point1) Math.min Math B K-point1 K point1 if(K - point1 - point0 > 0) {
				pointMinus1 = K - point1 - point0;
			} K - point1 - point0 > 0 K - point1 - point0 K - point1 - point0 K point1 point0 0 {
				pointMinus1 = K - point1 - point0;
			} pointMinus1 = K - point1 - point0 pointMinus1 K - point1 - point0 K - point1 - point0 K point1 point0 ans = point1 * 1 + point0 * 0 - pointMinus1 * 1 ans point1 * 1 + point0 * 0 - pointMinus1 * 1 point1 * 1 + point0 * 0 point1 * 1 point1 1 point0 * 0 point0 0 pointMinus1 * 1 pointMinus1 1 System.out.println(ans) System.out.println System.out System System.out ans scan.close() scan.close scan 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 ans = 0;

		int point1 = Math.min(A, K);
		int point0 = 0;
		int pointMinus1 = 0;


		if(K - point1 > 0) {
			point0 = Math.min(B, K-point1);

			if(K - point1 - point0 > 0) {
				pointMinus1 = K - point1 - point0;
			}

		}

		ans = point1 * 1 + point0 * 0 - pointMinus1 * 1;

		System.out.println(ans);
		scan.close();

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

		int point1 = Math.min(A, K);
		int point0 = 0;
		int pointMinus1 = 0;


		if(K - point1 > 0) {
			point0 = Math.min(B, K-point1);

			if(K - point1 - point0 > 0) {
				pointMinus1 = K - point1 - point0;
			}

		}

		ans = point1 * 1 + point0 * 0 - pointMinus1 * 1;

		System.out.println(ans);
		scan.close();

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

		int point1 = Math.min(A, K);
		int point0 = 0;
		int pointMinus1 = 0;


		if(K - point1 > 0) {
			point0 = Math.min(B, K-point1);

			if(K - point1 - point0 > 0) {
				pointMinus1 = K - point1 - point0;
			}

		}

		ans = point1 * 1 + point0 * 0 - pointMinus1 * 1;

		System.out.println(ans);
		scan.close();

	}
}
