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