import java.io.*;
import java.util.*;
import static java.lang.System.*;
import static java.lang.Math.*;

public class Main
{	
	public static void main(String []args)throws IOException
	{
		BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
		
		//long X=Long.parseLong(br.readLine());
		String []s=br.readLine().split(" ");
		long A=Long.parseLong(s[0]);
		long B=Long.parseLong(s[1]);
		long C=Long.parseLong(s[2]);
		long K=Long.parseLong(s[3]);
		
		long sum=0;
		boolean flag=true;
		
		if(K>A && flag==true)
		{
			K-=A;
			sum+=A;
			//out.println("if A K="+ K+" sum="+sum);
		}
		
		else if(K<=A && flag==true)
		{
			sum+=K;
			K=0;
			flag=false;
			//out.println("elif A K="+ K+" sum="+sum);
		}
		
		if(K>B &&  flag==true)
		{
			K-=B;
			//out.println("if B K="+ K+" sum="+sum);
		}
			
		else if(K<=B && flag==true)
		{
			K=0;
			flag=false;
			//out.println("elif B K="+ K+" sum="+sum);		
		}
		
		if(K>C && flag==true)
		{
			K-=C;
			sum-=C;
			//out.println("if C K="+ K+" sum="+sum);		
		}
		else if(K<=C && flag==true)
		{
			sum-=K;
			K=0;
			flag=false;
			//out.println("elif C K="+ K+" sum="+sum);		
		}
		
		
		//ans = flag==true? "Yes" : "No";
			
		out.println(sum);
		
	}
	
	
}

 import java.io.*; io.* java import java.util.*; util.* java import static java.lang.System.*; import static java.lang.System.*; import static java.lang.System.*; import static java.lang.Math.*; import static java.lang.Math.*; import static java.lang.Math.*; public class Main
{	
	public static void main(String []args)throws IOException
	{
		BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
		
		//long X=Long.parseLong(br.readLine());
		String []s=br.readLine().split(" ");
		long A=Long.parseLong(s[0]);
		long B=Long.parseLong(s[1]);
		long C=Long.parseLong(s[2]);
		long K=Long.parseLong(s[3]);
		
		long sum=0;
		boolean flag=true;
		
		if(K>A && flag==true)
		{
			K-=A;
			sum+=A;
			//out.println("if A K="+ K+" sum="+sum);
		}
		
		else if(K<=A && flag==true)
		{
			sum+=K;
			K=0;
			flag=false;
			//out.println("elif A K="+ K+" sum="+sum);
		}
		
		if(K>B &&  flag==true)
		{
			K-=B;
			//out.println("if B K="+ K+" sum="+sum);
		}
			
		else if(K<=B && flag==true)
		{
			K=0;
			flag=false;
			//out.println("elif B K="+ K+" sum="+sum);		
		}
		
		if(K>C && flag==true)
		{
			K-=C;
			sum-=C;
			//out.println("if C K="+ K+" sum="+sum);		
		}
		else if(K<=C && flag==true)
		{
			sum-=K;
			K=0;
			flag=false;
			//out.println("elif C K="+ K+" sum="+sum);		
		}
		
		
		//ans = flag==true? "Yes" : "No";
			
		out.println(sum);
		
	}
	
	
} Main public static void main(String []args)throws IOException
	{
		BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
		
		//long X=Long.parseLong(br.readLine());
		String []s=br.readLine().split(" ");
		long A=Long.parseLong(s[0]);
		long B=Long.parseLong(s[1]);
		long C=Long.parseLong(s[2]);
		long K=Long.parseLong(s[3]);
		
		long sum=0;
		boolean flag=true;
		
		if(K>A && flag==true)
		{
			K-=A;
			sum+=A;
			//out.println("if A K="+ K+" sum="+sum);
		}
		
		else if(K<=A && flag==true)
		{
			sum+=K;
			K=0;
			flag=false;
			//out.println("elif A K="+ K+" sum="+sum);
		}
		
		if(K>B &&  flag==true)
		{
			K-=B;
			//out.println("if B K="+ K+" sum="+sum);
		}
			
		else if(K<=B && flag==true)
		{
			K=0;
			flag=false;
			//out.println("elif B K="+ K+" sum="+sum);		
		}
		
		if(K>C && flag==true)
		{
			K-=C;
			sum-=C;
			//out.println("if C K="+ K+" sum="+sum);		
		}
		else if(K<=C && flag==true)
		{
			sum-=K;
			K=0;
			flag=false;
			//out.println("elif C K="+ K+" sum="+sum);		
		}
		
		
		//ans = flag==true? "Yes" : "No";
			
		out.println(sum);
		
	} main {
		BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
		
		//long X=Long.parseLong(br.readLine());
		String []s=br.readLine().split(" ");
		long A=Long.parseLong(s[0]);
		long B=Long.parseLong(s[1]);
		long C=Long.parseLong(s[2]);
		long K=Long.parseLong(s[3]);
		
		long sum=0;
		boolean flag=true;
		
		if(K>A && flag==true)
		{
			K-=A;
			sum+=A;
			//out.println("if A K="+ K+" sum="+sum);
		}
		
		else if(K<=A && flag==true)
		{
			sum+=K;
			K=0;
			flag=false;
			//out.println("elif A K="+ K+" sum="+sum);
		}
		
		if(K>B &&  flag==true)
		{
			K-=B;
			//out.println("if B K="+ K+" sum="+sum);
		}
			
		else if(K<=B && flag==true)
		{
			K=0;
			flag=false;
			//out.println("elif B K="+ K+" sum="+sum);		
		}
		
		if(K>C && flag==true)
		{
			K-=C;
			sum-=C;
			//out.println("if C K="+ K+" sum="+sum);		
		}
		else if(K<=C && flag==true)
		{
			sum-=K;
			K=0;
			flag=false;
			//out.println("elif C K="+ K+" sum="+sum);		
		}
		
		
		//ans = flag==true? "Yes" : "No";
			
		out.println(sum);
		
	} BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); br new BufferedReader(new InputStreamReader(System.in)) String []s=br.readLine().split(" "); s br.readLine().split(" ") br.readLine().split br.readLine() br.readLine br " " long A=Long.parseLong(s[0]); A Long.parseLong(s[0]) Long.parseLong Long s[0] s 0 long B=Long.parseLong(s[1]); B Long.parseLong(s[1]) Long.parseLong Long s[1] s 1 long C=Long.parseLong(s[2]); C Long.parseLong(s[2]) Long.parseLong Long s[2] s 2 long K=Long.parseLong(s[3]); K Long.parseLong(s[3]) Long.parseLong Long s[3] s 3 long sum=0; sum 0 boolean flag=true; flag true if(K>A && flag==true)
		{
			K-=A;
			sum+=A;
			//out.println("if A K="+ K+" sum="+sum);
		}
		
		else if(K<=A && flag==true)
		{
			sum+=K;
			K=0;
			flag=false;
			//out.println("elif A K="+ K+" sum="+sum);
		} K>A && flag==true K>A K A flag==true flag true {
			K-=A;
			sum+=A;
			//out.println("if A K="+ K+" sum="+sum);
		} K-=A K A sum+=A sum A if(K<=A && flag==true)
		{
			sum+=K;
			K=0;
			flag=false;
			//out.println("elif A K="+ K+" sum="+sum);
		} K<=A && flag==true K<=A K A flag==true flag true {
			sum+=K;
			K=0;
			flag=false;
			//out.println("elif A K="+ K+" sum="+sum);
		} sum+=K sum K K=0 K 0 flag=false flag false if(K>B &&  flag==true)
		{
			K-=B;
			//out.println("if B K="+ K+" sum="+sum);
		}
			
		else if(K<=B && flag==true)
		{
			K=0;
			flag=false;
			//out.println("elif B K="+ K+" sum="+sum);		
		} K>B &&  flag==true K>B K B flag==true flag true {
			K-=B;
			//out.println("if B K="+ K+" sum="+sum);
		} K-=B K B if(K<=B && flag==true)
		{
			K=0;
			flag=false;
			//out.println("elif B K="+ K+" sum="+sum);		
		} K<=B && flag==true K<=B K B flag==true flag true {
			K=0;
			flag=false;
			//out.println("elif B K="+ K+" sum="+sum);		
		} K=0 K 0 flag=false flag false if(K>C && flag==true)
		{
			K-=C;
			sum-=C;
			//out.println("if C K="+ K+" sum="+sum);		
		}
		else if(K<=C && flag==true)
		{
			sum-=K;
			K=0;
			flag=false;
			//out.println("elif C K="+ K+" sum="+sum);		
		} K>C && flag==true K>C K C flag==true flag true {
			K-=C;
			sum-=C;
			//out.println("if C K="+ K+" sum="+sum);		
		} K-=C K C sum-=C sum C if(K<=C && flag==true)
		{
			sum-=K;
			K=0;
			flag=false;
			//out.println("elif C K="+ K+" sum="+sum);		
		} K<=C && flag==true K<=C K C flag==true flag true {
			sum-=K;
			K=0;
			flag=false;
			//out.println("elif C K="+ K+" sum="+sum);		
		} sum-=K sum K K=0 K 0 flag=false flag false out.println(sum) out.println out sum String []args args public class Main
{	
	public static void main(String []args)throws IOException
	{
		BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
		
		//long X=Long.parseLong(br.readLine());
		String []s=br.readLine().split(" ");
		long A=Long.parseLong(s[0]);
		long B=Long.parseLong(s[1]);
		long C=Long.parseLong(s[2]);
		long K=Long.parseLong(s[3]);
		
		long sum=0;
		boolean flag=true;
		
		if(K>A && flag==true)
		{
			K-=A;
			sum+=A;
			//out.println("if A K="+ K+" sum="+sum);
		}
		
		else if(K<=A && flag==true)
		{
			sum+=K;
			K=0;
			flag=false;
			//out.println("elif A K="+ K+" sum="+sum);
		}
		
		if(K>B &&  flag==true)
		{
			K-=B;
			//out.println("if B K="+ K+" sum="+sum);
		}
			
		else if(K<=B && flag==true)
		{
			K=0;
			flag=false;
			//out.println("elif B K="+ K+" sum="+sum);		
		}
		
		if(K>C && flag==true)
		{
			K-=C;
			sum-=C;
			//out.println("if C K="+ K+" sum="+sum);		
		}
		else if(K<=C && flag==true)
		{
			sum-=K;
			K=0;
			flag=false;
			//out.println("elif C K="+ K+" sum="+sum);		
		}
		
		
		//ans = flag==true? "Yes" : "No";
			
		out.println(sum);
		
	}
	
	
} public class Main
{	
	public static void main(String []args)throws IOException
	{
		BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
		
		//long X=Long.parseLong(br.readLine());
		String []s=br.readLine().split(" ");
		long A=Long.parseLong(s[0]);
		long B=Long.parseLong(s[1]);
		long C=Long.parseLong(s[2]);
		long K=Long.parseLong(s[3]);
		
		long sum=0;
		boolean flag=true;
		
		if(K>A && flag==true)
		{
			K-=A;
			sum+=A;
			//out.println("if A K="+ K+" sum="+sum);
		}
		
		else if(K<=A && flag==true)
		{
			sum+=K;
			K=0;
			flag=false;
			//out.println("elif A K="+ K+" sum="+sum);
		}
		
		if(K>B &&  flag==true)
		{
			K-=B;
			//out.println("if B K="+ K+" sum="+sum);
		}
			
		else if(K<=B && flag==true)
		{
			K=0;
			flag=false;
			//out.println("elif B K="+ K+" sum="+sum);		
		}
		
		if(K>C && flag==true)
		{
			K-=C;
			sum-=C;
			//out.println("if C K="+ K+" sum="+sum);		
		}
		else if(K<=C && flag==true)
		{
			sum-=K;
			K=0;
			flag=false;
			//out.println("elif C K="+ K+" sum="+sum);		
		}
		
		
		//ans = flag==true? "Yes" : "No";
			
		out.println(sum);
		
	}
	
	
} Main
import java.io.*;
import java.util.*;
import static java.lang.System.*;
import static java.lang.Math.*;

public class Main
{	
	public static void main(String []args)throws IOException
	{
		BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
		
		//long X=Long.parseLong(br.readLine());
		String []s=br.readLine().split(" ");
		long A=Long.parseLong(s[0]);
		long B=Long.parseLong(s[1]);
		long C=Long.parseLong(s[2]);
		long K=Long.parseLong(s[3]);
		
		long sum=0;
		boolean flag=true;
		
		if(K>A && flag==true)
		{
			K-=A;
			sum+=A;
			//out.println("if A K="+ K+" sum="+sum);
		}
		
		else if(K<=A && flag==true)
		{
			sum+=K;
			K=0;
			flag=false;
			//out.println("elif A K="+ K+" sum="+sum);
		}
		
		if(K>B &&  flag==true)
		{
			K-=B;
			//out.println("if B K="+ K+" sum="+sum);
		}
			
		else if(K<=B && flag==true)
		{
			K=0;
			flag=false;
			//out.println("elif B K="+ K+" sum="+sum);		
		}
		
		if(K>C && flag==true)
		{
			K-=C;
			sum-=C;
			//out.println("if C K="+ K+" sum="+sum);		
		}
		else if(K<=C && flag==true)
		{
			sum-=K;
			K=0;
			flag=false;
			//out.println("elif C K="+ K+" sum="+sum);		
		}
		
		
		//ans = flag==true? "Yes" : "No";
			
		out.println(sum);
		
	}
	
	
}


