import java.util.*;
import java.io.*;
import java.lang.*;

public class Main {
	public static void main (String[] args) {
		Scanner sc = new Scanner(System.in);
		int a = sc.nextInt();	
		int b = sc.nextInt();
		int c = sc.nextInt();
		int k = sc.nextInt();
		int value = 0;
		while (k > 0) {
			if (a!=0) {
				a--;
				k--; 
				value+=1;
			} else if (b != 0) {
				b--; k--; ;
			} else if (c != 0) {
				c--; k--; value-=1;
			} else {
				break;
			}
		}

		System.out.println(value);
	}
} import java.util.*; util.* java import java.io.*; io.* java import java.lang.*; lang.* java public class Main {
	public static void main (String[] args) {
		Scanner sc = new Scanner(System.in);
		int a = sc.nextInt();	
		int b = sc.nextInt();
		int c = sc.nextInt();
		int k = sc.nextInt();
		int value = 0;
		while (k > 0) {
			if (a!=0) {
				a--;
				k--; 
				value+=1;
			} else if (b != 0) {
				b--; k--; ;
			} else if (c != 0) {
				c--; k--; value-=1;
			} else {
				break;
			}
		}

		System.out.println(value);
	}
} Main public static void main (String[] args) {
		Scanner sc = new Scanner(System.in);
		int a = sc.nextInt();	
		int b = sc.nextInt();
		int c = sc.nextInt();
		int k = sc.nextInt();
		int value = 0;
		while (k > 0) {
			if (a!=0) {
				a--;
				k--; 
				value+=1;
			} else if (b != 0) {
				b--; k--; ;
			} else if (c != 0) {
				c--; k--; value-=1;
			} else {
				break;
			}
		}

		System.out.println(value);
	} main {
		Scanner sc = new Scanner(System.in);
		int a = sc.nextInt();	
		int b = sc.nextInt();
		int c = sc.nextInt();
		int k = sc.nextInt();
		int value = 0;
		while (k > 0) {
			if (a!=0) {
				a--;
				k--; 
				value+=1;
			} else if (b != 0) {
				b--; k--; ;
			} else if (c != 0) {
				c--; k--; value-=1;
			} else {
				break;
			}
		}

		System.out.println(value);
	} Scanner sc = new Scanner(System.in); sc new Scanner(System.in) int a = sc.nextInt(); a sc.nextInt() sc.nextInt sc int b = sc.nextInt(); b sc.nextInt() sc.nextInt sc int c = sc.nextInt(); c sc.nextInt() sc.nextInt sc int k = sc.nextInt(); k sc.nextInt() sc.nextInt sc int value = 0; value 0 while (k > 0) {
			if (a!=0) {
				a--;
				k--; 
				value+=1;
			} else if (b != 0) {
				b--; k--; ;
			} else if (c != 0) {
				c--; k--; value-=1;
			} else {
				break;
			}
		} k > 0 k 0 {
			if (a!=0) {
				a--;
				k--; 
				value+=1;
			} else if (b != 0) {
				b--; k--; ;
			} else if (c != 0) {
				c--; k--; value-=1;
			} else {
				break;
			}
		} if (a!=0) {
				a--;
				k--; 
				value+=1;
			} else if (b != 0) {
				b--; k--; ;
			} else if (c != 0) {
				c--; k--; value-=1;
			} else {
				break;
			} a!=0 a 0 {
				a--;
				k--; 
				value+=1;
			} a-- a k-- k value+=1 value 1 if (b != 0) {
				b--; k--; ;
			} else if (c != 0) {
				c--; k--; value-=1;
			} else {
				break;
			} b != 0 b 0 {
				b--; k--; ;
			} b-- b k-- k if (c != 0) {
				c--; k--; value-=1;
			} else {
				break;
			} c != 0 c 0 {
				c--; k--; value-=1;
			} c-- c k-- k value-=1 value 1 {
				break;
			} break; System.out.println(value) System.out.println System.out System System.out value String[] args args public class Main {
	public static void main (String[] args) {
		Scanner sc = new Scanner(System.in);
		int a = sc.nextInt();	
		int b = sc.nextInt();
		int c = sc.nextInt();
		int k = sc.nextInt();
		int value = 0;
		while (k > 0) {
			if (a!=0) {
				a--;
				k--; 
				value+=1;
			} else if (b != 0) {
				b--; k--; ;
			} else if (c != 0) {
				c--; k--; value-=1;
			} else {
				break;
			}
		}

		System.out.println(value);
	}
} public class Main {
	public static void main (String[] args) {
		Scanner sc = new Scanner(System.in);
		int a = sc.nextInt();	
		int b = sc.nextInt();
		int c = sc.nextInt();
		int k = sc.nextInt();
		int value = 0;
		while (k > 0) {
			if (a!=0) {
				a--;
				k--; 
				value+=1;
			} else if (b != 0) {
				b--; k--; ;
			} else if (c != 0) {
				c--; k--; value-=1;
			} else {
				break;
			}
		}

		System.out.println(value);
	}
} Main
import java.util.*;
import java.io.*;
import java.lang.*;

public class Main {
	public static void main (String[] args) {
		Scanner sc = new Scanner(System.in);
		int a = sc.nextInt();	
		int b = sc.nextInt();
		int c = sc.nextInt();
		int k = sc.nextInt();
		int value = 0;
		while (k > 0) {
			if (a!=0) {
				a--;
				k--; 
				value+=1;
			} else if (b != 0) {
				b--; k--; ;
			} else if (c != 0) {
				c--; k--; value-=1;
			} else {
				break;
			}
		}

		System.out.println(value);
	}
}
