import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		// TODO 自動生成されたメソッド・スタブ
		Scanner in = new Scanner(System.in);
		int[] s  = {0,0,0,0};
		int a = in.nextInt();
		int b = in.nextInt();
		int c = in.nextInt();
		int k = in.nextInt();
		int full = 0;

		if(a == k) {
			full = a;
		}else if(a > k) {
			full = k;
		}else {
			full = a;
			if(b <= k-a) {
				full -= k-a-b;
			}
		}
		System.out.println(full);

	}

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

	public static void main(String[] args) {
		// TODO 自動生成されたメソッド・スタブ
		Scanner in = new Scanner(System.in);
		int[] s  = {0,0,0,0};
		int a = in.nextInt();
		int b = in.nextInt();
		int c = in.nextInt();
		int k = in.nextInt();
		int full = 0;

		if(a == k) {
			full = a;
		}else if(a > k) {
			full = k;
		}else {
			full = a;
			if(b <= k-a) {
				full -= k-a-b;
			}
		}
		System.out.println(full);

	}

} Main public static void main(String[] args) {
		// TODO 自動生成されたメソッド・スタブ
		Scanner in = new Scanner(System.in);
		int[] s  = {0,0,0,0};
		int a = in.nextInt();
		int b = in.nextInt();
		int c = in.nextInt();
		int k = in.nextInt();
		int full = 0;

		if(a == k) {
			full = a;
		}else if(a > k) {
			full = k;
		}else {
			full = a;
			if(b <= k-a) {
				full -= k-a-b;
			}
		}
		System.out.println(full);

	} main {
		// TODO 自動生成されたメソッド・スタブ
		Scanner in = new Scanner(System.in);
		int[] s  = {0,0,0,0};
		int a = in.nextInt();
		int b = in.nextInt();
		int c = in.nextInt();
		int k = in.nextInt();
		int full = 0;

		if(a == k) {
			full = a;
		}else if(a > k) {
			full = k;
		}else {
			full = a;
			if(b <= k-a) {
				full -= k-a-b;
			}
		}
		System.out.println(full);

	} Scanner in = new Scanner(System.in); in new Scanner(System.in) int[] s  = {0,0,0,0}; s {0,0,0,0} 0 0 0 0 int a = in.nextInt(); a in.nextInt() in.nextInt in int b = in.nextInt(); b in.nextInt() in.nextInt in int c = in.nextInt(); c in.nextInt() in.nextInt in int k = in.nextInt(); k in.nextInt() in.nextInt in int full = 0; full 0 if(a == k) {
			full = a;
		}else if(a > k) {
			full = k;
		}else {
			full = a;
			if(b <= k-a) {
				full -= k-a-b;
			}
		} a == k a k {
			full = a;
		} full = a full a if(a > k) {
			full = k;
		}else {
			full = a;
			if(b <= k-a) {
				full -= k-a-b;
			}
		} a > k a k {
			full = k;
		} full = k full k {
			full = a;
			if(b <= k-a) {
				full -= k-a-b;
			}
		} full = a full a if(b <= k-a) {
				full -= k-a-b;
			} b <= k-a b k-a k a {
				full -= k-a-b;
			} full -= k-a-b full k-a-b k-a-b k a b System.out.println(full) System.out.println System.out System System.out full String[] args args public class Main {

	public static void main(String[] args) {
		// TODO 自動生成されたメソッド・スタブ
		Scanner in = new Scanner(System.in);
		int[] s  = {0,0,0,0};
		int a = in.nextInt();
		int b = in.nextInt();
		int c = in.nextInt();
		int k = in.nextInt();
		int full = 0;

		if(a == k) {
			full = a;
		}else if(a > k) {
			full = k;
		}else {
			full = a;
			if(b <= k-a) {
				full -= k-a-b;
			}
		}
		System.out.println(full);

	}

} public class Main {

	public static void main(String[] args) {
		// TODO 自動生成されたメソッド・スタブ
		Scanner in = new Scanner(System.in);
		int[] s  = {0,0,0,0};
		int a = in.nextInt();
		int b = in.nextInt();
		int c = in.nextInt();
		int k = in.nextInt();
		int full = 0;

		if(a == k) {
			full = a;
		}else if(a > k) {
			full = k;
		}else {
			full = a;
			if(b <= k-a) {
				full -= k-a-b;
			}
		}
		System.out.println(full);

	}

} Main
import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		// TODO 自動生成されたメソッド・スタブ
		Scanner in = new Scanner(System.in);
		int[] s  = {0,0,0,0};
		int a = in.nextInt();
		int b = in.nextInt();
		int c = in.nextInt();
		int k = in.nextInt();
		int full = 0;

		if(a == k) {
			full = a;
		}else if(a > k) {
			full = k;
		}else {
			full = a;
			if(b <= k-a) {
				full -= k-a-b;
			}
		}
		System.out.println(full);

	}

}
