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;
		for(;0<k;k--) {
			if(a>0) {
				a--;ans++;
			}else if(b>0) {
				b--;
			}else {
				ans--;
			}
		}
		System.out.println(ans);
	}
} 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;
		for(;0<k;k--) {
			if(a>0) {
				a--;ans++;
			}else if(b>0) {
				b--;
			}else {
				ans--;
			}
		}
		System.out.println(ans);
	}
} 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;
		for(;0<k;k--) {
			if(a>0) {
				a--;ans++;
			}else if(b>0) {
				b--;
			}else {
				ans--;
			}
		}
		System.out.println(ans);
	} 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;
		for(;0<k;k--) {
			if(a>0) {
				a--;ans++;
			}else if(b>0) {
				b--;
			}else {
				ans--;
			}
		}
		System.out.println(ans);
	} 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 for(;0<k;k--) {
			if(a>0) {
				a--;ans++;
			}else if(b>0) {
				b--;
			}else {
				ans--;
			}
		} ; 0<k 0 k k-- k-- k {
			if(a>0) {
				a--;ans++;
			}else if(b>0) {
				b--;
			}else {
				ans--;
			}
		} {
			if(a>0) {
				a--;ans++;
			}else if(b>0) {
				b--;
			}else {
				ans--;
			}
		} if(a>0) {
				a--;ans++;
			}else if(b>0) {
				b--;
			}else {
				ans--;
			} a>0 a 0 {
				a--;ans++;
			} a-- a ans++ ans if(b>0) {
				b--;
			}else {
				ans--;
			} b>0 b 0 {
				b--;
			} b-- b {
				ans--;
			} ans-- ans System.out.println(ans) System.out.println System.out System System.out ans 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;
		for(;0<k;k--) {
			if(a>0) {
				a--;ans++;
			}else if(b>0) {
				b--;
			}else {
				ans--;
			}
		}
		System.out.println(ans);
	}
} 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;
		for(;0<k;k--) {
			if(a>0) {
				a--;ans++;
			}else if(b>0) {
				b--;
			}else {
				ans--;
			}
		}
		System.out.println(ans);
	}
} 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;
		for(;0<k;k--) {
			if(a>0) {
				a--;ans++;
			}else if(b>0) {
				b--;
			}else {
				ans--;
			}
		}
		System.out.println(ans);
	}
}