
import java.util.Scanner;

public class Main  {

	public static  void main(String[] args) {
		int A , B , C , K;
	int T = 0 ;
	Scanner sc = new Scanner(System.in);
	A = Integer.parseInt(sc.next());
	B = Integer.parseInt(sc.next());
	C = Integer.parseInt(sc.next());
	K = Integer.parseInt(sc.next());
	while(K != 0 && A != 0) {
		T++;
		A--;
		K--;
	}while(K != 0 && B != 0) {
		B--;
		K--;
	}while(K != 0 && C != 0) {
		T--;
		C--;
		K--;
	}
	System.out.println(T);
	}

} import java.util.Scanner; Scanner java.util public class Main  {

	public static  void main(String[] args) {
		int A , B , C , K;
	int T = 0 ;
	Scanner sc = new Scanner(System.in);
	A = Integer.parseInt(sc.next());
	B = Integer.parseInt(sc.next());
	C = Integer.parseInt(sc.next());
	K = Integer.parseInt(sc.next());
	while(K != 0 && A != 0) {
		T++;
		A--;
		K--;
	}while(K != 0 && B != 0) {
		B--;
		K--;
	}while(K != 0 && C != 0) {
		T--;
		C--;
		K--;
	}
	System.out.println(T);
	}

} Main public static  void main(String[] args) {
		int A , B , C , K;
	int T = 0 ;
	Scanner sc = new Scanner(System.in);
	A = Integer.parseInt(sc.next());
	B = Integer.parseInt(sc.next());
	C = Integer.parseInt(sc.next());
	K = Integer.parseInt(sc.next());
	while(K != 0 && A != 0) {
		T++;
		A--;
		K--;
	}while(K != 0 && B != 0) {
		B--;
		K--;
	}while(K != 0 && C != 0) {
		T--;
		C--;
		K--;
	}
	System.out.println(T);
	} main {
		int A , B , C , K;
	int T = 0 ;
	Scanner sc = new Scanner(System.in);
	A = Integer.parseInt(sc.next());
	B = Integer.parseInt(sc.next());
	C = Integer.parseInt(sc.next());
	K = Integer.parseInt(sc.next());
	while(K != 0 && A != 0) {
		T++;
		A--;
		K--;
	}while(K != 0 && B != 0) {
		B--;
		K--;
	}while(K != 0 && C != 0) {
		T--;
		C--;
		K--;
	}
	System.out.println(T);
	} int A A B B C C K; K int T = 0 ; T 0 Scanner sc = new Scanner(System.in); sc new Scanner(System.in) A = Integer.parseInt(sc.next()) A Integer.parseInt(sc.next()) Integer.parseInt Integer sc.next() sc.next sc B = Integer.parseInt(sc.next()) B Integer.parseInt(sc.next()) Integer.parseInt Integer sc.next() sc.next sc C = Integer.parseInt(sc.next()) C Integer.parseInt(sc.next()) Integer.parseInt Integer sc.next() sc.next sc K = Integer.parseInt(sc.next()) K Integer.parseInt(sc.next()) Integer.parseInt Integer sc.next() sc.next sc while(K != 0 && A != 0) {
		T++;
		A--;
		K--;
	} K != 0 && A != 0 K != 0 K 0 A != 0 A 0 {
		T++;
		A--;
		K--;
	} T++ T A-- A K-- K while(K != 0 && B != 0) {
		B--;
		K--;
	} K != 0 && B != 0 K != 0 K 0 B != 0 B 0 {
		B--;
		K--;
	} B-- B K-- K while(K != 0 && C != 0) {
		T--;
		C--;
		K--;
	} K != 0 && C != 0 K != 0 K 0 C != 0 C 0 {
		T--;
		C--;
		K--;
	} T-- T C-- C K-- K System.out.println(T) System.out.println System.out System System.out T String[] args args public class Main  {

	public static  void main(String[] args) {
		int A , B , C , K;
	int T = 0 ;
	Scanner sc = new Scanner(System.in);
	A = Integer.parseInt(sc.next());
	B = Integer.parseInt(sc.next());
	C = Integer.parseInt(sc.next());
	K = Integer.parseInt(sc.next());
	while(K != 0 && A != 0) {
		T++;
		A--;
		K--;
	}while(K != 0 && B != 0) {
		B--;
		K--;
	}while(K != 0 && C != 0) {
		T--;
		C--;
		K--;
	}
	System.out.println(T);
	}

} public class Main  {

	public static  void main(String[] args) {
		int A , B , C , K;
	int T = 0 ;
	Scanner sc = new Scanner(System.in);
	A = Integer.parseInt(sc.next());
	B = Integer.parseInt(sc.next());
	C = Integer.parseInt(sc.next());
	K = Integer.parseInt(sc.next());
	while(K != 0 && A != 0) {
		T++;
		A--;
		K--;
	}while(K != 0 && B != 0) {
		B--;
		K--;
	}while(K != 0 && C != 0) {
		T--;
		C--;
		K--;
	}
	System.out.println(T);
	}

} Main

import java.util.Scanner;

public class Main  {

	public static  void main(String[] args) {
		int A , B , C , K;
	int T = 0 ;
	Scanner sc = new Scanner(System.in);
	A = Integer.parseInt(sc.next());
	B = Integer.parseInt(sc.next());
	C = Integer.parseInt(sc.next());
	K = Integer.parseInt(sc.next());
	while(K != 0 && A != 0) {
		T++;
		A--;
		K--;
	}while(K != 0 && B != 0) {
		B--;
		K--;
	}while(K != 0 && C != 0) {
		T--;
		C--;
		K--;
	}
	System.out.println(T);
	}

}
