import java.util.*;
 
public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		long a = sc.nextLong(); // 1
		long b = sc.nextLong(); // 0
		long c = sc.nextLong(); // -1
		long k = sc.nextLong();

		if (a >= k) {
			System.out.println(k);
		} else if (a + b >= k) {
			System.out.println(a);
		} else {
			long ans = a + b - k + a;
			System.out.println(ans);
		}
	}
}
 
 
 
 
 
 
 
 
 
  import java.util.*; util.* java public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		long a = sc.nextLong(); // 1
		long b = sc.nextLong(); // 0
		long c = sc.nextLong(); // -1
		long k = sc.nextLong();

		if (a >= k) {
			System.out.println(k);
		} else if (a + b >= k) {
			System.out.println(a);
		} else {
			long ans = a + b - k + a;
			System.out.println(ans);
		}
	}
} Main public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		long a = sc.nextLong(); // 1
		long b = sc.nextLong(); // 0
		long c = sc.nextLong(); // -1
		long k = sc.nextLong();

		if (a >= k) {
			System.out.println(k);
		} else if (a + b >= k) {
			System.out.println(a);
		} else {
			long ans = a + b - k + a;
			System.out.println(ans);
		}
	} main {
		Scanner sc = new Scanner(System.in);
		long a = sc.nextLong(); // 1
		long b = sc.nextLong(); // 0
		long c = sc.nextLong(); // -1
		long k = sc.nextLong();

		if (a >= k) {
			System.out.println(k);
		} else if (a + b >= k) {
			System.out.println(a);
		} else {
			long ans = a + b - k + a;
			System.out.println(ans);
		}
	} 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 if (a >= k) {
			System.out.println(k);
		} else if (a + b >= k) {
			System.out.println(a);
		} else {
			long ans = a + b - k + a;
			System.out.println(ans);
		} a >= k a k {
			System.out.println(k);
		} System.out.println(k) System.out.println System.out System System.out k if (a + b >= k) {
			System.out.println(a);
		} else {
			long ans = a + b - k + a;
			System.out.println(ans);
		} a + b >= k a + b a b k {
			System.out.println(a);
		} System.out.println(a) System.out.println System.out System System.out a {
			long ans = a + b - k + a;
			System.out.println(ans);
		} long ans = a + b - k + a; ans a + b - k + a a + b - k a + b a b k a 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) {
		Scanner sc = new Scanner(System.in);
		long a = sc.nextLong(); // 1
		long b = sc.nextLong(); // 0
		long c = sc.nextLong(); // -1
		long k = sc.nextLong();

		if (a >= k) {
			System.out.println(k);
		} else if (a + b >= k) {
			System.out.println(a);
		} else {
			long ans = a + b - k + a;
			System.out.println(ans);
		}
	}
} public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		long a = sc.nextLong(); // 1
		long b = sc.nextLong(); // 0
		long c = sc.nextLong(); // -1
		long k = sc.nextLong();

		if (a >= k) {
			System.out.println(k);
		} else if (a + b >= k) {
			System.out.println(a);
		} else {
			long ans = a + b - k + a;
			System.out.println(ans);
		}
	}
} Main
import java.util.*;
 
public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		long a = sc.nextLong(); // 1
		long b = sc.nextLong(); // 0
		long c = sc.nextLong(); // -1
		long k = sc.nextLong();

		if (a >= k) {
			System.out.println(k);
		} else if (a + b >= k) {
			System.out.println(a);
		} else {
			long ans = a + b - k + a;
			System.out.println(ans);
		}
	}
}
 
 
 
 
 
 
 
 
 
 
