import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main {
	public static void main(String[] args) throws IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		String line1 = br.readLine();
		String[] work = line1.split(" ");
		int a = Integer.parseInt(work[0]);
		int b = Integer.parseInt(work[1]);
		int c = Integer.parseInt(work[2]);
		int k = Integer.parseInt(work[3]);
		System.out.println(Math.min(a, k) - Math.max(0, (k - a - b)));
	}
} import java.io.BufferedReader; BufferedReader java.io import java.io.IOException; IOException java.io import java.io.InputStreamReader; InputStreamReader java.io public class Main {
	public static void main(String[] args) throws IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		String line1 = br.readLine();
		String[] work = line1.split(" ");
		int a = Integer.parseInt(work[0]);
		int b = Integer.parseInt(work[1]);
		int c = Integer.parseInt(work[2]);
		int k = Integer.parseInt(work[3]);
		System.out.println(Math.min(a, k) - Math.max(0, (k - a - b)));
	}
} Main public static void main(String[] args) throws IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		String line1 = br.readLine();
		String[] work = line1.split(" ");
		int a = Integer.parseInt(work[0]);
		int b = Integer.parseInt(work[1]);
		int c = Integer.parseInt(work[2]);
		int k = Integer.parseInt(work[3]);
		System.out.println(Math.min(a, k) - Math.max(0, (k - a - b)));
	} main {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		String line1 = br.readLine();
		String[] work = line1.split(" ");
		int a = Integer.parseInt(work[0]);
		int b = Integer.parseInt(work[1]);
		int c = Integer.parseInt(work[2]);
		int k = Integer.parseInt(work[3]);
		System.out.println(Math.min(a, k) - Math.max(0, (k - a - b)));
	} BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); br new BufferedReader(new InputStreamReader(System.in)) String line1 = br.readLine(); line1 br.readLine() br.readLine br String[] work = line1.split(" "); work line1.split(" ") line1.split line1 " " int a = Integer.parseInt(work[0]); a Integer.parseInt(work[0]) Integer.parseInt Integer work[0] work 0 int b = Integer.parseInt(work[1]); b Integer.parseInt(work[1]) Integer.parseInt Integer work[1] work 1 int c = Integer.parseInt(work[2]); c Integer.parseInt(work[2]) Integer.parseInt Integer work[2] work 2 int k = Integer.parseInt(work[3]); k Integer.parseInt(work[3]) Integer.parseInt Integer work[3] work 3 System.out.println(Math.min(a, k) - Math.max(0, (k - a - b))) System.out.println System.out System System.out Math.min(a, k) - Math.max(0, (k - a - b)) Math.min(a, k) Math.min Math a k Math.max(0, (k - a - b)) Math.max Math 0 (k - a - b) k - a - b k a b String[] args args public class Main {
	public static void main(String[] args) throws IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		String line1 = br.readLine();
		String[] work = line1.split(" ");
		int a = Integer.parseInt(work[0]);
		int b = Integer.parseInt(work[1]);
		int c = Integer.parseInt(work[2]);
		int k = Integer.parseInt(work[3]);
		System.out.println(Math.min(a, k) - Math.max(0, (k - a - b)));
	}
} public class Main {
	public static void main(String[] args) throws IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		String line1 = br.readLine();
		String[] work = line1.split(" ");
		int a = Integer.parseInt(work[0]);
		int b = Integer.parseInt(work[1]);
		int c = Integer.parseInt(work[2]);
		int k = Integer.parseInt(work[3]);
		System.out.println(Math.min(a, k) - Math.max(0, (k - a - b)));
	}
} Main
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main {
	public static void main(String[] args) throws IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		String line1 = br.readLine();
		String[] work = line1.split(" ");
		int a = Integer.parseInt(work[0]);
		int b = Integer.parseInt(work[1]);
		int c = Integer.parseInt(work[2]);
		int k = Integer.parseInt(work[3]);
		System.out.println(Math.min(a, k) - Math.max(0, (k - a - b)));
	}
}
