import java.util.Scanner;
//import java.util.Arrays;
public class Main{
	public static void main(String[] args){
		Scanner sc = new Scanner(System.in);
		//
		long a = sc.nextLong();
		long b = sc.nextLong();
		long c = sc.nextLong();
		long k = sc.nextLong();
		long result = 0;
		if(k >= a){
			result += a;
			if(k > a + b){
				result -= k - a - b;
			}
		}else{
			result += k;
		}
		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);
		//
		long a = sc.nextLong();
		long b = sc.nextLong();
		long c = sc.nextLong();
		long k = sc.nextLong();
		long result = 0;
		if(k >= a){
			result += a;
			if(k > a + b){
				result -= k - a - b;
			}
		}else{
			result += k;
		}
		System.out.println(result);
	}
} Main public static void main(String[] args){
		Scanner sc = new Scanner(System.in);
		//
		long a = sc.nextLong();
		long b = sc.nextLong();
		long c = sc.nextLong();
		long k = sc.nextLong();
		long result = 0;
		if(k >= a){
			result += a;
			if(k > a + b){
				result -= k - a - b;
			}
		}else{
			result += k;
		}
		System.out.println(result);
	} main {
		Scanner sc = new Scanner(System.in);
		//
		long a = sc.nextLong();
		long b = sc.nextLong();
		long c = sc.nextLong();
		long k = sc.nextLong();
		long result = 0;
		if(k >= a){
			result += a;
			if(k > a + b){
				result -= k - a - b;
			}
		}else{
			result += k;
		}
		System.out.println(result);
	} Scanner sc = new Scanner(System.in); sc new Scanner(System.in) long a = sc.nextLong(); a sc.nextLong() sc.nextLong sc long b = sc.nextLong(); b sc.nextLong() sc.nextLong sc long c = sc.nextLong(); c sc.nextLong() sc.nextLong sc long k = sc.nextLong(); k sc.nextLong() sc.nextLong sc long result = 0; result 0 if(k >= a){
			result += a;
			if(k > a + b){
				result -= k - a - b;
			}
		}else{
			result += k;
		} k >= a k a {
			result += a;
			if(k > a + b){
				result -= k - a - b;
			}
		} result += a result a if(k > a + b){
				result -= k - a - b;
			} k > a + b k a + b a b {
				result -= k - a - b;
			} result -= k - a - b result k - a - b k - a - b k a b {
			result += k;
		} result += k result k 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);
		//
		long a = sc.nextLong();
		long b = sc.nextLong();
		long c = sc.nextLong();
		long k = sc.nextLong();
		long result = 0;
		if(k >= a){
			result += a;
			if(k > a + b){
				result -= k - a - b;
			}
		}else{
			result += k;
		}
		System.out.println(result);
	}
} public class Main{
	public static void main(String[] args){
		Scanner sc = new Scanner(System.in);
		//
		long a = sc.nextLong();
		long b = sc.nextLong();
		long c = sc.nextLong();
		long k = sc.nextLong();
		long result = 0;
		if(k >= a){
			result += a;
			if(k > a + b){
				result -= k - a - b;
			}
		}else{
			result += k;
		}
		System.out.println(result);
	}
} Main
import java.util.Scanner;
//import java.util.Arrays;
public class Main{
	public static void main(String[] args){
		Scanner sc = new Scanner(System.in);
		//
		long a = sc.nextLong();
		long b = sc.nextLong();
		long c = sc.nextLong();
		long k = sc.nextLong();
		long result = 0;
		if(k >= a){
			result += a;
			if(k > a + b){
				result -= k - a - b;
			}
		}else{
			result += k;
		}
		System.out.println(result);
	}
}
