import java.util.Scanner;
public class Main {

	public static void main(String[] args) {
		// TODO 自動生成されたメソッド・スタブ

Scanner scan = new Scanner(System.in);

		int a = scan.nextInt();
		int b = scan.nextInt();
		int c = scan.nextInt();
		long k = scan.nextLong();

		long re = 0L;

		if(k <= a) {
			re = k;
			}else if(k <= (a+b)) {
	re = a;
			}else {
	re = a-(k-a-b);
			}
		System.out.println(re);
		scan.close();
	}

} import java.util.Scanner; Scanner java.util public class Main {

	public static void main(String[] args) {
		// TODO 自動生成されたメソッド・スタブ

Scanner scan = new Scanner(System.in);

		int a = scan.nextInt();
		int b = scan.nextInt();
		int c = scan.nextInt();
		long k = scan.nextLong();

		long re = 0L;

		if(k <= a) {
			re = k;
			}else if(k <= (a+b)) {
	re = a;
			}else {
	re = a-(k-a-b);
			}
		System.out.println(re);
		scan.close();
	}

} Main public static void main(String[] args) {
		// TODO 自動生成されたメソッド・スタブ

Scanner scan = new Scanner(System.in);

		int a = scan.nextInt();
		int b = scan.nextInt();
		int c = scan.nextInt();
		long k = scan.nextLong();

		long re = 0L;

		if(k <= a) {
			re = k;
			}else if(k <= (a+b)) {
	re = a;
			}else {
	re = a-(k-a-b);
			}
		System.out.println(re);
		scan.close();
	} main {
		// TODO 自動生成されたメソッド・スタブ

Scanner scan = new Scanner(System.in);

		int a = scan.nextInt();
		int b = scan.nextInt();
		int c = scan.nextInt();
		long k = scan.nextLong();

		long re = 0L;

		if(k <= a) {
			re = k;
			}else if(k <= (a+b)) {
	re = a;
			}else {
	re = a-(k-a-b);
			}
		System.out.println(re);
		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 long k = scan.nextLong(); k scan.nextLong() scan.nextLong scan long re = 0L; re 0L if(k <= a) {
			re = k;
			}else if(k <= (a+b)) {
	re = a;
			}else {
	re = a-(k-a-b);
			} k <= a k a {
			re = k;
			} re = k re k if(k <= (a+b)) {
	re = a;
			}else {
	re = a-(k-a-b);
			} k <= (a+b) k (a+b) a b {
	re = a;
			} re = a re a {
	re = a-(k-a-b);
			} re = a-(k-a-b) re a-(k-a-b) a (k-a-b) k-a-b k a b System.out.println(re) System.out.println System.out System System.out re scan.close() scan.close scan String[] args args public class Main {

	public static void main(String[] args) {
		// TODO 自動生成されたメソッド・スタブ

Scanner scan = new Scanner(System.in);

		int a = scan.nextInt();
		int b = scan.nextInt();
		int c = scan.nextInt();
		long k = scan.nextLong();

		long re = 0L;

		if(k <= a) {
			re = k;
			}else if(k <= (a+b)) {
	re = a;
			}else {
	re = a-(k-a-b);
			}
		System.out.println(re);
		scan.close();
	}

} public class Main {

	public static void main(String[] args) {
		// TODO 自動生成されたメソッド・スタブ

Scanner scan = new Scanner(System.in);

		int a = scan.nextInt();
		int b = scan.nextInt();
		int c = scan.nextInt();
		long k = scan.nextLong();

		long re = 0L;

		if(k <= a) {
			re = k;
			}else if(k <= (a+b)) {
	re = a;
			}else {
	re = a-(k-a-b);
			}
		System.out.println(re);
		scan.close();
	}

} Main
import java.util.Scanner;
public class Main {

	public static void main(String[] args) {
		// TODO 自動生成されたメソッド・スタブ

Scanner scan = new Scanner(System.in);

		int a = scan.nextInt();
		int b = scan.nextInt();
		int c = scan.nextInt();
		long k = scan.nextLong();

		long re = 0L;

		if(k <= a) {
			re = k;
			}else if(k <= (a+b)) {
	re = a;
			}else {
	re = a-(k-a-b);
			}
		System.out.println(re);
		scan.close();
	}

}
