import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner s = new Scanner(System.in);
		long A = s.nextInt();
		long B = s.nextInt();
		long C = s.nextInt();
		long K = s.nextInt();
		
		long ans =0;
		
		if(K <= A) {ans =K;}
		else if(K <= A+B) {ans = A;}
		else {ans = A-(K-A-B);}
		
		System.out.println(ans);
	}

} import java.util.Scanner; Scanner java.util public class Main {
	public static void main(String[] args) {
		Scanner s = new Scanner(System.in);
		long A = s.nextInt();
		long B = s.nextInt();
		long C = s.nextInt();
		long K = s.nextInt();
		
		long ans =0;
		
		if(K <= A) {ans =K;}
		else if(K <= A+B) {ans = A;}
		else {ans = A-(K-A-B);}
		
		System.out.println(ans);
	}

} Main public static void main(String[] args) {
		Scanner s = new Scanner(System.in);
		long A = s.nextInt();
		long B = s.nextInt();
		long C = s.nextInt();
		long K = s.nextInt();
		
		long ans =0;
		
		if(K <= A) {ans =K;}
		else if(K <= A+B) {ans = A;}
		else {ans = A-(K-A-B);}
		
		System.out.println(ans);
	} main {
		Scanner s = new Scanner(System.in);
		long A = s.nextInt();
		long B = s.nextInt();
		long C = s.nextInt();
		long K = s.nextInt();
		
		long ans =0;
		
		if(K <= A) {ans =K;}
		else if(K <= A+B) {ans = A;}
		else {ans = A-(K-A-B);}
		
		System.out.println(ans);
	} Scanner s = new Scanner(System.in); s new Scanner(System.in) long A = s.nextInt(); A s.nextInt() s.nextInt s long B = s.nextInt(); B s.nextInt() s.nextInt s long C = s.nextInt(); C s.nextInt() s.nextInt s long K = s.nextInt(); K s.nextInt() s.nextInt s long ans =0; ans 0 if(K <= A) {ans =K;}
		else if(K <= A+B) {ans = A;}
		else {ans = A-(K-A-B);} K <= A K A {ans =K;} ans =K ans K if(K <= A+B) {ans = A;}
		else {ans = A-(K-A-B);} K <= A+B K A+B A B {ans = A;} ans = A ans A {ans = A-(K-A-B);} ans = A-(K-A-B) ans A-(K-A-B) A (K-A-B) K-A-B K A B 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 s = new Scanner(System.in);
		long A = s.nextInt();
		long B = s.nextInt();
		long C = s.nextInt();
		long K = s.nextInt();
		
		long ans =0;
		
		if(K <= A) {ans =K;}
		else if(K <= A+B) {ans = A;}
		else {ans = A-(K-A-B);}
		
		System.out.println(ans);
	}

} public class Main {
	public static void main(String[] args) {
		Scanner s = new Scanner(System.in);
		long A = s.nextInt();
		long B = s.nextInt();
		long C = s.nextInt();
		long K = s.nextInt();
		
		long ans =0;
		
		if(K <= A) {ans =K;}
		else if(K <= A+B) {ans = A;}
		else {ans = A-(K-A-B);}
		
		System.out.println(ans);
	}

} Main
import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner s = new Scanner(System.in);
		long A = s.nextInt();
		long B = s.nextInt();
		long C = s.nextInt();
		long K = s.nextInt();
		
		long ans =0;
		
		if(K <= A) {ans =K;}
		else if(K <= A+B) {ans = A;}
		else {ans = A-(K-A-B);}
		
		System.out.println(ans);
	}

}
