import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner scr =new Scanner(System.in);
	
		int A = scr.nextInt();
		int B = scr.nextInt();
		int c = scr.nextInt();
		int K = scr.nextInt();
		
		int X = K-A;//A���Ƃ��āC���ƂƂ�K�v�̂��閇��
		int Y = K-X;//A�̖���
		
		int Z = X-B;//
		
		int max;
	
		if(X < 0) {
			System.out.println(K);
		}else {
			if(X-B < 0) {
				System.out.println(Y);
			}else {
				max = (Z*-1)+(Y);
				System.out.println(max);
			}
		}
		
		
	
	}
} import java.util.Scanner; Scanner java.util public class Main {
	public static void main(String[] args) {
		Scanner scr =new Scanner(System.in);
	
		int A = scr.nextInt();
		int B = scr.nextInt();
		int c = scr.nextInt();
		int K = scr.nextInt();
		
		int X = K-A;//A���Ƃ��āC���ƂƂ�K�v�̂��閇��
		int Y = K-X;//A�̖���
		
		int Z = X-B;//
		
		int max;
	
		if(X < 0) {
			System.out.println(K);
		}else {
			if(X-B < 0) {
				System.out.println(Y);
			}else {
				max = (Z*-1)+(Y);
				System.out.println(max);
			}
		}
		
		
	
	}
} Main public static void main(String[] args) {
		Scanner scr =new Scanner(System.in);
	
		int A = scr.nextInt();
		int B = scr.nextInt();
		int c = scr.nextInt();
		int K = scr.nextInt();
		
		int X = K-A;//A���Ƃ��āC���ƂƂ�K�v�̂��閇��
		int Y = K-X;//A�̖���
		
		int Z = X-B;//
		
		int max;
	
		if(X < 0) {
			System.out.println(K);
		}else {
			if(X-B < 0) {
				System.out.println(Y);
			}else {
				max = (Z*-1)+(Y);
				System.out.println(max);
			}
		}
		
		
	
	} main {
		Scanner scr =new Scanner(System.in);
	
		int A = scr.nextInt();
		int B = scr.nextInt();
		int c = scr.nextInt();
		int K = scr.nextInt();
		
		int X = K-A;//A���Ƃ��āC���ƂƂ�K�v�̂��閇��
		int Y = K-X;//A�̖���
		
		int Z = X-B;//
		
		int max;
	
		if(X < 0) {
			System.out.println(K);
		}else {
			if(X-B < 0) {
				System.out.println(Y);
			}else {
				max = (Z*-1)+(Y);
				System.out.println(max);
			}
		}
		
		
	
	} Scanner scr =new Scanner(System.in); scr new Scanner(System.in) int A = scr.nextInt(); A scr.nextInt() scr.nextInt scr int B = scr.nextInt(); B scr.nextInt() scr.nextInt scr int c = scr.nextInt(); c scr.nextInt() scr.nextInt scr int K = scr.nextInt(); K scr.nextInt() scr.nextInt scr int X = K-A; X K-A K A int Y = K-X; Y K-X K X int Z = X-B; Z X-B X B int max; max if(X < 0) {
			System.out.println(K);
		}else {
			if(X-B < 0) {
				System.out.println(Y);
			}else {
				max = (Z*-1)+(Y);
				System.out.println(max);
			}
		} X < 0 X 0 {
			System.out.println(K);
		} System.out.println(K) System.out.println System.out System System.out K {
			if(X-B < 0) {
				System.out.println(Y);
			}else {
				max = (Z*-1)+(Y);
				System.out.println(max);
			}
		} if(X-B < 0) {
				System.out.println(Y);
			}else {
				max = (Z*-1)+(Y);
				System.out.println(max);
			} X-B < 0 X-B X B 0 {
				System.out.println(Y);
			} System.out.println(Y) System.out.println System.out System System.out Y {
				max = (Z*-1)+(Y);
				System.out.println(max);
			} max = (Z*-1)+(Y) max (Z*-1)+(Y) (Z*-1) Z -1 1 (Y) System.out.println(max) System.out.println System.out System System.out max String[] args args public class Main {
	public static void main(String[] args) {
		Scanner scr =new Scanner(System.in);
	
		int A = scr.nextInt();
		int B = scr.nextInt();
		int c = scr.nextInt();
		int K = scr.nextInt();
		
		int X = K-A;//A���Ƃ��āC���ƂƂ�K�v�̂��閇��
		int Y = K-X;//A�̖���
		
		int Z = X-B;//
		
		int max;
	
		if(X < 0) {
			System.out.println(K);
		}else {
			if(X-B < 0) {
				System.out.println(Y);
			}else {
				max = (Z*-1)+(Y);
				System.out.println(max);
			}
		}
		
		
	
	}
} public class Main {
	public static void main(String[] args) {
		Scanner scr =new Scanner(System.in);
	
		int A = scr.nextInt();
		int B = scr.nextInt();
		int c = scr.nextInt();
		int K = scr.nextInt();
		
		int X = K-A;//A���Ƃ��āC���ƂƂ�K�v�̂��閇��
		int Y = K-X;//A�̖���
		
		int Z = X-B;//
		
		int max;
	
		if(X < 0) {
			System.out.println(K);
		}else {
			if(X-B < 0) {
				System.out.println(Y);
			}else {
				max = (Z*-1)+(Y);
				System.out.println(max);
			}
		}
		
		
	
	}
} Main
import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner scr =new Scanner(System.in);
	
		int A = scr.nextInt();
		int B = scr.nextInt();
		int c = scr.nextInt();
		int K = scr.nextInt();
		
		int X = K-A;//A���Ƃ��āC���ƂƂ�K�v�̂��閇��
		int Y = K-X;//A�̖���
		
		int Z = X-B;//
		
		int max;
	
		if(X < 0) {
			System.out.println(K);
		}else {
			if(X-B < 0) {
				System.out.println(Y);
			}else {
				max = (Z*-1)+(Y);
				System.out.println(max);
			}
		}
		
		
	
	}
}