import java.util.*;
import java.io.PrintWriter;
import static java.lang.Integer.*;
import static java.lang.Long.*;
import static java.lang.Math.*;
import static java.lang.System.*;

public class Main {
	public static final int MOD = 1000000007;
	public static void main(String[] args) {
		int i,j;
		Scanner sc = new Scanner(in);
		long a = parseLong(sc.next());
		long b = parseLong(sc.next());
		long c = parseLong(sc.next());
		long k = parseLong(sc.next());
		sc.close();
		long ans=0;
		ans=Math.min(a, k);
		k=Math.max(k-a-b, 0);
		if(k>0) {
			ans-=Math.min(c, k);
		}
		out.println(ans);
	}
} import java.util.*; util.* java import java.io.PrintWriter; PrintWriter java.io import static java.lang.Integer.*; import static java.lang.Integer.*; import static java.lang.Integer.*; import static java.lang.Long.*; import static java.lang.Long.*; import static java.lang.Long.*; import static java.lang.Math.*; import static java.lang.Math.*; import static java.lang.Math.*; import static java.lang.System.*; import static java.lang.System.*; import static java.lang.System.*; public class Main {
	public static final int MOD = 1000000007;
	public static void main(String[] args) {
		int i,j;
		Scanner sc = new Scanner(in);
		long a = parseLong(sc.next());
		long b = parseLong(sc.next());
		long c = parseLong(sc.next());
		long k = parseLong(sc.next());
		sc.close();
		long ans=0;
		ans=Math.min(a, k);
		k=Math.max(k-a-b, 0);
		if(k>0) {
			ans-=Math.min(c, k);
		}
		out.println(ans);
	}
} Main public static final int MOD = 1000000007; MOD 1000000007 public static void main(String[] args) {
		int i,j;
		Scanner sc = new Scanner(in);
		long a = parseLong(sc.next());
		long b = parseLong(sc.next());
		long c = parseLong(sc.next());
		long k = parseLong(sc.next());
		sc.close();
		long ans=0;
		ans=Math.min(a, k);
		k=Math.max(k-a-b, 0);
		if(k>0) {
			ans-=Math.min(c, k);
		}
		out.println(ans);
	} main {
		int i,j;
		Scanner sc = new Scanner(in);
		long a = parseLong(sc.next());
		long b = parseLong(sc.next());
		long c = parseLong(sc.next());
		long k = parseLong(sc.next());
		sc.close();
		long ans=0;
		ans=Math.min(a, k);
		k=Math.max(k-a-b, 0);
		if(k>0) {
			ans-=Math.min(c, k);
		}
		out.println(ans);
	} int i i j; j Scanner sc = new Scanner(in); sc new Scanner(in) long a = parseLong(sc.next()); a parseLong(sc.next()) parseLong sc.next() sc.next sc long b = parseLong(sc.next()); b parseLong(sc.next()) parseLong sc.next() sc.next sc long c = parseLong(sc.next()); c parseLong(sc.next()) parseLong sc.next() sc.next sc long k = parseLong(sc.next()); k parseLong(sc.next()) parseLong sc.next() sc.next sc sc.close() sc.close sc long ans=0; ans 0 ans=Math.min(a, k) ans Math.min(a, k) Math.min Math a k k=Math.max(k-a-b, 0) k Math.max(k-a-b, 0) Math.max Math k-a-b k-a-b k a b 0 if(k>0) {
			ans-=Math.min(c, k);
		} k>0 k 0 {
			ans-=Math.min(c, k);
		} ans-=Math.min(c, k) ans Math.min(c, k) Math.min Math c k out.println(ans) out.println out ans String[] args args public class Main {
	public static final int MOD = 1000000007;
	public static void main(String[] args) {
		int i,j;
		Scanner sc = new Scanner(in);
		long a = parseLong(sc.next());
		long b = parseLong(sc.next());
		long c = parseLong(sc.next());
		long k = parseLong(sc.next());
		sc.close();
		long ans=0;
		ans=Math.min(a, k);
		k=Math.max(k-a-b, 0);
		if(k>0) {
			ans-=Math.min(c, k);
		}
		out.println(ans);
	}
} public class Main {
	public static final int MOD = 1000000007;
	public static void main(String[] args) {
		int i,j;
		Scanner sc = new Scanner(in);
		long a = parseLong(sc.next());
		long b = parseLong(sc.next());
		long c = parseLong(sc.next());
		long k = parseLong(sc.next());
		sc.close();
		long ans=0;
		ans=Math.min(a, k);
		k=Math.max(k-a-b, 0);
		if(k>0) {
			ans-=Math.min(c, k);
		}
		out.println(ans);
	}
} Main
import java.util.*;
import java.io.PrintWriter;
import static java.lang.Integer.*;
import static java.lang.Long.*;
import static java.lang.Math.*;
import static java.lang.System.*;

public class Main {
	public static final int MOD = 1000000007;
	public static void main(String[] args) {
		int i,j;
		Scanner sc = new Scanner(in);
		long a = parseLong(sc.next());
		long b = parseLong(sc.next());
		long c = parseLong(sc.next());
		long k = parseLong(sc.next());
		sc.close();
		long ans=0;
		ans=Math.min(a, k);
		k=Math.max(k-a-b, 0);
		if(k>0) {
			ans-=Math.min(c, k);
		}
		out.println(ans);
	}
}
