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