import java.util.Scanner;
 
public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
 
		int A,B,C,K;
		A=sc.nextInt();
		B=sc.nextInt();
		C=sc.nextInt();
		K=sc.nextInt();
		int sum=A;
		if(A>=K) {
			sum = K;
		}
		else if(A+B>=K) {
			sum=A;
		}else {
			sum=A-(K-A-B);
		}
		System.out.println(sum);
		
	}
}
 
 
  import java.util.Scanner; Scanner java.util public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
 
		int A,B,C,K;
		A=sc.nextInt();
		B=sc.nextInt();
		C=sc.nextInt();
		K=sc.nextInt();
		int sum=A;
		if(A>=K) {
			sum = K;
		}
		else if(A+B>=K) {
			sum=A;
		}else {
			sum=A-(K-A-B);
		}
		System.out.println(sum);
		
	}
} Main public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
 
		int A,B,C,K;
		A=sc.nextInt();
		B=sc.nextInt();
		C=sc.nextInt();
		K=sc.nextInt();
		int sum=A;
		if(A>=K) {
			sum = K;
		}
		else if(A+B>=K) {
			sum=A;
		}else {
			sum=A-(K-A-B);
		}
		System.out.println(sum);
		
	} main {
		Scanner sc = new Scanner(System.in);
 
		int A,B,C,K;
		A=sc.nextInt();
		B=sc.nextInt();
		C=sc.nextInt();
		K=sc.nextInt();
		int sum=A;
		if(A>=K) {
			sum = K;
		}
		else if(A+B>=K) {
			sum=A;
		}else {
			sum=A-(K-A-B);
		}
		System.out.println(sum);
		
	} 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 int sum=A; sum A if(A>=K) {
			sum = K;
		}
		else if(A+B>=K) {
			sum=A;
		}else {
			sum=A-(K-A-B);
		} A>=K A K {
			sum = K;
		} sum = K sum K if(A+B>=K) {
			sum=A;
		}else {
			sum=A-(K-A-B);
		} A+B>=K A+B A B K {
			sum=A;
		} sum=A sum A {
			sum=A-(K-A-B);
		} sum=A-(K-A-B) sum A-(K-A-B) A (K-A-B) K-A-B K A B System.out.println(sum) System.out.println System.out System System.out sum String[] args args public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
 
		int A,B,C,K;
		A=sc.nextInt();
		B=sc.nextInt();
		C=sc.nextInt();
		K=sc.nextInt();
		int sum=A;
		if(A>=K) {
			sum = K;
		}
		else if(A+B>=K) {
			sum=A;
		}else {
			sum=A-(K-A-B);
		}
		System.out.println(sum);
		
	}
} public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
 
		int A,B,C,K;
		A=sc.nextInt();
		B=sc.nextInt();
		C=sc.nextInt();
		K=sc.nextInt();
		int sum=A;
		if(A>=K) {
			sum = K;
		}
		else if(A+B>=K) {
			sum=A;
		}else {
			sum=A-(K-A-B);
		}
		System.out.println(sum);
		
	}
} Main
import java.util.Scanner;
 
public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
 
		int A,B,C,K;
		A=sc.nextInt();
		B=sc.nextInt();
		C=sc.nextInt();
		K=sc.nextInt();
		int sum=A;
		if(A>=K) {
			sum = K;
		}
		else if(A+B>=K) {
			sum=A;
		}else {
			sum=A-(K-A-B);
		}
		System.out.println(sum);
		
	}
}
 
 
 