import java.util.*;
import java.lang.*;
import java.io.*;
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 i=0;
	   int sum=0;
	   if(k>=a)
	  { sum+=a;
		  k=k-a;
	  }
	  else
	    {sum=k;
			k=0;
		}
		if(k>=b)
		k=k-b;
		else k=0;
		if(k>=c)
		sum=sum-c;
	    else sum=sum-k;
	   
	System.out.println(sum);
   
   }
	   
	 }
 import java.util.*; util.* java import java.lang.*; lang.* java import java.io.*; io.* java 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 i=0;
	   int sum=0;
	   if(k>=a)
	  { sum+=a;
		  k=k-a;
	  }
	  else
	    {sum=k;
			k=0;
		}
		if(k>=b)
		k=k-b;
		else k=0;
		if(k>=c)
		sum=sum-c;
	    else sum=sum-k;
	   
	System.out.println(sum);
   
   }
	   
	 } 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 i=0;
	   int sum=0;
	   if(k>=a)
	  { sum+=a;
		  k=k-a;
	  }
	  else
	    {sum=k;
			k=0;
		}
		if(k>=b)
		k=k-b;
		else k=0;
		if(k>=c)
		sum=sum-c;
	    else sum=sum-k;
	   
	System.out.println(sum);
   
   } main {
Scanner scan=new Scanner(System.in);

	    
	    int a=scan.nextInt();
	    int b=scan.nextInt();
	    int c=scan.nextInt();
	   int k=scan.nextInt();
	   int i=0;
	   int sum=0;
	   if(k>=a)
	  { sum+=a;
		  k=k-a;
	  }
	  else
	    {sum=k;
			k=0;
		}
		if(k>=b)
		k=k-b;
		else k=0;
		if(k>=c)
		sum=sum-c;
	    else sum=sum-k;
	   
	System.out.println(sum);
   
   } 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 i=0; i 0 int sum=0; sum 0 if(k>=a)
	  { sum+=a;
		  k=k-a;
	  }
	  else
	    {sum=k;
			k=0;
		} k>=a k a { sum+=a;
		  k=k-a;
	  } sum+=a sum a k=k-a k k-a k a {sum=k;
			k=0;
		} sum=k sum k k=0 k 0 if(k>=b)
		k=k-b;
		else k=0; k>=b k b k=k-b k k-b k b k=0 k 0 if(k>=c)
		sum=sum-c;
	    else sum=sum-k; k>=c k c sum=sum-c sum sum-c sum c sum=sum-k sum sum-k sum k 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)
	 {
Scanner scan=new Scanner(System.in);

	    
	    int a=scan.nextInt();
	    int b=scan.nextInt();
	    int c=scan.nextInt();
	   int k=scan.nextInt();
	   int i=0;
	   int sum=0;
	   if(k>=a)
	  { sum+=a;
		  k=k-a;
	  }
	  else
	    {sum=k;
			k=0;
		}
		if(k>=b)
		k=k-b;
		else k=0;
		if(k>=c)
		sum=sum-c;
	    else sum=sum-k;
	   
	System.out.println(sum);
   
   }
	   
	 } 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 i=0;
	   int sum=0;
	   if(k>=a)
	  { sum+=a;
		  k=k-a;
	  }
	  else
	    {sum=k;
			k=0;
		}
		if(k>=b)
		k=k-b;
		else k=0;
		if(k>=c)
		sum=sum-c;
	    else sum=sum-k;
	   
	System.out.println(sum);
   
   }
	   
	 } Main
import java.util.*;
import java.lang.*;
import java.io.*;
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 i=0;
	   int sum=0;
	   if(k>=a)
	  { sum+=a;
		  k=k-a;
	  }
	  else
	    {sum=k;
			k=0;
		}
		if(k>=b)
		k=k-b;
		else k=0;
		if(k>=c)
		sum=sum-c;
	    else sum=sum-k;
	   
	System.out.println(sum);
   
   }
	   
	 }

