import java.util.*;
import java.lang.*;
 
public class Main
{
	public static void main (String[] args) throws java.lang.Exception
	{
		Scanner sc=new Scanner(System.in);
		int a,b,c,k;
		a=sc.nextInt();b=sc.nextInt();c=sc.nextInt();k=sc.nextInt();
		if(k<=a)
		{
		 System.out.println(k);   		
		}
	  else if(k>a && k<(a+b))
		{
		 System.out.println(a);   
		}
		else{
		    System.out.println(a-(k-a-b));
		} 
	}
}
		 import java.util.*; util.* java import java.lang.*; lang.* java public class Main
{
	public static void main (String[] args) throws java.lang.Exception
	{
		Scanner sc=new Scanner(System.in);
		int a,b,c,k;
		a=sc.nextInt();b=sc.nextInt();c=sc.nextInt();k=sc.nextInt();
		if(k<=a)
		{
		 System.out.println(k);   		
		}
	  else if(k>a && k<(a+b))
		{
		 System.out.println(a);   
		}
		else{
		    System.out.println(a-(k-a-b));
		} 
	}
} Main public static void main (String[] args) throws java.lang.Exception
	{
		Scanner sc=new Scanner(System.in);
		int a,b,c,k;
		a=sc.nextInt();b=sc.nextInt();c=sc.nextInt();k=sc.nextInt();
		if(k<=a)
		{
		 System.out.println(k);   		
		}
	  else if(k>a && k<(a+b))
		{
		 System.out.println(a);   
		}
		else{
		    System.out.println(a-(k-a-b));
		} 
	} main {
		Scanner sc=new Scanner(System.in);
		int a,b,c,k;
		a=sc.nextInt();b=sc.nextInt();c=sc.nextInt();k=sc.nextInt();
		if(k<=a)
		{
		 System.out.println(k);   		
		}
	  else if(k>a && k<(a+b))
		{
		 System.out.println(a);   
		}
		else{
		    System.out.println(a-(k-a-b));
		} 
	} Scanner sc=new Scanner(System.in); sc new Scanner(System.in) int a a b b c c k; k a=sc.nextInt() a sc.nextInt() sc.nextInt sc b=sc.nextInt() b sc.nextInt() sc.nextInt sc c=sc.nextInt() c sc.nextInt() sc.nextInt sc k=sc.nextInt() k sc.nextInt() sc.nextInt sc if(k<=a)
		{
		 System.out.println(k);   		
		}
	  else if(k>a && k<(a+b))
		{
		 System.out.println(a);   
		}
		else{
		    System.out.println(a-(k-a-b));
		} k<=a k a {
		 System.out.println(k);   		
		} System.out.println(k) System.out.println System.out System System.out k if(k>a && k<(a+b))
		{
		 System.out.println(a);   
		}
		else{
		    System.out.println(a-(k-a-b));
		} k>a && k<(a+b) k>a k a k<(a+b) k (a+b) a b {
		 System.out.println(a);   
		} System.out.println(a) System.out.println System.out System System.out a {
		    System.out.println(a-(k-a-b));
		} System.out.println(a-(k-a-b)) System.out.println System.out System System.out a-(k-a-b) a (k-a-b) k-a-b k a b String[] args args public class Main
{
	public static void main (String[] args) throws java.lang.Exception
	{
		Scanner sc=new Scanner(System.in);
		int a,b,c,k;
		a=sc.nextInt();b=sc.nextInt();c=sc.nextInt();k=sc.nextInt();
		if(k<=a)
		{
		 System.out.println(k);   		
		}
	  else if(k>a && k<(a+b))
		{
		 System.out.println(a);   
		}
		else{
		    System.out.println(a-(k-a-b));
		} 
	}
} public class Main
{
	public static void main (String[] args) throws java.lang.Exception
	{
		Scanner sc=new Scanner(System.in);
		int a,b,c,k;
		a=sc.nextInt();b=sc.nextInt();c=sc.nextInt();k=sc.nextInt();
		if(k<=a)
		{
		 System.out.println(k);   		
		}
	  else if(k>a && k<(a+b))
		{
		 System.out.println(a);   
		}
		else{
		    System.out.println(a-(k-a-b));
		} 
	}
} Main
import java.util.*;
import java.lang.*;
 
public class Main
{
	public static void main (String[] args) throws java.lang.Exception
	{
		Scanner sc=new Scanner(System.in);
		int a,b,c,k;
		a=sc.nextInt();b=sc.nextInt();c=sc.nextInt();k=sc.nextInt();
		if(k<=a)
		{
		 System.out.println(k);   		
		}
	  else if(k>a && k<(a+b))
		{
		 System.out.println(a);   
		}
		else{
		    System.out.println(a-(k-a-b));
		} 
	}
}
		