import java.util.Scanner;

public class Main {
	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();
		sc.close();
		long point=0l;
		
		if((A>=K)&&K!=0l) {
			point=K;
			K=0l;
		}else {
			point+=A;
			K-=A;
		}
		if((B>=K)) {
			K=0l;
		}else if((B<K)&&K!=0l) {
			K-=B;
		}
		if(K!=0l)
			point-=K;
		System.out.println(point);
	}
} import java.util.Scanner; Scanner java.util public class Main {
	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();
		sc.close();
		long point=0l;
		
		if((A>=K)&&K!=0l) {
			point=K;
			K=0l;
		}else {
			point+=A;
			K-=A;
		}
		if((B>=K)) {
			K=0l;
		}else if((B<K)&&K!=0l) {
			K-=B;
		}
		if(K!=0l)
			point-=K;
		System.out.println(point);
	}
} Main 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();
		sc.close();
		long point=0l;
		
		if((A>=K)&&K!=0l) {
			point=K;
			K=0l;
		}else {
			point+=A;
			K-=A;
		}
		if((B>=K)) {
			K=0l;
		}else if((B<K)&&K!=0l) {
			K-=B;
		}
		if(K!=0l)
			point-=K;
		System.out.println(point);
	} main {
		Scanner sc = new Scanner(System.in);
		Long A=sc.nextLong();
		Long B=sc.nextLong();
		Long C=sc.nextLong();
		Long K=sc.nextLong();
		sc.close();
		long point=0l;
		
		if((A>=K)&&K!=0l) {
			point=K;
			K=0l;
		}else {
			point+=A;
			K-=A;
		}
		if((B>=K)) {
			K=0l;
		}else if((B<K)&&K!=0l) {
			K-=B;
		}
		if(K!=0l)
			point-=K;
		System.out.println(point);
	} 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 sc.close() sc.close sc long point=0l; point 0l if((A>=K)&&K!=0l) {
			point=K;
			K=0l;
		}else {
			point+=A;
			K-=A;
		} (A>=K)&&K!=0l (A>=K) A K K!=0l K 0l {
			point=K;
			K=0l;
		} point=K point K K=0l K 0l {
			point+=A;
			K-=A;
		} point+=A point A K-=A K A if((B>=K)) {
			K=0l;
		}else if((B<K)&&K!=0l) {
			K-=B;
		} (B>=K) B K {
			K=0l;
		} K=0l K 0l if((B<K)&&K!=0l) {
			K-=B;
		} (B<K)&&K!=0l (B<K) B K K!=0l K 0l {
			K-=B;
		} K-=B K B if(K!=0l)
			point-=K; K!=0l K 0l point-=K point K System.out.println(point) System.out.println System.out System System.out point String[] args args public class Main {
	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();
		sc.close();
		long point=0l;
		
		if((A>=K)&&K!=0l) {
			point=K;
			K=0l;
		}else {
			point+=A;
			K-=A;
		}
		if((B>=K)) {
			K=0l;
		}else if((B<K)&&K!=0l) {
			K-=B;
		}
		if(K!=0l)
			point-=K;
		System.out.println(point);
	}
} public class Main {
	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();
		sc.close();
		long point=0l;
		
		if((A>=K)&&K!=0l) {
			point=K;
			K=0l;
		}else {
			point+=A;
			K-=A;
		}
		if((B>=K)) {
			K=0l;
		}else if((B<K)&&K!=0l) {
			K-=B;
		}
		if(K!=0l)
			point-=K;
		System.out.println(point);
	}
} Main
import java.util.Scanner;

public class Main {
	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();
		sc.close();
		long point=0l;
		
		if((A>=K)&&K!=0l) {
			point=K;
			K=0l;
		}else {
			point+=A;
			K-=A;
		}
		if((B>=K)) {
			K=0l;
		}else if((B<K)&&K!=0l) {
			K-=B;
		}
		if(K!=0l)
			point-=K;
		System.out.println(point);
	}
}
