import java.util.*;

public class Main {
	Scanner sc = new Scanner(System.in);
	final int MOD = 1000000007;
	final int MAX = Integer.MAX_VALUE;
	final long LMAX = Long.MAX_VALUE;
	int len = (int)1e6 + 1;
	
	void doIt() {
		int A = sc.nextInt();
		int B = sc.nextInt();
		int C = sc.nextInt();
		int K = sc.nextInt();
		int sum = 0;
		if(K >= A) {
			K -= A;
			sum += A;
		}
		else {
			sum += K;
			K = 0;
		}
		K -= B;
		if(K >= 0) {
			sum -= Math.min(K, C);
		}
		System.out.println(sum);
		
		
		
		
	}
	
	public static void main(String[] args) {
		new Main().doIt();
	}

} import java.util.*; util.* java public class Main {
	Scanner sc = new Scanner(System.in);
	final int MOD = 1000000007;
	final int MAX = Integer.MAX_VALUE;
	final long LMAX = Long.MAX_VALUE;
	int len = (int)1e6 + 1;
	
	void doIt() {
		int A = sc.nextInt();
		int B = sc.nextInt();
		int C = sc.nextInt();
		int K = sc.nextInt();
		int sum = 0;
		if(K >= A) {
			K -= A;
			sum += A;
		}
		else {
			sum += K;
			K = 0;
		}
		K -= B;
		if(K >= 0) {
			sum -= Math.min(K, C);
		}
		System.out.println(sum);
		
		
		
		
	}
	
	public static void main(String[] args) {
		new Main().doIt();
	}

} Main Scanner sc = new Scanner(System.in); sc new Scanner(System.in) final int MOD = 1000000007; MOD 1000000007 final int MAX = Integer.MAX_VALUE; MAX Integer.MAX_VALUE Integer Integer.MAX_VALUE final long LMAX = Long.MAX_VALUE; LMAX Long.MAX_VALUE Long Long.MAX_VALUE int len = (int)1e6 + 1; len (int)1e6 + 1 (int)1e6 1 void doIt() {
		int A = sc.nextInt();
		int B = sc.nextInt();
		int C = sc.nextInt();
		int K = sc.nextInt();
		int sum = 0;
		if(K >= A) {
			K -= A;
			sum += A;
		}
		else {
			sum += K;
			K = 0;
		}
		K -= B;
		if(K >= 0) {
			sum -= Math.min(K, C);
		}
		System.out.println(sum);
		
		
		
		
	} doIt {
		int A = sc.nextInt();
		int B = sc.nextInt();
		int C = sc.nextInt();
		int K = sc.nextInt();
		int sum = 0;
		if(K >= A) {
			K -= A;
			sum += A;
		}
		else {
			sum += K;
			K = 0;
		}
		K -= B;
		if(K >= 0) {
			sum -= Math.min(K, C);
		}
		System.out.println(sum);
		
		
		
		
	} 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 int sum = 0; sum 0 if(K >= A) {
			K -= A;
			sum += A;
		}
		else {
			sum += K;
			K = 0;
		} K >= A K A {
			K -= A;
			sum += A;
		} K -= A K A sum += A sum A {
			sum += K;
			K = 0;
		} sum += K sum K K = 0 K 0 K -= B K B if(K >= 0) {
			sum -= Math.min(K, C);
		} K >= 0 K 0 {
			sum -= Math.min(K, C);
		} sum -= Math.min(K, C) sum Math.min(K, C) Math.min Math K C System.out.println(sum) System.out.println System.out System System.out sum public static void main(String[] args) {
		new Main().doIt();
	} main {
		new Main().doIt();
	} new Main().doIt() new Main().doIt new Main() String[] args args public class Main {
	Scanner sc = new Scanner(System.in);
	final int MOD = 1000000007;
	final int MAX = Integer.MAX_VALUE;
	final long LMAX = Long.MAX_VALUE;
	int len = (int)1e6 + 1;
	
	void doIt() {
		int A = sc.nextInt();
		int B = sc.nextInt();
		int C = sc.nextInt();
		int K = sc.nextInt();
		int sum = 0;
		if(K >= A) {
			K -= A;
			sum += A;
		}
		else {
			sum += K;
			K = 0;
		}
		K -= B;
		if(K >= 0) {
			sum -= Math.min(K, C);
		}
		System.out.println(sum);
		
		
		
		
	}
	
	public static void main(String[] args) {
		new Main().doIt();
	}

} public class Main {
	Scanner sc = new Scanner(System.in);
	final int MOD = 1000000007;
	final int MAX = Integer.MAX_VALUE;
	final long LMAX = Long.MAX_VALUE;
	int len = (int)1e6 + 1;
	
	void doIt() {
		int A = sc.nextInt();
		int B = sc.nextInt();
		int C = sc.nextInt();
		int K = sc.nextInt();
		int sum = 0;
		if(K >= A) {
			K -= A;
			sum += A;
		}
		else {
			sum += K;
			K = 0;
		}
		K -= B;
		if(K >= 0) {
			sum -= Math.min(K, C);
		}
		System.out.println(sum);
		
		
		
		
	}
	
	public static void main(String[] args) {
		new Main().doIt();
	}

} Main
import java.util.*;

public class Main {
	Scanner sc = new Scanner(System.in);
	final int MOD = 1000000007;
	final int MAX = Integer.MAX_VALUE;
	final long LMAX = Long.MAX_VALUE;
	int len = (int)1e6 + 1;
	
	void doIt() {
		int A = sc.nextInt();
		int B = sc.nextInt();
		int C = sc.nextInt();
		int K = sc.nextInt();
		int sum = 0;
		if(K >= A) {
			K -= A;
			sum += A;
		}
		else {
			sum += K;
			K = 0;
		}
		K -= B;
		if(K >= 0) {
			sum -= Math.min(K, C);
		}
		System.out.println(sum);
		
		
		
		
	}
	
	public static void main(String[] args) {
		new Main().doIt();
	}

}
