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

public class Main {
	public static void main(String[] args) throws IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer st = new StringTokenizer(br.readLine());
		int po = Integer.parseInt(st.nextToken());
		int z = Integer.parseInt(st.nextToken());
		int no = Integer.parseInt(st.nextToken());
		int K = Integer.parseInt(st.nextToken());
		if (K <= po) {
			System.out.println(K);
		} else if (K <= po + z) {
			System.out.println(po);
		} else {
			System.out.println(po - (K - po - z));
		}
	}
} import java.io.BufferedReader; BufferedReader java.io import java.io.IOException; IOException java.io import java.io.InputStreamReader; InputStreamReader java.io import java.util.StringTokenizer; StringTokenizer java.util public class Main {
	public static void main(String[] args) throws IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer st = new StringTokenizer(br.readLine());
		int po = Integer.parseInt(st.nextToken());
		int z = Integer.parseInt(st.nextToken());
		int no = Integer.parseInt(st.nextToken());
		int K = Integer.parseInt(st.nextToken());
		if (K <= po) {
			System.out.println(K);
		} else if (K <= po + z) {
			System.out.println(po);
		} else {
			System.out.println(po - (K - po - z));
		}
	}
} Main public static void main(String[] args) throws IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer st = new StringTokenizer(br.readLine());
		int po = Integer.parseInt(st.nextToken());
		int z = Integer.parseInt(st.nextToken());
		int no = Integer.parseInt(st.nextToken());
		int K = Integer.parseInt(st.nextToken());
		if (K <= po) {
			System.out.println(K);
		} else if (K <= po + z) {
			System.out.println(po);
		} else {
			System.out.println(po - (K - po - z));
		}
	} main {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer st = new StringTokenizer(br.readLine());
		int po = Integer.parseInt(st.nextToken());
		int z = Integer.parseInt(st.nextToken());
		int no = Integer.parseInt(st.nextToken());
		int K = Integer.parseInt(st.nextToken());
		if (K <= po) {
			System.out.println(K);
		} else if (K <= po + z) {
			System.out.println(po);
		} else {
			System.out.println(po - (K - po - z));
		}
	} BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); br new BufferedReader(new InputStreamReader(System.in)) StringTokenizer st = new StringTokenizer(br.readLine()); st new StringTokenizer(br.readLine()) int po = Integer.parseInt(st.nextToken()); po Integer.parseInt(st.nextToken()) Integer.parseInt Integer st.nextToken() st.nextToken st int z = Integer.parseInt(st.nextToken()); z Integer.parseInt(st.nextToken()) Integer.parseInt Integer st.nextToken() st.nextToken st int no = Integer.parseInt(st.nextToken()); no Integer.parseInt(st.nextToken()) Integer.parseInt Integer st.nextToken() st.nextToken st int K = Integer.parseInt(st.nextToken()); K Integer.parseInt(st.nextToken()) Integer.parseInt Integer st.nextToken() st.nextToken st if (K <= po) {
			System.out.println(K);
		} else if (K <= po + z) {
			System.out.println(po);
		} else {
			System.out.println(po - (K - po - z));
		} K <= po K po {
			System.out.println(K);
		} System.out.println(K) System.out.println System.out System System.out K if (K <= po + z) {
			System.out.println(po);
		} else {
			System.out.println(po - (K - po - z));
		} K <= po + z K po + z po z {
			System.out.println(po);
		} System.out.println(po) System.out.println System.out System System.out po {
			System.out.println(po - (K - po - z));
		} System.out.println(po - (K - po - z)) System.out.println System.out System System.out po - (K - po - z) po (K - po - z) K - po - z K po z String[] args args public class Main {
	public static void main(String[] args) throws IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer st = new StringTokenizer(br.readLine());
		int po = Integer.parseInt(st.nextToken());
		int z = Integer.parseInt(st.nextToken());
		int no = Integer.parseInt(st.nextToken());
		int K = Integer.parseInt(st.nextToken());
		if (K <= po) {
			System.out.println(K);
		} else if (K <= po + z) {
			System.out.println(po);
		} else {
			System.out.println(po - (K - po - z));
		}
	}
} public class Main {
	public static void main(String[] args) throws IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer st = new StringTokenizer(br.readLine());
		int po = Integer.parseInt(st.nextToken());
		int z = Integer.parseInt(st.nextToken());
		int no = Integer.parseInt(st.nextToken());
		int K = Integer.parseInt(st.nextToken());
		if (K <= po) {
			System.out.println(K);
		} else if (K <= po + z) {
			System.out.println(po);
		} else {
			System.out.println(po - (K - po - z));
		}
	}
} Main
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class Main {
	public static void main(String[] args) throws IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer st = new StringTokenizer(br.readLine());
		int po = Integer.parseInt(st.nextToken());
		int z = Integer.parseInt(st.nextToken());
		int no = Integer.parseInt(st.nextToken());
		int K = Integer.parseInt(st.nextToken());
		if (K <= po) {
			System.out.println(K);
		} else if (K <= po + z) {
			System.out.println(po);
		} else {
			System.out.println(po - (K - po - z));
		}
	}
}
