import java.util.Scanner;

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 result = 0;
	        if(k<a) {
	        	result = k;
	        }else if(k>=a && a+b>=k) {
	        	result = a;
	        }else  {
	        	int d = k-a-b;
	        	result = a-d;
	        }
	        System.out.println(result); 
	        
            

          
	        
            //System.out.println(res); 
	        
	}
} import java.util.Scanner; Scanner java.util 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 result = 0;
	        if(k<a) {
	        	result = k;
	        }else if(k>=a && a+b>=k) {
	        	result = a;
	        }else  {
	        	int d = k-a-b;
	        	result = a-d;
	        }
	        System.out.println(result); 
	        
            

          
	        
            //System.out.println(res); 
	        
	}
} 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 result = 0;
	        if(k<a) {
	        	result = k;
	        }else if(k>=a && a+b>=k) {
	        	result = a;
	        }else  {
	        	int d = k-a-b;
	        	result = a-d;
	        }
	        System.out.println(result); 
	        
            

          
	        
            //System.out.println(res); 
	        
	} main {
	        Scanner sc = new Scanner(System.in);
	        int a = sc.nextInt();
	        int b = sc.nextInt();
	        int c = sc.nextInt();
	        int k = sc.nextInt();
	        
	        int result = 0;
	        if(k<a) {
	        	result = k;
	        }else if(k>=a && a+b>=k) {
	        	result = a;
	        }else  {
	        	int d = k-a-b;
	        	result = a-d;
	        }
	        System.out.println(result); 
	        
            

          
	        
            //System.out.println(res); 
	        
	} 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 result = 0; result 0 if(k<a) {
	        	result = k;
	        }else if(k>=a && a+b>=k) {
	        	result = a;
	        }else  {
	        	int d = k-a-b;
	        	result = a-d;
	        } k<a k a {
	        	result = k;
	        } result = k result k if(k>=a && a+b>=k) {
	        	result = a;
	        }else  {
	        	int d = k-a-b;
	        	result = a-d;
	        } k>=a && a+b>=k k>=a k a a+b>=k a+b a b k {
	        	result = a;
	        } result = a result a {
	        	int d = k-a-b;
	        	result = a-d;
	        } int d = k-a-b; d k-a-b k-a-b k a b result = a-d result a-d a d System.out.println(result) System.out.println System.out System System.out result 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 result = 0;
	        if(k<a) {
	        	result = k;
	        }else if(k>=a && a+b>=k) {
	        	result = a;
	        }else  {
	        	int d = k-a-b;
	        	result = a-d;
	        }
	        System.out.println(result); 
	        
            

          
	        
            //System.out.println(res); 
	        
	}
} 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 result = 0;
	        if(k<a) {
	        	result = k;
	        }else if(k>=a && a+b>=k) {
	        	result = a;
	        }else  {
	        	int d = k-a-b;
	        	result = a-d;
	        }
	        System.out.println(result); 
	        
            

          
	        
            //System.out.println(res); 
	        
	}
} Main
import java.util.Scanner;

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 result = 0;
	        if(k<a) {
	        	result = k;
	        }else if(k>=a && a+b>=k) {
	        	result = a;
	        }else  {
	        	int d = k-a-b;
	        	result = a-d;
	        }
	        System.out.println(result); 
	        
            

          
	        
            //System.out.println(res); 
	        
	}
}