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

		if(A >= K) {
			S = K;
		} else {
			S = A;
			K -= A;
			K -= B;
			if(K > 0) {
				S -= K;
			}
		}
		System.out.println(S);
	}
} 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 S = 0;

		if(A >= K) {
			S = K;
		} else {
			S = A;
			K -= A;
			K -= B;
			if(K > 0) {
				S -= K;
			}
		}
		System.out.println(S);
	}
} 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 S = 0;

		if(A >= K) {
			S = K;
		} else {
			S = A;
			K -= A;
			K -= B;
			if(K > 0) {
				S -= K;
			}
		}
		System.out.println(S);
	} main {

		Scanner scan = new Scanner(System.in);

		int A = scan.nextInt();
		int B = scan.nextInt();
		int C = scan.nextInt();
		int K = scan.nextInt();
		int S = 0;

		if(A >= K) {
			S = K;
		} else {
			S = A;
			K -= A;
			K -= B;
			if(K > 0) {
				S -= K;
			}
		}
		System.out.println(S);
	} 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 S = 0; S 0 if(A >= K) {
			S = K;
		} else {
			S = A;
			K -= A;
			K -= B;
			if(K > 0) {
				S -= K;
			}
		} A >= K A K {
			S = K;
		} S = K S K {
			S = A;
			K -= A;
			K -= B;
			if(K > 0) {
				S -= K;
			}
		} S = A S A K -= A K A K -= B K B if(K > 0) {
				S -= K;
			} K > 0 K 0 {
				S -= K;
			} S -= K S K System.out.println(S) System.out.println System.out System System.out S 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 S = 0;

		if(A >= K) {
			S = K;
		} else {
			S = A;
			K -= A;
			K -= B;
			if(K > 0) {
				S -= K;
			}
		}
		System.out.println(S);
	}
} 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 S = 0;

		if(A >= K) {
			S = K;
		} else {
			S = A;
			K -= A;
			K -= B;
			if(K > 0) {
				S -= K;
			}
		}
		System.out.println(S);
	}
} 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 S = 0;

		if(A >= K) {
			S = K;
		} else {
			S = A;
			K -= A;
			K -= B;
			if(K > 0) {
				S -= K;
			}
		}
		System.out.println(S);
	}
}