import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner scanner=new Scanner(System.in);
      long a=scanner.nextInt();
      long b=scanner.nextInt();
      long c=scanner.nextInt();
      long k=scanner.nextInt();
      long sum=0;
    
    	 if(k>=a&&(k-a-b)>c) {
    		 sum=a*1;
    		 k=k-a;
    	 }
    	 else if(k<a) {
    		 sum=k;
    	 }
    	 else if((k-a)<=b) {
    		 sum=a*1;
    	 }
    	 else if((k-a-b)<=c) {
    		 sum=a+(k-a-b)*(-1);
    	 }
    	System.out.println(sum);
    	 
     
   
	}

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

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner scanner=new Scanner(System.in);
      long a=scanner.nextInt();
      long b=scanner.nextInt();
      long c=scanner.nextInt();
      long k=scanner.nextInt();
      long sum=0;
    
    	 if(k>=a&&(k-a-b)>c) {
    		 sum=a*1;
    		 k=k-a;
    	 }
    	 else if(k<a) {
    		 sum=k;
    	 }
    	 else if((k-a)<=b) {
    		 sum=a*1;
    	 }
    	 else if((k-a-b)<=c) {
    		 sum=a+(k-a-b)*(-1);
    	 }
    	System.out.println(sum);
    	 
     
   
	}

} Main public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner scanner=new Scanner(System.in);
      long a=scanner.nextInt();
      long b=scanner.nextInt();
      long c=scanner.nextInt();
      long k=scanner.nextInt();
      long sum=0;
    
    	 if(k>=a&&(k-a-b)>c) {
    		 sum=a*1;
    		 k=k-a;
    	 }
    	 else if(k<a) {
    		 sum=k;
    	 }
    	 else if((k-a)<=b) {
    		 sum=a*1;
    	 }
    	 else if((k-a-b)<=c) {
    		 sum=a+(k-a-b)*(-1);
    	 }
    	System.out.println(sum);
    	 
     
   
	} main {
		// TODO Auto-generated method stub
		Scanner scanner=new Scanner(System.in);
      long a=scanner.nextInt();
      long b=scanner.nextInt();
      long c=scanner.nextInt();
      long k=scanner.nextInt();
      long sum=0;
    
    	 if(k>=a&&(k-a-b)>c) {
    		 sum=a*1;
    		 k=k-a;
    	 }
    	 else if(k<a) {
    		 sum=k;
    	 }
    	 else if((k-a)<=b) {
    		 sum=a*1;
    	 }
    	 else if((k-a-b)<=c) {
    		 sum=a+(k-a-b)*(-1);
    	 }
    	System.out.println(sum);
    	 
     
   
	} Scanner scanner=new Scanner(System.in); scanner new Scanner(System.in) long a=scanner.nextInt(); a scanner.nextInt() scanner.nextInt scanner long b=scanner.nextInt(); b scanner.nextInt() scanner.nextInt scanner long c=scanner.nextInt(); c scanner.nextInt() scanner.nextInt scanner long k=scanner.nextInt(); k scanner.nextInt() scanner.nextInt scanner long sum=0; sum 0 if(k>=a&&(k-a-b)>c) {
    		 sum=a*1;
    		 k=k-a;
    	 }
    	 else if(k<a) {
    		 sum=k;
    	 }
    	 else if((k-a)<=b) {
    		 sum=a*1;
    	 }
    	 else if((k-a-b)<=c) {
    		 sum=a+(k-a-b)*(-1);
    	 } k>=a&&(k-a-b)>c k>=a k a (k-a-b)>c (k-a-b) k-a-b k a b c {
    		 sum=a*1;
    		 k=k-a;
    	 } sum=a*1 sum a*1 a 1 k=k-a k k-a k a if(k<a) {
    		 sum=k;
    	 }
    	 else if((k-a)<=b) {
    		 sum=a*1;
    	 }
    	 else if((k-a-b)<=c) {
    		 sum=a+(k-a-b)*(-1);
    	 } k<a k a {
    		 sum=k;
    	 } sum=k sum k if((k-a)<=b) {
    		 sum=a*1;
    	 }
    	 else if((k-a-b)<=c) {
    		 sum=a+(k-a-b)*(-1);
    	 } (k-a)<=b (k-a) k a b {
    		 sum=a*1;
    	 } sum=a*1 sum a*1 a 1 if((k-a-b)<=c) {
    		 sum=a+(k-a-b)*(-1);
    	 } (k-a-b)<=c (k-a-b) k-a-b k a b c {
    		 sum=a+(k-a-b)*(-1);
    	 } sum=a+(k-a-b)*(-1) sum a+(k-a-b)*(-1) a (k-a-b)*(-1) (k-a-b) k-a-b k a b (-1) 1 System.out.println(sum) System.out.println System.out System System.out sum String[] args args public class Main {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner scanner=new Scanner(System.in);
      long a=scanner.nextInt();
      long b=scanner.nextInt();
      long c=scanner.nextInt();
      long k=scanner.nextInt();
      long sum=0;
    
    	 if(k>=a&&(k-a-b)>c) {
    		 sum=a*1;
    		 k=k-a;
    	 }
    	 else if(k<a) {
    		 sum=k;
    	 }
    	 else if((k-a)<=b) {
    		 sum=a*1;
    	 }
    	 else if((k-a-b)<=c) {
    		 sum=a+(k-a-b)*(-1);
    	 }
    	System.out.println(sum);
    	 
     
   
	}

} public class Main {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner scanner=new Scanner(System.in);
      long a=scanner.nextInt();
      long b=scanner.nextInt();
      long c=scanner.nextInt();
      long k=scanner.nextInt();
      long sum=0;
    
    	 if(k>=a&&(k-a-b)>c) {
    		 sum=a*1;
    		 k=k-a;
    	 }
    	 else if(k<a) {
    		 sum=k;
    	 }
    	 else if((k-a)<=b) {
    		 sum=a*1;
    	 }
    	 else if((k-a-b)<=c) {
    		 sum=a+(k-a-b)*(-1);
    	 }
    	System.out.println(sum);
    	 
     
   
	}

} Main
import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner scanner=new Scanner(System.in);
      long a=scanner.nextInt();
      long b=scanner.nextInt();
      long c=scanner.nextInt();
      long k=scanner.nextInt();
      long sum=0;
    
    	 if(k>=a&&(k-a-b)>c) {
    		 sum=a*1;
    		 k=k-a;
    	 }
    	 else if(k<a) {
    		 sum=k;
    	 }
    	 else if((k-a)<=b) {
    		 sum=a*1;
    	 }
    	 else if((k-a-b)<=c) {
    		 sum=a+(k-a-b)*(-1);
    	 }
    	System.out.println(sum);
    	 
     
   
	}

}