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 score = 0;
		
		while(a > 0 && k > 0) {
			score++;
			k -= 1;
			a -= 1;
		}
		while(b > 0 && k > 0) {
			k -= 1;
			b -= 1;
		}
		while(c > 0 && k > 0) {
			score--;
			k -= 1;
			c -= 1;
		}
		System.out.println(score);
	}

} 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 score = 0;
		
		while(a > 0 && k > 0) {
			score++;
			k -= 1;
			a -= 1;
		}
		while(b > 0 && k > 0) {
			k -= 1;
			b -= 1;
		}
		while(c > 0 && k > 0) {
			score--;
			k -= 1;
			c -= 1;
		}
		System.out.println(score);
	}

} 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 score = 0;
		
		while(a > 0 && k > 0) {
			score++;
			k -= 1;
			a -= 1;
		}
		while(b > 0 && k > 0) {
			k -= 1;
			b -= 1;
		}
		while(c > 0 && k > 0) {
			score--;
			k -= 1;
			c -= 1;
		}
		System.out.println(score);
	} main {
		Scanner sc = new Scanner(System.in);
		int a = sc.nextInt();
		int b = sc.nextInt();
		int c = sc.nextInt();
		int k = sc.nextInt();
		int score = 0;
		
		while(a > 0 && k > 0) {
			score++;
			k -= 1;
			a -= 1;
		}
		while(b > 0 && k > 0) {
			k -= 1;
			b -= 1;
		}
		while(c > 0 && k > 0) {
			score--;
			k -= 1;
			c -= 1;
		}
		System.out.println(score);
	} 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 score = 0; score 0 while(a > 0 && k > 0) {
			score++;
			k -= 1;
			a -= 1;
		} a > 0 && k > 0 a > 0 a 0 k > 0 k 0 {
			score++;
			k -= 1;
			a -= 1;
		} score++ score k -= 1 k 1 a -= 1 a 1 while(b > 0 && k > 0) {
			k -= 1;
			b -= 1;
		} b > 0 && k > 0 b > 0 b 0 k > 0 k 0 {
			k -= 1;
			b -= 1;
		} k -= 1 k 1 b -= 1 b 1 while(c > 0 && k > 0) {
			score--;
			k -= 1;
			c -= 1;
		} c > 0 && k > 0 c > 0 c 0 k > 0 k 0 {
			score--;
			k -= 1;
			c -= 1;
		} score-- score k -= 1 k 1 c -= 1 c 1 System.out.println(score) System.out.println System.out System System.out score 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 score = 0;
		
		while(a > 0 && k > 0) {
			score++;
			k -= 1;
			a -= 1;
		}
		while(b > 0 && k > 0) {
			k -= 1;
			b -= 1;
		}
		while(c > 0 && k > 0) {
			score--;
			k -= 1;
			c -= 1;
		}
		System.out.println(score);
	}

} 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 score = 0;
		
		while(a > 0 && k > 0) {
			score++;
			k -= 1;
			a -= 1;
		}
		while(b > 0 && k > 0) {
			k -= 1;
			b -= 1;
		}
		while(c > 0 && k > 0) {
			score--;
			k -= 1;
			c -= 1;
		}
		System.out.println(score);
	}

} 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 score = 0;
		
		while(a > 0 && k > 0) {
			score++;
			k -= 1;
			a -= 1;
		}
		while(b > 0 && k > 0) {
			k -= 1;
			b -= 1;
		}
		while(c > 0 && k > 0) {
			score--;
			k -= 1;
			c -= 1;
		}
		System.out.println(score);
	}

}
