import java.util.Scanner;
public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		Integer a = sc.nextInt();
		Integer b = sc.nextInt();
		Integer c = sc.nextInt();
		Integer k = sc.nextInt();
		
		Integer result = 0;
		if(k <= a) {
			result = k;
		} 
		else if(k <= a+b) {
			result = a;
		}
		else{
			result = a - (k - a - b);
		}
		System.out.println(result);
	}
} import java.util.Scanner; Scanner java.util public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		Integer a = sc.nextInt();
		Integer b = sc.nextInt();
		Integer c = sc.nextInt();
		Integer k = sc.nextInt();
		
		Integer result = 0;
		if(k <= a) {
			result = k;
		} 
		else if(k <= a+b) {
			result = a;
		}
		else{
			result = a - (k - a - b);
		}
		System.out.println(result);
	}
} Main public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		Integer a = sc.nextInt();
		Integer b = sc.nextInt();
		Integer c = sc.nextInt();
		Integer k = sc.nextInt();
		
		Integer result = 0;
		if(k <= a) {
			result = k;
		} 
		else if(k <= a+b) {
			result = a;
		}
		else{
			result = a - (k - a - b);
		}
		System.out.println(result);
	} main {
		Scanner sc = new Scanner(System.in);
		Integer a = sc.nextInt();
		Integer b = sc.nextInt();
		Integer c = sc.nextInt();
		Integer k = sc.nextInt();
		
		Integer result = 0;
		if(k <= a) {
			result = k;
		} 
		else if(k <= a+b) {
			result = a;
		}
		else{
			result = a - (k - a - b);
		}
		System.out.println(result);
	} Scanner sc = new Scanner(System.in); sc new Scanner(System.in) Integer a = sc.nextInt(); a sc.nextInt() sc.nextInt sc Integer b = sc.nextInt(); b sc.nextInt() sc.nextInt sc Integer c = sc.nextInt(); c sc.nextInt() sc.nextInt sc Integer k = sc.nextInt(); k sc.nextInt() sc.nextInt sc Integer result = 0; result 0 if(k <= a) {
			result = k;
		} 
		else if(k <= a+b) {
			result = a;
		}
		else{
			result = a - (k - a - b);
		} k <= a k a {
			result = k;
		} result = k result k if(k <= a+b) {
			result = a;
		}
		else{
			result = a - (k - a - b);
		} k <= a+b k a+b a b {
			result = a;
		} result = a result a {
			result = a - (k - a - b);
		} result = a - (k - a - b) result a - (k - a - b) a (k - a - b) k - a - b k a b System.out.println(result) System.out.println System.out System System.out result String[] args args public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		Integer a = sc.nextInt();
		Integer b = sc.nextInt();
		Integer c = sc.nextInt();
		Integer k = sc.nextInt();
		
		Integer result = 0;
		if(k <= a) {
			result = k;
		} 
		else if(k <= a+b) {
			result = a;
		}
		else{
			result = a - (k - a - b);
		}
		System.out.println(result);
	}
} public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		Integer a = sc.nextInt();
		Integer b = sc.nextInt();
		Integer c = sc.nextInt();
		Integer k = sc.nextInt();
		
		Integer result = 0;
		if(k <= a) {
			result = k;
		} 
		else if(k <= a+b) {
			result = a;
		}
		else{
			result = a - (k - a - b);
		}
		System.out.println(result);
	}
} Main
import java.util.Scanner;
public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		Integer a = sc.nextInt();
		Integer b = sc.nextInt();
		Integer c = sc.nextInt();
		Integer k = sc.nextInt();
		
		Integer result = 0;
		if(k <= a) {
			result = k;
		} 
		else if(k <= a+b) {
			result = a;
		}
		else{
			result = a - (k - a - b);
		}
		System.out.println(result);
	}
}