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

public class Main{
	static PrintWriter out=new PrintWriter(System.out);
	public static void main (String[] args) throws IOException{
		BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
		
		String[] input=br.readLine().trim().split(" ");
		
		
		int a=Integer.parseInt(input[0]);
		int b=Integer.parseInt(input[1]);
		int c=Integer.parseInt(input[2]);
		int k=Integer.parseInt(input[3]);
		
		
		out.println(maxSum(a,b,c,k));
		
		out.flush();
		out.close();
	}
	public static int maxSum(int a,int b,int c,int k)
	{
	   int ans=0;
	   if(a>=k){
	       ans+=k;
	   }
	   else{
	       ans+=a;
	       k-=a;
	       if(b>=k){
	           
	       }
	       else{
	           k-=b;
	           ans-=k;
	       }
	   }
	   return ans;
	}
} import java.util.*; util.* java import java.io.*; io.* java public class Main{
	static PrintWriter out=new PrintWriter(System.out);
	public static void main (String[] args) throws IOException{
		BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
		
		String[] input=br.readLine().trim().split(" ");
		
		
		int a=Integer.parseInt(input[0]);
		int b=Integer.parseInt(input[1]);
		int c=Integer.parseInt(input[2]);
		int k=Integer.parseInt(input[3]);
		
		
		out.println(maxSum(a,b,c,k));
		
		out.flush();
		out.close();
	}
	public static int maxSum(int a,int b,int c,int k)
	{
	   int ans=0;
	   if(a>=k){
	       ans+=k;
	   }
	   else{
	       ans+=a;
	       k-=a;
	       if(b>=k){
	           
	       }
	       else{
	           k-=b;
	           ans-=k;
	       }
	   }
	   return ans;
	}
} Main static PrintWriter out=new PrintWriter(System.out); out new PrintWriter(System.out) public static void main (String[] args) throws IOException{
		BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
		
		String[] input=br.readLine().trim().split(" ");
		
		
		int a=Integer.parseInt(input[0]);
		int b=Integer.parseInt(input[1]);
		int c=Integer.parseInt(input[2]);
		int k=Integer.parseInt(input[3]);
		
		
		out.println(maxSum(a,b,c,k));
		
		out.flush();
		out.close();
	} main {
		BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
		
		String[] input=br.readLine().trim().split(" ");
		
		
		int a=Integer.parseInt(input[0]);
		int b=Integer.parseInt(input[1]);
		int c=Integer.parseInt(input[2]);
		int k=Integer.parseInt(input[3]);
		
		
		out.println(maxSum(a,b,c,k));
		
		out.flush();
		out.close();
	} BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); br new BufferedReader(new InputStreamReader(System.in)) String[] input=br.readLine().trim().split(" "); input br.readLine().trim().split(" ") br.readLine().trim().split br.readLine().trim() br.readLine().trim br.readLine() br.readLine br " " int a=Integer.parseInt(input[0]); a Integer.parseInt(input[0]) Integer.parseInt Integer input[0] input 0 int b=Integer.parseInt(input[1]); b Integer.parseInt(input[1]) Integer.parseInt Integer input[1] input 1 int c=Integer.parseInt(input[2]); c Integer.parseInt(input[2]) Integer.parseInt Integer input[2] input 2 int k=Integer.parseInt(input[3]); k Integer.parseInt(input[3]) Integer.parseInt Integer input[3] input 3 out.println(maxSum(a,b,c,k)) out.println out maxSum(a,b,c,k) maxSum a b c k out.flush() out.flush out out.close() out.close out String[] args args public static int maxSum(int a,int b,int c,int k)
	{
	   int ans=0;
	   if(a>=k){
	       ans+=k;
	   }
	   else{
	       ans+=a;
	       k-=a;
	       if(b>=k){
	           
	       }
	       else{
	           k-=b;
	           ans-=k;
	       }
	   }
	   return ans;
	} maxSum {
	   int ans=0;
	   if(a>=k){
	       ans+=k;
	   }
	   else{
	       ans+=a;
	       k-=a;
	       if(b>=k){
	           
	       }
	       else{
	           k-=b;
	           ans-=k;
	       }
	   }
	   return ans;
	} int ans=0; ans 0 if(a>=k){
	       ans+=k;
	   }
	   else{
	       ans+=a;
	       k-=a;
	       if(b>=k){
	           
	       }
	       else{
	           k-=b;
	           ans-=k;
	       }
	   } a>=k a k {
	       ans+=k;
	   } ans+=k ans k {
	       ans+=a;
	       k-=a;
	       if(b>=k){
	           
	       }
	       else{
	           k-=b;
	           ans-=k;
	       }
	   } ans+=a ans a k-=a k a if(b>=k){
	           
	       }
	       else{
	           k-=b;
	           ans-=k;
	       } b>=k b k {
	           
	       } {
	           k-=b;
	           ans-=k;
	       } k-=b k b ans-=k ans k return ans; ans int a a int b b int c c int k k public class Main{
	static PrintWriter out=new PrintWriter(System.out);
	public static void main (String[] args) throws IOException{
		BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
		
		String[] input=br.readLine().trim().split(" ");
		
		
		int a=Integer.parseInt(input[0]);
		int b=Integer.parseInt(input[1]);
		int c=Integer.parseInt(input[2]);
		int k=Integer.parseInt(input[3]);
		
		
		out.println(maxSum(a,b,c,k));
		
		out.flush();
		out.close();
	}
	public static int maxSum(int a,int b,int c,int k)
	{
	   int ans=0;
	   if(a>=k){
	       ans+=k;
	   }
	   else{
	       ans+=a;
	       k-=a;
	       if(b>=k){
	           
	       }
	       else{
	           k-=b;
	           ans-=k;
	       }
	   }
	   return ans;
	}
} public class Main{
	static PrintWriter out=new PrintWriter(System.out);
	public static void main (String[] args) throws IOException{
		BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
		
		String[] input=br.readLine().trim().split(" ");
		
		
		int a=Integer.parseInt(input[0]);
		int b=Integer.parseInt(input[1]);
		int c=Integer.parseInt(input[2]);
		int k=Integer.parseInt(input[3]);
		
		
		out.println(maxSum(a,b,c,k));
		
		out.flush();
		out.close();
	}
	public static int maxSum(int a,int b,int c,int k)
	{
	   int ans=0;
	   if(a>=k){
	       ans+=k;
	   }
	   else{
	       ans+=a;
	       k-=a;
	       if(b>=k){
	           
	       }
	       else{
	           k-=b;
	           ans-=k;
	       }
	   }
	   return ans;
	}
} Main
import java.util.*;
import java.io.*;

public class Main{
	static PrintWriter out=new PrintWriter(System.out);
	public static void main (String[] args) throws IOException{
		BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
		
		String[] input=br.readLine().trim().split(" ");
		
		
		int a=Integer.parseInt(input[0]);
		int b=Integer.parseInt(input[1]);
		int c=Integer.parseInt(input[2]);
		int k=Integer.parseInt(input[3]);
		
		
		out.println(maxSum(a,b,c,k));
		
		out.flush();
		out.close();
	}
	public static int maxSum(int a,int b,int c,int k)
	{
	   int ans=0;
	   if(a>=k){
	       ans+=k;
	   }
	   else{
	       ans+=a;
	       k-=a;
	       if(b>=k){
	           
	       }
	       else{
	           k-=b;
	           ans-=k;
	       }
	   }
	   return ans;
	}
}