import java.util.Scanner;

public class Main{
	public static void main(String[] args){
		Scanner scan = new Scanner(System.in);
		int A = scan.nextInt();
		int B = scan.nextInt();
		int C = scan.nextInt();
		int K = scan.nextInt();

		int sum = 0;

		if(A > K){
			sum += K;
		}else if(A+B >= K){
			sum += A;
		}else{
			K -= A+B;
			sum += (A - K);
		}

		System.out.println(sum);

		scan.close();
	}
} import java.util.Scanner; Scanner java.util public class Main{
	public static void main(String[] args){
		Scanner scan = new Scanner(System.in);
		int A = scan.nextInt();
		int B = scan.nextInt();
		int C = scan.nextInt();
		int K = scan.nextInt();

		int sum = 0;

		if(A > K){
			sum += K;
		}else if(A+B >= K){
			sum += A;
		}else{
			K -= A+B;
			sum += (A - K);
		}

		System.out.println(sum);

		scan.close();
	}
} Main public static void main(String[] args){
		Scanner scan = new Scanner(System.in);
		int A = scan.nextInt();
		int B = scan.nextInt();
		int C = scan.nextInt();
		int K = scan.nextInt();

		int sum = 0;

		if(A > K){
			sum += K;
		}else if(A+B >= K){
			sum += A;
		}else{
			K -= A+B;
			sum += (A - K);
		}

		System.out.println(sum);

		scan.close();
	} main {
		Scanner scan = new Scanner(System.in);
		int A = scan.nextInt();
		int B = scan.nextInt();
		int C = scan.nextInt();
		int K = scan.nextInt();

		int sum = 0;

		if(A > K){
			sum += K;
		}else if(A+B >= K){
			sum += A;
		}else{
			K -= A+B;
			sum += (A - K);
		}

		System.out.println(sum);

		scan.close();
	} Scanner scan = new Scanner(System.in); scan new Scanner(System.in) int A = scan.nextInt(); A scan.nextInt() scan.nextInt scan int B = scan.nextInt(); B scan.nextInt() scan.nextInt scan int C = scan.nextInt(); C scan.nextInt() scan.nextInt scan int K = scan.nextInt(); K scan.nextInt() scan.nextInt scan int sum = 0; sum 0 if(A > K){
			sum += K;
		}else if(A+B >= K){
			sum += A;
		}else{
			K -= A+B;
			sum += (A - K);
		} A > K A K {
			sum += K;
		} sum += K sum K if(A+B >= K){
			sum += A;
		}else{
			K -= A+B;
			sum += (A - K);
		} A+B >= K A+B A B K {
			sum += A;
		} sum += A sum A {
			K -= A+B;
			sum += (A - K);
		} K -= A+B K A+B A B sum += (A - K) sum (A - K) A K System.out.println(sum) System.out.println System.out System System.out sum scan.close() scan.close scan String[] args args public class Main{
	public static void main(String[] args){
		Scanner scan = new Scanner(System.in);
		int A = scan.nextInt();
		int B = scan.nextInt();
		int C = scan.nextInt();
		int K = scan.nextInt();

		int sum = 0;

		if(A > K){
			sum += K;
		}else if(A+B >= K){
			sum += A;
		}else{
			K -= A+B;
			sum += (A - K);
		}

		System.out.println(sum);

		scan.close();
	}
} public class Main{
	public static void main(String[] args){
		Scanner scan = new Scanner(System.in);
		int A = scan.nextInt();
		int B = scan.nextInt();
		int C = scan.nextInt();
		int K = scan.nextInt();

		int sum = 0;

		if(A > K){
			sum += K;
		}else if(A+B >= K){
			sum += A;
		}else{
			K -= A+B;
			sum += (A - K);
		}

		System.out.println(sum);

		scan.close();
	}
} Main
import java.util.Scanner;

public class Main{
	public static void main(String[] args){
		Scanner scan = new Scanner(System.in);
		int A = scan.nextInt();
		int B = scan.nextInt();
		int C = scan.nextInt();
		int K = scan.nextInt();

		int sum = 0;

		if(A > K){
			sum += K;
		}else if(A+B >= K){
			sum += A;
		}else{
			K -= A+B;
			sum += (A - K);
		}

		System.out.println(sum);

		scan.close();
	}
}