import java.util.Scanner;
public class Main {
	static int[][] gyo2=new int[100000][12];
	static int count=0;
	@SuppressWarnings("resource")
	public static void main(String[] args) {
		Scanner sc=new Scanner(System.in);
		long A=sc.nextLong();
		long B=sc.nextLong();
		long C=sc.nextLong();
		long K=sc.nextLong();
		long ans=0;
		if(K>A) {
			ans+=A;
			K-=A;
		}else {
			ans+=K;
			K=0;
		}
		if(K>0) {
			K-=B;
		}
		if(K>0) {
			if(K>C) {
				ans-=C;
			}else {
				ans-=K;
			}
		}
		System.out.println(ans);

}

} import java.util.Scanner; Scanner java.util public class Main {
	static int[][] gyo2=new int[100000][12];
	static int count=0;
	@SuppressWarnings("resource")
	public static void main(String[] args) {
		Scanner sc=new Scanner(System.in);
		long A=sc.nextLong();
		long B=sc.nextLong();
		long C=sc.nextLong();
		long K=sc.nextLong();
		long ans=0;
		if(K>A) {
			ans+=A;
			K-=A;
		}else {
			ans+=K;
			K=0;
		}
		if(K>0) {
			K-=B;
		}
		if(K>0) {
			if(K>C) {
				ans-=C;
			}else {
				ans-=K;
			}
		}
		System.out.println(ans);

}

} Main static int[][] gyo2=new int[100000][12]; gyo2 new int[100000][12] 100000 12 static int count=0; count 0 @SuppressWarnings("resource")
	public static void main(String[] args) {
		Scanner sc=new Scanner(System.in);
		long A=sc.nextLong();
		long B=sc.nextLong();
		long C=sc.nextLong();
		long K=sc.nextLong();
		long ans=0;
		if(K>A) {
			ans+=A;
			K-=A;
		}else {
			ans+=K;
			K=0;
		}
		if(K>0) {
			K-=B;
		}
		if(K>0) {
			if(K>C) {
				ans-=C;
			}else {
				ans-=K;
			}
		}
		System.out.println(ans);

} main {
		Scanner sc=new Scanner(System.in);
		long A=sc.nextLong();
		long B=sc.nextLong();
		long C=sc.nextLong();
		long K=sc.nextLong();
		long ans=0;
		if(K>A) {
			ans+=A;
			K-=A;
		}else {
			ans+=K;
			K=0;
		}
		if(K>0) {
			K-=B;
		}
		if(K>0) {
			if(K>C) {
				ans-=C;
			}else {
				ans-=K;
			}
		}
		System.out.println(ans);

} Scanner sc=new Scanner(System.in); sc new Scanner(System.in) long A=sc.nextLong(); A sc.nextLong() sc.nextLong sc long B=sc.nextLong(); B sc.nextLong() sc.nextLong sc long C=sc.nextLong(); C sc.nextLong() sc.nextLong sc long K=sc.nextLong(); K sc.nextLong() sc.nextLong sc long ans=0; ans 0 if(K>A) {
			ans+=A;
			K-=A;
		}else {
			ans+=K;
			K=0;
		} K>A K A {
			ans+=A;
			K-=A;
		} ans+=A ans A K-=A K A {
			ans+=K;
			K=0;
		} ans+=K ans K K=0 K 0 if(K>0) {
			K-=B;
		} K>0 K 0 {
			K-=B;
		} K-=B K B if(K>0) {
			if(K>C) {
				ans-=C;
			}else {
				ans-=K;
			}
		} K>0 K 0 {
			if(K>C) {
				ans-=C;
			}else {
				ans-=K;
			}
		} if(K>C) {
				ans-=C;
			}else {
				ans-=K;
			} K>C K C {
				ans-=C;
			} ans-=C ans C {
				ans-=K;
			} ans-=K ans K System.out.println(ans) System.out.println System.out System System.out ans String[] args args public class Main {
	static int[][] gyo2=new int[100000][12];
	static int count=0;
	@SuppressWarnings("resource")
	public static void main(String[] args) {
		Scanner sc=new Scanner(System.in);
		long A=sc.nextLong();
		long B=sc.nextLong();
		long C=sc.nextLong();
		long K=sc.nextLong();
		long ans=0;
		if(K>A) {
			ans+=A;
			K-=A;
		}else {
			ans+=K;
			K=0;
		}
		if(K>0) {
			K-=B;
		}
		if(K>0) {
			if(K>C) {
				ans-=C;
			}else {
				ans-=K;
			}
		}
		System.out.println(ans);

}

} public class Main {
	static int[][] gyo2=new int[100000][12];
	static int count=0;
	@SuppressWarnings("resource")
	public static void main(String[] args) {
		Scanner sc=new Scanner(System.in);
		long A=sc.nextLong();
		long B=sc.nextLong();
		long C=sc.nextLong();
		long K=sc.nextLong();
		long ans=0;
		if(K>A) {
			ans+=A;
			K-=A;
		}else {
			ans+=K;
			K=0;
		}
		if(K>0) {
			K-=B;
		}
		if(K>0) {
			if(K>C) {
				ans-=C;
			}else {
				ans-=K;
			}
		}
		System.out.println(ans);

}

} Main
import java.util.Scanner;
public class Main {
	static int[][] gyo2=new int[100000][12];
	static int count=0;
	@SuppressWarnings("resource")
	public static void main(String[] args) {
		Scanner sc=new Scanner(System.in);
		long A=sc.nextLong();
		long B=sc.nextLong();
		long C=sc.nextLong();
		long K=sc.nextLong();
		long ans=0;
		if(K>A) {
			ans+=A;
			K-=A;
		}else {
			ans+=K;
			K=0;
		}
		if(K>0) {
			K-=B;
		}
		if(K>0) {
			if(K>C) {
				ans-=C;
			}else {
				ans-=K;
			}
		}
		System.out.println(ans);

}

}
