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();

		long sum = 0;
		if (a >= k) {
			sum = k;
		} else if (a < k && a + b >= k) {
			sum = a;
		} else {
			sum = a - (k - (a + b));
		}

		// 出力
		System.out.println(sum);
		sc.close();
	}
} 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();

		long sum = 0;
		if (a >= k) {
			sum = k;
		} else if (a < k && a + b >= k) {
			sum = a;
		} else {
			sum = a - (k - (a + b));
		}

		// 出力
		System.out.println(sum);
		sc.close();
	}
} 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();

		long sum = 0;
		if (a >= k) {
			sum = k;
		} else if (a < k && a + b >= k) {
			sum = a;
		} else {
			sum = a - (k - (a + b));
		}

		// 出力
		System.out.println(sum);
		sc.close();
	} main {
		Scanner sc = new Scanner(System.in);
		// 文字列の入力
		long a = sc.nextLong();
		long b = sc.nextLong();
		long c = sc.nextLong();
		long k = sc.nextLong();

		long sum = 0;
		if (a >= k) {
			sum = k;
		} else if (a < k && a + b >= k) {
			sum = a;
		} else {
			sum = a - (k - (a + b));
		}

		// 出力
		System.out.println(sum);
		sc.close();
	} 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 long sum = 0; sum 0 if (a >= k) {
			sum = k;
		} else if (a < k && a + b >= k) {
			sum = a;
		} else {
			sum = a - (k - (a + b));
		} a >= k a k {
			sum = k;
		} sum = k sum k if (a < k && a + b >= k) {
			sum = a;
		} else {
			sum = a - (k - (a + b));
		} a < k && a + b >= k a < k a k 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) a b System.out.println(sum) System.out.println System.out System System.out sum sc.close() sc.close sc 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();

		long sum = 0;
		if (a >= k) {
			sum = k;
		} else if (a < k && a + b >= k) {
			sum = a;
		} else {
			sum = a - (k - (a + b));
		}

		// 出力
		System.out.println(sum);
		sc.close();
	}
} 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();

		long sum = 0;
		if (a >= k) {
			sum = k;
		} else if (a < k && a + b >= k) {
			sum = a;
		} else {
			sum = a - (k - (a + b));
		}

		// 出力
		System.out.println(sum);
		sc.close();
	}
} Main
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();

		long sum = 0;
		if (a >= k) {
			sum = k;
		} else if (a < k && a + b >= k) {
			sum = a;
		} else {
			sum = a - (k - (a + b));
		}

		// 出力
		System.out.println(sum);
		sc.close();
	}
}
