import java.util.*;
public class Main {
	static Scanner sc = new Scanner(System.in);
	public static void main(String[] args) {
		int a = sc.nextInt();
		int b = sc.nextInt();
		int c = sc.nextInt();
		int k = sc.nextInt();
		System.out.println(Math.min(a, k) - Math.max(k - a - b, 0));
	}
} import java.util.*; util.* java public class Main {
	static Scanner sc = new Scanner(System.in);
	public static void main(String[] args) {
		int a = sc.nextInt();
		int b = sc.nextInt();
		int c = sc.nextInt();
		int k = sc.nextInt();
		System.out.println(Math.min(a, k) - Math.max(k - a - b, 0));
	}
} Main static Scanner sc = new Scanner(System.in); sc new Scanner(System.in) public static void main(String[] args) {
		int a = sc.nextInt();
		int b = sc.nextInt();
		int c = sc.nextInt();
		int k = sc.nextInt();
		System.out.println(Math.min(a, k) - Math.max(k - a - b, 0));
	} main {
		int a = sc.nextInt();
		int b = sc.nextInt();
		int c = sc.nextInt();
		int k = sc.nextInt();
		System.out.println(Math.min(a, k) - Math.max(k - a - b, 0));
	} int a = sc.nextInt(); a sc.nextInt() sc.nextInt sc int b = sc.nextInt(); b sc.nextInt() sc.nextInt sc int c = sc.nextInt(); c sc.nextInt() sc.nextInt sc int k = sc.nextInt(); k sc.nextInt() sc.nextInt sc System.out.println(Math.min(a, k) - Math.max(k - a - b, 0)) System.out.println System.out System System.out Math.min(a, k) - Math.max(k - a - b, 0) Math.min(a, k) Math.min Math a k Math.max(k - a - b, 0) Math.max Math k - a - b k - a - b k a b 0 String[] args args public class Main {
	static Scanner sc = new Scanner(System.in);
	public static void main(String[] args) {
		int a = sc.nextInt();
		int b = sc.nextInt();
		int c = sc.nextInt();
		int k = sc.nextInt();
		System.out.println(Math.min(a, k) - Math.max(k - a - b, 0));
	}
} public class Main {
	static Scanner sc = new Scanner(System.in);
	public static void main(String[] args) {
		int a = sc.nextInt();
		int b = sc.nextInt();
		int c = sc.nextInt();
		int k = sc.nextInt();
		System.out.println(Math.min(a, k) - Math.max(k - a - b, 0));
	}
} Main
import java.util.*;
public class Main {
	static Scanner sc = new Scanner(System.in);
	public static void main(String[] args) {
		int a = sc.nextInt();
		int b = sc.nextInt();
		int c = sc.nextInt();
		int k = sc.nextInt();
		System.out.println(Math.min(a, k) - Math.max(k - a - b, 0));
	}
}