import java.util.*;

public class Main {
public static void main(String[] args) {
		
		Scanner keyboard = new Scanner(System.in); 
		
		int A= keyboard.nextInt();
		int B= keyboard.nextInt();
		int C= keyboard.nextInt();
		int K= keyboard.nextInt();
		
		int ans =0;
		
		if(A<=K){
			ans = A;
			K -= A;
			if(K!=0 && B<=K){
				//ans += 0;
				K -= B;
				ans -=K;
			}
		}else{
			ans = K;
		}

		System.out.print(ans);
		keyboard.close();	
		
	}
} import java.util.*; util.* java public class Main {
public static void main(String[] args) {
		
		Scanner keyboard = new Scanner(System.in); 
		
		int A= keyboard.nextInt();
		int B= keyboard.nextInt();
		int C= keyboard.nextInt();
		int K= keyboard.nextInt();
		
		int ans =0;
		
		if(A<=K){
			ans = A;
			K -= A;
			if(K!=0 && B<=K){
				//ans += 0;
				K -= B;
				ans -=K;
			}
		}else{
			ans = K;
		}

		System.out.print(ans);
		keyboard.close();	
		
	}
} Main public static void main(String[] args) {
		
		Scanner keyboard = new Scanner(System.in); 
		
		int A= keyboard.nextInt();
		int B= keyboard.nextInt();
		int C= keyboard.nextInt();
		int K= keyboard.nextInt();
		
		int ans =0;
		
		if(A<=K){
			ans = A;
			K -= A;
			if(K!=0 && B<=K){
				//ans += 0;
				K -= B;
				ans -=K;
			}
		}else{
			ans = K;
		}

		System.out.print(ans);
		keyboard.close();	
		
	} main {
		
		Scanner keyboard = new Scanner(System.in); 
		
		int A= keyboard.nextInt();
		int B= keyboard.nextInt();
		int C= keyboard.nextInt();
		int K= keyboard.nextInt();
		
		int ans =0;
		
		if(A<=K){
			ans = A;
			K -= A;
			if(K!=0 && B<=K){
				//ans += 0;
				K -= B;
				ans -=K;
			}
		}else{
			ans = K;
		}

		System.out.print(ans);
		keyboard.close();	
		
	} Scanner keyboard = new Scanner(System.in); keyboard new Scanner(System.in) int A= keyboard.nextInt(); A keyboard.nextInt() keyboard.nextInt keyboard int B= keyboard.nextInt(); B keyboard.nextInt() keyboard.nextInt keyboard int C= keyboard.nextInt(); C keyboard.nextInt() keyboard.nextInt keyboard int K= keyboard.nextInt(); K keyboard.nextInt() keyboard.nextInt keyboard int ans =0; ans 0 if(A<=K){
			ans = A;
			K -= A;
			if(K!=0 && B<=K){
				//ans += 0;
				K -= B;
				ans -=K;
			}
		}else{
			ans = K;
		} A<=K A K {
			ans = A;
			K -= A;
			if(K!=0 && B<=K){
				//ans += 0;
				K -= B;
				ans -=K;
			}
		} ans = A ans A K -= A K A if(K!=0 && B<=K){
				//ans += 0;
				K -= B;
				ans -=K;
			} K!=0 && B<=K K!=0 K 0 B<=K B K {
				//ans += 0;
				K -= B;
				ans -=K;
			} K -= B K B ans -=K ans K {
			ans = K;
		} ans = K ans K System.out.print(ans) System.out.print System.out System System.out ans keyboard.close() keyboard.close keyboard String[] args args public class Main {
public static void main(String[] args) {
		
		Scanner keyboard = new Scanner(System.in); 
		
		int A= keyboard.nextInt();
		int B= keyboard.nextInt();
		int C= keyboard.nextInt();
		int K= keyboard.nextInt();
		
		int ans =0;
		
		if(A<=K){
			ans = A;
			K -= A;
			if(K!=0 && B<=K){
				//ans += 0;
				K -= B;
				ans -=K;
			}
		}else{
			ans = K;
		}

		System.out.print(ans);
		keyboard.close();	
		
	}
} public class Main {
public static void main(String[] args) {
		
		Scanner keyboard = new Scanner(System.in); 
		
		int A= keyboard.nextInt();
		int B= keyboard.nextInt();
		int C= keyboard.nextInt();
		int K= keyboard.nextInt();
		
		int ans =0;
		
		if(A<=K){
			ans = A;
			K -= A;
			if(K!=0 && B<=K){
				//ans += 0;
				K -= B;
				ans -=K;
			}
		}else{
			ans = K;
		}

		System.out.print(ans);
		keyboard.close();	
		
	}
} Main
import java.util.*;

public class Main {
public static void main(String[] args) {
		
		Scanner keyboard = new Scanner(System.in); 
		
		int A= keyboard.nextInt();
		int B= keyboard.nextInt();
		int C= keyboard.nextInt();
		int K= keyboard.nextInt();
		
		int ans =0;
		
		if(A<=K){
			ans = A;
			K -= A;
			if(K!=0 && B<=K){
				//ans += 0;
				K -= B;
				ans -=K;
			}
		}else{
			ans = K;
		}

		System.out.print(ans);
		keyboard.close();	
		
	}
}