import java.util.Scanner;

public class Main {
	public static void main(String[] args ) {
		Scanner input = new Scanner(System.in);

		int A = input.nextInt();
		int B = input.nextInt();
		int C = input.nextInt();

		int K = input.nextInt();

		input.close();

		int aNum = 0;
		int bNum = 0;
		int cNum = 0;


		if(A < K ) {
			aNum = A;

			if(B < K - A) {

				bNum = B;

				cNum = K - A - B;

			}else {

				bNum = K - A;

			}

		}else {

			aNum = K;

		}

		System.out.println(aNum - cNum);

	}


} import java.util.Scanner; Scanner java.util public class Main {
	public static void main(String[] args ) {
		Scanner input = new Scanner(System.in);

		int A = input.nextInt();
		int B = input.nextInt();
		int C = input.nextInt();

		int K = input.nextInt();

		input.close();

		int aNum = 0;
		int bNum = 0;
		int cNum = 0;


		if(A < K ) {
			aNum = A;

			if(B < K - A) {

				bNum = B;

				cNum = K - A - B;

			}else {

				bNum = K - A;

			}

		}else {

			aNum = K;

		}

		System.out.println(aNum - cNum);

	}


} Main public static void main(String[] args ) {
		Scanner input = new Scanner(System.in);

		int A = input.nextInt();
		int B = input.nextInt();
		int C = input.nextInt();

		int K = input.nextInt();

		input.close();

		int aNum = 0;
		int bNum = 0;
		int cNum = 0;


		if(A < K ) {
			aNum = A;

			if(B < K - A) {

				bNum = B;

				cNum = K - A - B;

			}else {

				bNum = K - A;

			}

		}else {

			aNum = K;

		}

		System.out.println(aNum - cNum);

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

		int A = input.nextInt();
		int B = input.nextInt();
		int C = input.nextInt();

		int K = input.nextInt();

		input.close();

		int aNum = 0;
		int bNum = 0;
		int cNum = 0;


		if(A < K ) {
			aNum = A;

			if(B < K - A) {

				bNum = B;

				cNum = K - A - B;

			}else {

				bNum = K - A;

			}

		}else {

			aNum = K;

		}

		System.out.println(aNum - cNum);

	} Scanner input = new Scanner(System.in); input new Scanner(System.in) int A = input.nextInt(); A input.nextInt() input.nextInt input int B = input.nextInt(); B input.nextInt() input.nextInt input int C = input.nextInt(); C input.nextInt() input.nextInt input int K = input.nextInt(); K input.nextInt() input.nextInt input input.close() input.close input int aNum = 0; aNum 0 int bNum = 0; bNum 0 int cNum = 0; cNum 0 if(A < K ) {
			aNum = A;

			if(B < K - A) {

				bNum = B;

				cNum = K - A - B;

			}else {

				bNum = K - A;

			}

		}else {

			aNum = K;

		} A < K A K {
			aNum = A;

			if(B < K - A) {

				bNum = B;

				cNum = K - A - B;

			}else {

				bNum = K - A;

			}

		} aNum = A aNum A if(B < K - A) {

				bNum = B;

				cNum = K - A - B;

			}else {

				bNum = K - A;

			} B < K - A B K - A K A {

				bNum = B;

				cNum = K - A - B;

			} bNum = B bNum B cNum = K - A - B cNum K - A - B K - A - B K A B {

				bNum = K - A;

			} bNum = K - A bNum K - A K A {

			aNum = K;

		} aNum = K aNum K System.out.println(aNum - cNum) System.out.println System.out System System.out aNum - cNum aNum cNum String[] args args public class Main {
	public static void main(String[] args ) {
		Scanner input = new Scanner(System.in);

		int A = input.nextInt();
		int B = input.nextInt();
		int C = input.nextInt();

		int K = input.nextInt();

		input.close();

		int aNum = 0;
		int bNum = 0;
		int cNum = 0;


		if(A < K ) {
			aNum = A;

			if(B < K - A) {

				bNum = B;

				cNum = K - A - B;

			}else {

				bNum = K - A;

			}

		}else {

			aNum = K;

		}

		System.out.println(aNum - cNum);

	}


} public class Main {
	public static void main(String[] args ) {
		Scanner input = new Scanner(System.in);

		int A = input.nextInt();
		int B = input.nextInt();
		int C = input.nextInt();

		int K = input.nextInt();

		input.close();

		int aNum = 0;
		int bNum = 0;
		int cNum = 0;


		if(A < K ) {
			aNum = A;

			if(B < K - A) {

				bNum = B;

				cNum = K - A - B;

			}else {

				bNum = K - A;

			}

		}else {

			aNum = K;

		}

		System.out.println(aNum - cNum);

	}


} Main
import java.util.Scanner;

public class Main {
	public static void main(String[] args ) {
		Scanner input = new Scanner(System.in);

		int A = input.nextInt();
		int B = input.nextInt();
		int C = input.nextInt();

		int K = input.nextInt();

		input.close();

		int aNum = 0;
		int bNum = 0;
		int cNum = 0;


		if(A < K ) {
			aNum = A;

			if(B < K - A) {

				bNum = B;

				cNum = K - A - B;

			}else {

				bNum = K - A;

			}

		}else {

			aNum = K;

		}

		System.out.println(aNum - cNum);

	}


}