//package atcoder;
 
import java.util.*;
 
public class Main{
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		
		long A = sc.nextLong();
		long B = sc.nextLong();
		long C = sc.nextLong();
		long K = sc.nextLong();
		
		if(A>=K) {
			System.out.println(K);
		}
		if(A<K) {
			if((K-A)<=B) {
				System.out.println(A);
			}
			if((K-A)>B) {
				System.out.println(A-(K-(A+B)));
			}
		}
	}
} import java.util.*; util.* java public class Main{
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		
		long A = sc.nextLong();
		long B = sc.nextLong();
		long C = sc.nextLong();
		long K = sc.nextLong();
		
		if(A>=K) {
			System.out.println(K);
		}
		if(A<K) {
			if((K-A)<=B) {
				System.out.println(A);
			}
			if((K-A)>B) {
				System.out.println(A-(K-(A+B)));
			}
		}
	}
} Main public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		
		long A = sc.nextLong();
		long B = sc.nextLong();
		long C = sc.nextLong();
		long K = sc.nextLong();
		
		if(A>=K) {
			System.out.println(K);
		}
		if(A<K) {
			if((K-A)<=B) {
				System.out.println(A);
			}
			if((K-A)>B) {
				System.out.println(A-(K-(A+B)));
			}
		}
	} main {
		Scanner sc = new Scanner(System.in);
		
		long A = sc.nextLong();
		long B = sc.nextLong();
		long C = sc.nextLong();
		long K = sc.nextLong();
		
		if(A>=K) {
			System.out.println(K);
		}
		if(A<K) {
			if((K-A)<=B) {
				System.out.println(A);
			}
			if((K-A)>B) {
				System.out.println(A-(K-(A+B)));
			}
		}
	} Scanner sc = new Scanner(System.in); sc new Scanner(System.in) long A = sc.nextLong(); A sc.nextLong() sc.nextLong sc long B = sc.nextLong(); B sc.nextLong() sc.nextLong sc long C = sc.nextLong(); C sc.nextLong() sc.nextLong sc long K = sc.nextLong(); K sc.nextLong() sc.nextLong sc if(A>=K) {
			System.out.println(K);
		} A>=K A K {
			System.out.println(K);
		} System.out.println(K) System.out.println System.out System System.out K if(A<K) {
			if((K-A)<=B) {
				System.out.println(A);
			}
			if((K-A)>B) {
				System.out.println(A-(K-(A+B)));
			}
		} A<K A K {
			if((K-A)<=B) {
				System.out.println(A);
			}
			if((K-A)>B) {
				System.out.println(A-(K-(A+B)));
			}
		} if((K-A)<=B) {
				System.out.println(A);
			} (K-A)<=B (K-A) K A B {
				System.out.println(A);
			} System.out.println(A) System.out.println System.out System System.out A if((K-A)>B) {
				System.out.println(A-(K-(A+B)));
			} (K-A)>B (K-A) K A B {
				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) A B String[] args args public class Main{
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		
		long A = sc.nextLong();
		long B = sc.nextLong();
		long C = sc.nextLong();
		long K = sc.nextLong();
		
		if(A>=K) {
			System.out.println(K);
		}
		if(A<K) {
			if((K-A)<=B) {
				System.out.println(A);
			}
			if((K-A)>B) {
				System.out.println(A-(K-(A+B)));
			}
		}
	}
} public class Main{
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		
		long A = sc.nextLong();
		long B = sc.nextLong();
		long C = sc.nextLong();
		long K = sc.nextLong();
		
		if(A>=K) {
			System.out.println(K);
		}
		if(A<K) {
			if((K-A)<=B) {
				System.out.println(A);
			}
			if((K-A)>B) {
				System.out.println(A-(K-(A+B)));
			}
		}
	}
} Main
//package atcoder;
 
import java.util.*;
 
public class Main{
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		
		long A = sc.nextLong();
		long B = sc.nextLong();
		long C = sc.nextLong();
		long K = sc.nextLong();
		
		if(A>=K) {
			System.out.println(K);
		}
		if(A<K) {
			if((K-A)<=B) {
				System.out.println(A);
			}
			if((K-A)>B) {
				System.out.println(A-(K-(A+B)));
			}
		}
	}
}