import java.util.Scanner;

public class Main {
	public static void main(String[] args) throws Exception {
		Scanner sc = new Scanner(System.in);
		int A = sc.nextInt();
		int B = sc.nextInt();
		int C = sc.nextInt();
		int K = sc.nextInt();
		int res = 0;
		res+=Math.min(A, K);
		K -= res;
		if(K==0) {
			System.out.println(res);
			System.exit(0);
		}
		K-=B;
		if(K>0) {
			res-=Math.min(K, C);
		}
		System.out.println(res);

	}
} import java.util.Scanner; Scanner java.util public class Main {
	public static void main(String[] args) throws Exception {
		Scanner sc = new Scanner(System.in);
		int A = sc.nextInt();
		int B = sc.nextInt();
		int C = sc.nextInt();
		int K = sc.nextInt();
		int res = 0;
		res+=Math.min(A, K);
		K -= res;
		if(K==0) {
			System.out.println(res);
			System.exit(0);
		}
		K-=B;
		if(K>0) {
			res-=Math.min(K, C);
		}
		System.out.println(res);

	}
} Main public static void main(String[] args) throws Exception {
		Scanner sc = new Scanner(System.in);
		int A = sc.nextInt();
		int B = sc.nextInt();
		int C = sc.nextInt();
		int K = sc.nextInt();
		int res = 0;
		res+=Math.min(A, K);
		K -= res;
		if(K==0) {
			System.out.println(res);
			System.exit(0);
		}
		K-=B;
		if(K>0) {
			res-=Math.min(K, C);
		}
		System.out.println(res);

	} main {
		Scanner sc = new Scanner(System.in);
		int A = sc.nextInt();
		int B = sc.nextInt();
		int C = sc.nextInt();
		int K = sc.nextInt();
		int res = 0;
		res+=Math.min(A, K);
		K -= res;
		if(K==0) {
			System.out.println(res);
			System.exit(0);
		}
		K-=B;
		if(K>0) {
			res-=Math.min(K, C);
		}
		System.out.println(res);

	} 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 res = 0; res 0 res+=Math.min(A, K) res Math.min(A, K) Math.min Math A K K -= res K res if(K==0) {
			System.out.println(res);
			System.exit(0);
		} K==0 K 0 {
			System.out.println(res);
			System.exit(0);
		} System.out.println(res) System.out.println System.out System System.out res System.exit(0) System.exit System 0 K-=B K B if(K>0) {
			res-=Math.min(K, C);
		} K>0 K 0 {
			res-=Math.min(K, C);
		} res-=Math.min(K, C) res Math.min(K, C) Math.min Math K C System.out.println(res) System.out.println System.out System System.out res String[] args args public class Main {
	public static void main(String[] args) throws Exception {
		Scanner sc = new Scanner(System.in);
		int A = sc.nextInt();
		int B = sc.nextInt();
		int C = sc.nextInt();
		int K = sc.nextInt();
		int res = 0;
		res+=Math.min(A, K);
		K -= res;
		if(K==0) {
			System.out.println(res);
			System.exit(0);
		}
		K-=B;
		if(K>0) {
			res-=Math.min(K, C);
		}
		System.out.println(res);

	}
} public class Main {
	public static void main(String[] args) throws Exception {
		Scanner sc = new Scanner(System.in);
		int A = sc.nextInt();
		int B = sc.nextInt();
		int C = sc.nextInt();
		int K = sc.nextInt();
		int res = 0;
		res+=Math.min(A, K);
		K -= res;
		if(K==0) {
			System.out.println(res);
			System.exit(0);
		}
		K-=B;
		if(K>0) {
			res-=Math.min(K, C);
		}
		System.out.println(res);

	}
} Main
import java.util.Scanner;

public class Main {
	public static void main(String[] args) throws Exception {
		Scanner sc = new Scanner(System.in);
		int A = sc.nextInt();
		int B = sc.nextInt();
		int C = sc.nextInt();
		int K = sc.nextInt();
		int res = 0;
		res+=Math.min(A, K);
		K -= res;
		if(K==0) {
			System.out.println(res);
			System.exit(0);
		}
		K-=B;
		if(K>0) {
			res-=Math.min(K, C);
		}
		System.out.println(res);

	}
}