import java.io.BufferedReader;
import java.io.InputStreamReader;

class Main{
	public static void main(String args[])throws Exception
	{
		BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
		String s=br.readLine();
		String str[]=s.split(" ");
		int a=Integer.parseInt(str[0]);
		int b=Integer.parseInt(str[1]);
		int c=Integer.parseInt(str[2]);
		int k=Integer.parseInt(str[3]);
		if(a>=k)
			System.out.println(k);
		else if((a+b)>=k)
			System.out.println(a);
		else
		{
			int temp=k-(a+b);
			System.out.println(a-temp);
		}
	}
} import java.io.BufferedReader; BufferedReader java.io import java.io.InputStreamReader; InputStreamReader java.io class Main{
	public static void main(String args[])throws Exception
	{
		BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
		String s=br.readLine();
		String str[]=s.split(" ");
		int a=Integer.parseInt(str[0]);
		int b=Integer.parseInt(str[1]);
		int c=Integer.parseInt(str[2]);
		int k=Integer.parseInt(str[3]);
		if(a>=k)
			System.out.println(k);
		else if((a+b)>=k)
			System.out.println(a);
		else
		{
			int temp=k-(a+b);
			System.out.println(a-temp);
		}
	}
} Main public static void main(String args[])throws Exception
	{
		BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
		String s=br.readLine();
		String str[]=s.split(" ");
		int a=Integer.parseInt(str[0]);
		int b=Integer.parseInt(str[1]);
		int c=Integer.parseInt(str[2]);
		int k=Integer.parseInt(str[3]);
		if(a>=k)
			System.out.println(k);
		else if((a+b)>=k)
			System.out.println(a);
		else
		{
			int temp=k-(a+b);
			System.out.println(a-temp);
		}
	} main {
		BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
		String s=br.readLine();
		String str[]=s.split(" ");
		int a=Integer.parseInt(str[0]);
		int b=Integer.parseInt(str[1]);
		int c=Integer.parseInt(str[2]);
		int k=Integer.parseInt(str[3]);
		if(a>=k)
			System.out.println(k);
		else if((a+b)>=k)
			System.out.println(a);
		else
		{
			int temp=k-(a+b);
			System.out.println(a-temp);
		}
	} BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); br new BufferedReader(new InputStreamReader(System.in)) String s=br.readLine(); s br.readLine() br.readLine br String str[]=s.split(" "); str s.split(" ") s.split s " " int a=Integer.parseInt(str[0]); a Integer.parseInt(str[0]) Integer.parseInt Integer str[0] str 0 int b=Integer.parseInt(str[1]); b Integer.parseInt(str[1]) Integer.parseInt Integer str[1] str 1 int c=Integer.parseInt(str[2]); c Integer.parseInt(str[2]) Integer.parseInt Integer str[2] str 2 int k=Integer.parseInt(str[3]); k Integer.parseInt(str[3]) Integer.parseInt Integer str[3] str 3 if(a>=k)
			System.out.println(k);
		else if((a+b)>=k)
			System.out.println(a);
		else
		{
			int temp=k-(a+b);
			System.out.println(a-temp);
		} a>=k a k System.out.println(k) System.out.println System.out System System.out k if((a+b)>=k)
			System.out.println(a);
		else
		{
			int temp=k-(a+b);
			System.out.println(a-temp);
		} (a+b)>=k (a+b) a b k System.out.println(a) System.out.println System.out System System.out a {
			int temp=k-(a+b);
			System.out.println(a-temp);
		} int temp=k-(a+b); temp k-(a+b) k (a+b) a b System.out.println(a-temp) System.out.println System.out System System.out a-temp a temp String args[] args
import java.io.BufferedReader;
import java.io.InputStreamReader;

class Main{
	public static void main(String args[])throws Exception
	{
		BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
		String s=br.readLine();
		String str[]=s.split(" ");
		int a=Integer.parseInt(str[0]);
		int b=Integer.parseInt(str[1]);
		int c=Integer.parseInt(str[2]);
		int k=Integer.parseInt(str[3]);
		if(a>=k)
			System.out.println(k);
		else if((a+b)>=k)
			System.out.println(a);
		else
		{
			int temp=k-(a+b);
			System.out.println(a-temp);
		}
	}
}
