import java.util.*;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		final long A = sc.nextLong();
		final long B = sc.nextLong();
		final long C = sc.nextLong();
		final long K = sc.nextLong();
		sc.close();
		long ans = Math.min(A, K);
		ans -= (K-A-B)>0?(K-A-B): 0;
		System.out.println(ans);
	}
} import java.util.*; util.* java public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		final long A = sc.nextLong();
		final long B = sc.nextLong();
		final long C = sc.nextLong();
		final long K = sc.nextLong();
		sc.close();
		long ans = Math.min(A, K);
		ans -= (K-A-B)>0?(K-A-B): 0;
		System.out.println(ans);
	}
} Main public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		final long A = sc.nextLong();
		final long B = sc.nextLong();
		final long C = sc.nextLong();
		final long K = sc.nextLong();
		sc.close();
		long ans = Math.min(A, K);
		ans -= (K-A-B)>0?(K-A-B): 0;
		System.out.println(ans);
	} main {
		Scanner sc = new Scanner(System.in);
		final long A = sc.nextLong();
		final long B = sc.nextLong();
		final long C = sc.nextLong();
		final long K = sc.nextLong();
		sc.close();
		long ans = Math.min(A, K);
		ans -= (K-A-B)>0?(K-A-B): 0;
		System.out.println(ans);
	} Scanner sc = new Scanner(System.in); sc new Scanner(System.in) final long A = sc.nextLong(); A sc.nextLong() sc.nextLong sc final long B = sc.nextLong(); B sc.nextLong() sc.nextLong sc final long C = sc.nextLong(); C sc.nextLong() sc.nextLong sc final long K = sc.nextLong(); K sc.nextLong() sc.nextLong sc sc.close() sc.close sc long ans = Math.min(A, K); ans Math.min(A, K) Math.min Math A K ans -= (K-A-B)>0?(K-A-B): 0 ans (K-A-B)>0?(K-A-B): 0 (K-A-B)>0 (K-A-B) K-A-B K A B 0 (K-A-B) K-A-B K A B 0 System.out.println(ans) System.out.println System.out System System.out ans String[] args args public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		final long A = sc.nextLong();
		final long B = sc.nextLong();
		final long C = sc.nextLong();
		final long K = sc.nextLong();
		sc.close();
		long ans = Math.min(A, K);
		ans -= (K-A-B)>0?(K-A-B): 0;
		System.out.println(ans);
	}
} public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		final long A = sc.nextLong();
		final long B = sc.nextLong();
		final long C = sc.nextLong();
		final long K = sc.nextLong();
		sc.close();
		long ans = Math.min(A, K);
		ans -= (K-A-B)>0?(K-A-B): 0;
		System.out.println(ans);
	}
} Main
import java.util.*;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		final long A = sc.nextLong();
		final long B = sc.nextLong();
		final long C = sc.nextLong();
		final long K = sc.nextLong();
		sc.close();
		long ans = Math.min(A, K);
		ans -= (K-A-B)>0?(K-A-B): 0;
		System.out.println(ans);
	}
}
