import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int A = sc.nextInt();
		int B = sc.nextInt();
		int C = sc.nextInt();
		int K = sc.nextInt();
		int ak = Math.min(K, A);
		K -= ak;
		int bk = Math.min(K, B);
		K -= bk;
		int ck = K;
		System.out.println(ak - ck);
		sc.close();
	}
} import java.util.Scanner; Scanner java.util public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int A = sc.nextInt();
		int B = sc.nextInt();
		int C = sc.nextInt();
		int K = sc.nextInt();
		int ak = Math.min(K, A);
		K -= ak;
		int bk = Math.min(K, B);
		K -= bk;
		int ck = K;
		System.out.println(ak - ck);
		sc.close();
	}
} Main public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int A = sc.nextInt();
		int B = sc.nextInt();
		int C = sc.nextInt();
		int K = sc.nextInt();
		int ak = Math.min(K, A);
		K -= ak;
		int bk = Math.min(K, B);
		K -= bk;
		int ck = K;
		System.out.println(ak - ck);
		sc.close();
	} main {
		Scanner sc = new Scanner(System.in);
		int A = sc.nextInt();
		int B = sc.nextInt();
		int C = sc.nextInt();
		int K = sc.nextInt();
		int ak = Math.min(K, A);
		K -= ak;
		int bk = Math.min(K, B);
		K -= bk;
		int ck = K;
		System.out.println(ak - ck);
		sc.close();
	} Scanner sc = new Scanner(System.in); sc new Scanner(System.in) 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 ak = Math.min(K, A); ak Math.min(K, A) Math.min Math K A K -= ak K ak int bk = Math.min(K, B); bk Math.min(K, B) Math.min Math K B K -= bk K bk int ck = K; ck K System.out.println(ak - ck) System.out.println System.out System System.out ak - ck ak ck sc.close() sc.close sc String[] args args public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int A = sc.nextInt();
		int B = sc.nextInt();
		int C = sc.nextInt();
		int K = sc.nextInt();
		int ak = Math.min(K, A);
		K -= ak;
		int bk = Math.min(K, B);
		K -= bk;
		int ck = K;
		System.out.println(ak - ck);
		sc.close();
	}
} public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int A = sc.nextInt();
		int B = sc.nextInt();
		int C = sc.nextInt();
		int K = sc.nextInt();
		int ak = Math.min(K, A);
		K -= ak;
		int bk = Math.min(K, B);
		K -= bk;
		int ck = K;
		System.out.println(ak - ck);
		sc.close();
	}
} Main
import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int A = sc.nextInt();
		int B = sc.nextInt();
		int C = sc.nextInt();
		int K = sc.nextInt();
		int ak = Math.min(K, A);
		K -= ak;
		int bk = Math.min(K, B);
		K -= bk;
		int ck = K;
		System.out.println(ak - ck);
		sc.close();
	}
}