import java.util.Scanner;


public class Main {

	public static void main(String[] args) {
		// TODO 自動生成されたメソッド・スタブ
		boolean judje = true;

		Scanner scan = new Scanner(System.in);

		int A = scan.nextInt();
		int B = scan.nextInt();
		int C = scan.nextInt();
		int K = scan.nextInt();
		int sum = 0;

		if( K < A)
			sum += K;
		else
			sum += A;

		K -= (A + B);

		if( K > 0){
			sum -= K;
		}
		System.out.println(sum);
	}

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

	public static void main(String[] args) {
		// TODO 自動生成されたメソッド・スタブ
		boolean judje = true;

		Scanner scan = new Scanner(System.in);

		int A = scan.nextInt();
		int B = scan.nextInt();
		int C = scan.nextInt();
		int K = scan.nextInt();
		int sum = 0;

		if( K < A)
			sum += K;
		else
			sum += A;

		K -= (A + B);

		if( K > 0){
			sum -= K;
		}
		System.out.println(sum);
	}

} Main public static void main(String[] args) {
		// TODO 自動生成されたメソッド・スタブ
		boolean judje = true;

		Scanner scan = new Scanner(System.in);

		int A = scan.nextInt();
		int B = scan.nextInt();
		int C = scan.nextInt();
		int K = scan.nextInt();
		int sum = 0;

		if( K < A)
			sum += K;
		else
			sum += A;

		K -= (A + B);

		if( K > 0){
			sum -= K;
		}
		System.out.println(sum);
	} main {
		// TODO 自動生成されたメソッド・スタブ
		boolean judje = true;

		Scanner scan = new Scanner(System.in);

		int A = scan.nextInt();
		int B = scan.nextInt();
		int C = scan.nextInt();
		int K = scan.nextInt();
		int sum = 0;

		if( K < A)
			sum += K;
		else
			sum += A;

		K -= (A + B);

		if( K > 0){
			sum -= K;
		}
		System.out.println(sum);
	} boolean judje = true; judje true Scanner scan = new Scanner(System.in); scan new Scanner(System.in) int A = scan.nextInt(); A scan.nextInt() scan.nextInt scan int B = scan.nextInt(); B scan.nextInt() scan.nextInt scan int C = scan.nextInt(); C scan.nextInt() scan.nextInt scan int K = scan.nextInt(); K scan.nextInt() scan.nextInt scan int sum = 0; sum 0 if( K < A)
			sum += K;
		else
			sum += A; K < A K A sum += K sum K sum += A sum A K -= (A + B) K (A + B) A B if( K > 0){
			sum -= K;
		} K > 0 K 0 {
			sum -= K;
		} sum -= K sum K System.out.println(sum) System.out.println System.out System System.out sum String[] args args public class Main {

	public static void main(String[] args) {
		// TODO 自動生成されたメソッド・スタブ
		boolean judje = true;

		Scanner scan = new Scanner(System.in);

		int A = scan.nextInt();
		int B = scan.nextInt();
		int C = scan.nextInt();
		int K = scan.nextInt();
		int sum = 0;

		if( K < A)
			sum += K;
		else
			sum += A;

		K -= (A + B);

		if( K > 0){
			sum -= K;
		}
		System.out.println(sum);
	}

} public class Main {

	public static void main(String[] args) {
		// TODO 自動生成されたメソッド・スタブ
		boolean judje = true;

		Scanner scan = new Scanner(System.in);

		int A = scan.nextInt();
		int B = scan.nextInt();
		int C = scan.nextInt();
		int K = scan.nextInt();
		int sum = 0;

		if( K < A)
			sum += K;
		else
			sum += A;

		K -= (A + B);

		if( K > 0){
			sum -= K;
		}
		System.out.println(sum);
	}

} Main
import java.util.Scanner;


public class Main {

	public static void main(String[] args) {
		// TODO 自動生成されたメソッド・スタブ
		boolean judje = true;

		Scanner scan = new Scanner(System.in);

		int A = scan.nextInt();
		int B = scan.nextInt();
		int C = scan.nextInt();
		int K = scan.nextInt();
		int sum = 0;

		if( K < A)
			sum += K;
		else
			sum += A;

		K -= (A + B);

		if( K > 0){
			sum -= K;
		}
		System.out.println(sum);
	}

}
