import java.util.Scanner;

public class Main {

	public static void main(String[] args) {

		Scanner sc = new Scanner(System.in);

		int a = sc.nextInt();
		int b = sc.nextInt();
		int c = sc.nextInt();
		int k = sc.nextInt();

		System.out.println(a+b>=k ? (a>=k ? 1*k : 1*a) : 1*a-1*(k-a-b));
	}
} import java.util.Scanner; Scanner java.util public class Main {

	public static void main(String[] args) {

		Scanner sc = new Scanner(System.in);

		int a = sc.nextInt();
		int b = sc.nextInt();
		int c = sc.nextInt();
		int k = sc.nextInt();

		System.out.println(a+b>=k ? (a>=k ? 1*k : 1*a) : 1*a-1*(k-a-b));
	}
} Main public static void main(String[] args) {

		Scanner sc = new Scanner(System.in);

		int a = sc.nextInt();
		int b = sc.nextInt();
		int c = sc.nextInt();
		int k = sc.nextInt();

		System.out.println(a+b>=k ? (a>=k ? 1*k : 1*a) : 1*a-1*(k-a-b));
	} main {

		Scanner sc = new Scanner(System.in);

		int a = sc.nextInt();
		int b = sc.nextInt();
		int c = sc.nextInt();
		int k = sc.nextInt();

		System.out.println(a+b>=k ? (a>=k ? 1*k : 1*a) : 1*a-1*(k-a-b));
	} Scanner sc = new Scanner(System.in); sc new Scanner(System.in) 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(a+b>=k ? (a>=k ? 1*k : 1*a) : 1*a-1*(k-a-b)) System.out.println System.out System System.out a+b>=k ? (a>=k ? 1*k : 1*a) : 1*a-1*(k-a-b) a+b>=k a+b a b k (a>=k ? 1*k : 1*a) a>=k a k 1*k 1 k 1*a 1 a 1*a-1*(k-a-b) 1*a 1 a 1*(k-a-b) 1 (k-a-b) k-a-b k a b String[] args args public class Main {

	public static void main(String[] args) {

		Scanner sc = new Scanner(System.in);

		int a = sc.nextInt();
		int b = sc.nextInt();
		int c = sc.nextInt();
		int k = sc.nextInt();

		System.out.println(a+b>=k ? (a>=k ? 1*k : 1*a) : 1*a-1*(k-a-b));
	}
} public class Main {

	public static void main(String[] args) {

		Scanner sc = new Scanner(System.in);

		int a = sc.nextInt();
		int b = sc.nextInt();
		int c = sc.nextInt();
		int k = sc.nextInt();

		System.out.println(a+b>=k ? (a>=k ? 1*k : 1*a) : 1*a-1*(k-a-b));
	}
} Main
import java.util.Scanner;

public class Main {

	public static void main(String[] args) {

		Scanner sc = new Scanner(System.in);

		int a = sc.nextInt();
		int b = sc.nextInt();
		int c = sc.nextInt();
		int k = sc.nextInt();

		System.out.println(a+b>=k ? (a>=k ? 1*k : 1*a) : 1*a-1*(k-a-b));
	}
}