import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		// TODO 自動生成されたメソッド・スタブ
		Scanner scan = new Scanner(System.in);
		
		long a = scan.nextLong();
		long b = scan.nextLong();
		long c = scan.nextLong();
		long k = scan.nextLong();
		long ans = 0;
		
		if(k <= a) {
			ans = k;
		}
		else if(k <= a + b) {
			ans = a;
		}
		else {
			ans = a - (k - a - b);
		}
		
		
		System.out.println(ans);

	}

} import java.util.Scanner; Scanner java.util public class Main {

	public static void main(String[] args) {
		// TODO 自動生成されたメソッド・スタブ
		Scanner scan = new Scanner(System.in);
		
		long a = scan.nextLong();
		long b = scan.nextLong();
		long c = scan.nextLong();
		long k = scan.nextLong();
		long ans = 0;
		
		if(k <= a) {
			ans = k;
		}
		else if(k <= a + b) {
			ans = a;
		}
		else {
			ans = a - (k - a - b);
		}
		
		
		System.out.println(ans);

	}

} Main public static void main(String[] args) {
		// TODO 自動生成されたメソッド・スタブ
		Scanner scan = new Scanner(System.in);
		
		long a = scan.nextLong();
		long b = scan.nextLong();
		long c = scan.nextLong();
		long k = scan.nextLong();
		long ans = 0;
		
		if(k <= a) {
			ans = k;
		}
		else if(k <= a + b) {
			ans = a;
		}
		else {
			ans = a - (k - a - b);
		}
		
		
		System.out.println(ans);

	} main {
		// TODO 自動生成されたメソッド・スタブ
		Scanner scan = new Scanner(System.in);
		
		long a = scan.nextLong();
		long b = scan.nextLong();
		long c = scan.nextLong();
		long k = scan.nextLong();
		long ans = 0;
		
		if(k <= a) {
			ans = k;
		}
		else if(k <= a + b) {
			ans = a;
		}
		else {
			ans = a - (k - a - b);
		}
		
		
		System.out.println(ans);

	} Scanner scan = new Scanner(System.in); scan new Scanner(System.in) long a = scan.nextLong(); a scan.nextLong() scan.nextLong scan long b = scan.nextLong(); b scan.nextLong() scan.nextLong scan long c = scan.nextLong(); c scan.nextLong() scan.nextLong scan long k = scan.nextLong(); k scan.nextLong() scan.nextLong scan long ans = 0; ans 0 if(k <= a) {
			ans = k;
		}
		else if(k <= a + b) {
			ans = a;
		}
		else {
			ans = a - (k - a - b);
		} k <= a k a {
			ans = k;
		} ans = k ans k if(k <= a + b) {
			ans = a;
		}
		else {
			ans = a - (k - a - b);
		} k <= a + b k a + b a b {
			ans = a;
		} ans = a ans a {
			ans = a - (k - a - b);
		} ans = a - (k - a - b) ans a - (k - a - b) a (k - a - b) k - a - b k a b System.out.println(ans) System.out.println System.out System System.out ans String[] args args public class Main {

	public static void main(String[] args) {
		// TODO 自動生成されたメソッド・スタブ
		Scanner scan = new Scanner(System.in);
		
		long a = scan.nextLong();
		long b = scan.nextLong();
		long c = scan.nextLong();
		long k = scan.nextLong();
		long ans = 0;
		
		if(k <= a) {
			ans = k;
		}
		else if(k <= a + b) {
			ans = a;
		}
		else {
			ans = a - (k - a - b);
		}
		
		
		System.out.println(ans);

	}

} public class Main {

	public static void main(String[] args) {
		// TODO 自動生成されたメソッド・スタブ
		Scanner scan = new Scanner(System.in);
		
		long a = scan.nextLong();
		long b = scan.nextLong();
		long c = scan.nextLong();
		long k = scan.nextLong();
		long ans = 0;
		
		if(k <= a) {
			ans = k;
		}
		else if(k <= a + b) {
			ans = a;
		}
		else {
			ans = a - (k - a - b);
		}
		
		
		System.out.println(ans);

	}

} Main
import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		// TODO 自動生成されたメソッド・スタブ
		Scanner scan = new Scanner(System.in);
		
		long a = scan.nextLong();
		long b = scan.nextLong();
		long c = scan.nextLong();
		long k = scan.nextLong();
		long ans = 0;
		
		if(k <= a) {
			ans = k;
		}
		else if(k <= a + b) {
			ans = a;
		}
		else {
			ans = a - (k - a - b);
		}
		
		
		System.out.println(ans);

	}

}
