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 max = 0;
		if(a >= k){
			max = 1 * k;
		}else if(b > k - a){
			max = a;
		}else{
			max = a - (k - a - b);
		}
		System.out.println(max);
	}

} 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 max = 0;
		if(a >= k){
			max = 1 * k;
		}else if(b > k - a){
			max = a;
		}else{
			max = a - (k - a - b);
		}
		System.out.println(max);
	}

} 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 max = 0;
		if(a >= k){
			max = 1 * k;
		}else if(b > k - a){
			max = a;
		}else{
			max = a - (k - a - b);
		}
		System.out.println(max);
	} main {
		Scanner sc = new Scanner(System.in);
		int a = sc.nextInt();
		int b = sc.nextInt();
		int c = sc.nextInt();
		int k = sc.nextInt();
		int max = 0;
		if(a >= k){
			max = 1 * k;
		}else if(b > k - a){
			max = a;
		}else{
			max = a - (k - a - b);
		}
		System.out.println(max);
	} 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 max = 0; max 0 if(a >= k){
			max = 1 * k;
		}else if(b > k - a){
			max = a;
		}else{
			max = a - (k - a - b);
		} a >= k a k {
			max = 1 * k;
		} max = 1 * k max 1 * k 1 k if(b > k - a){
			max = a;
		}else{
			max = a - (k - a - b);
		} b > k - a b k - a k a {
			max = a;
		} max = a max a {
			max = a - (k - a - b);
		} max = a - (k - a - b) max a - (k - a - b) a (k - a - b) k - a - b k a b System.out.println(max) System.out.println System.out System System.out max 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 max = 0;
		if(a >= k){
			max = 1 * k;
		}else if(b > k - a){
			max = a;
		}else{
			max = a - (k - a - b);
		}
		System.out.println(max);
	}

} 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 max = 0;
		if(a >= k){
			max = 1 * k;
		}else if(b > k - a){
			max = a;
		}else{
			max = a - (k - a - b);
		}
		System.out.println(max);
	}

} 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 max = 0;
		if(a >= k){
			max = 1 * k;
		}else if(b > k - a){
			max = a;
		}else{
			max = a - (k - a - b);
		}
		System.out.println(max);
	}

}