import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner kb = new Scanner(System.in);
		int a = kb.nextInt();
		int b = kb.nextInt();
		int c = kb.nextInt();
		int k = kb.nextInt();
		int count = 0;
		if (k <= a) {
			count += k;
		} else {
			count += a;
			k -= a;
			if (k <= b) {
			} else {
				k -= b;
				if (k <= c) {
					count -= k;
				} else {
					count -= c;
					k -= c;
				}
			}
		}

		System.out.println(count);
		kb.close();
	}
} import java.util.Scanner; Scanner java.util public class Main {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner kb = new Scanner(System.in);
		int a = kb.nextInt();
		int b = kb.nextInt();
		int c = kb.nextInt();
		int k = kb.nextInt();
		int count = 0;
		if (k <= a) {
			count += k;
		} else {
			count += a;
			k -= a;
			if (k <= b) {
			} else {
				k -= b;
				if (k <= c) {
					count -= k;
				} else {
					count -= c;
					k -= c;
				}
			}
		}

		System.out.println(count);
		kb.close();
	}
} Main public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner kb = new Scanner(System.in);
		int a = kb.nextInt();
		int b = kb.nextInt();
		int c = kb.nextInt();
		int k = kb.nextInt();
		int count = 0;
		if (k <= a) {
			count += k;
		} else {
			count += a;
			k -= a;
			if (k <= b) {
			} else {
				k -= b;
				if (k <= c) {
					count -= k;
				} else {
					count -= c;
					k -= c;
				}
			}
		}

		System.out.println(count);
		kb.close();
	} main {
		// TODO Auto-generated method stub
		Scanner kb = new Scanner(System.in);
		int a = kb.nextInt();
		int b = kb.nextInt();
		int c = kb.nextInt();
		int k = kb.nextInt();
		int count = 0;
		if (k <= a) {
			count += k;
		} else {
			count += a;
			k -= a;
			if (k <= b) {
			} else {
				k -= b;
				if (k <= c) {
					count -= k;
				} else {
					count -= c;
					k -= c;
				}
			}
		}

		System.out.println(count);
		kb.close();
	} Scanner kb = new Scanner(System.in); kb new Scanner(System.in) int a = kb.nextInt(); a kb.nextInt() kb.nextInt kb int b = kb.nextInt(); b kb.nextInt() kb.nextInt kb int c = kb.nextInt(); c kb.nextInt() kb.nextInt kb int k = kb.nextInt(); k kb.nextInt() kb.nextInt kb int count = 0; count 0 if (k <= a) {
			count += k;
		} else {
			count += a;
			k -= a;
			if (k <= b) {
			} else {
				k -= b;
				if (k <= c) {
					count -= k;
				} else {
					count -= c;
					k -= c;
				}
			}
		} k <= a k a {
			count += k;
		} count += k count k {
			count += a;
			k -= a;
			if (k <= b) {
			} else {
				k -= b;
				if (k <= c) {
					count -= k;
				} else {
					count -= c;
					k -= c;
				}
			}
		} count += a count a k -= a k a if (k <= b) {
			} else {
				k -= b;
				if (k <= c) {
					count -= k;
				} else {
					count -= c;
					k -= c;
				}
			} k <= b k b {
			} {
				k -= b;
				if (k <= c) {
					count -= k;
				} else {
					count -= c;
					k -= c;
				}
			} k -= b k b if (k <= c) {
					count -= k;
				} else {
					count -= c;
					k -= c;
				} k <= c k c {
					count -= k;
				} count -= k count k {
					count -= c;
					k -= c;
				} count -= c count c k -= c k c System.out.println(count) System.out.println System.out System System.out count kb.close() kb.close kb String[] args args public class Main {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner kb = new Scanner(System.in);
		int a = kb.nextInt();
		int b = kb.nextInt();
		int c = kb.nextInt();
		int k = kb.nextInt();
		int count = 0;
		if (k <= a) {
			count += k;
		} else {
			count += a;
			k -= a;
			if (k <= b) {
			} else {
				k -= b;
				if (k <= c) {
					count -= k;
				} else {
					count -= c;
					k -= c;
				}
			}
		}

		System.out.println(count);
		kb.close();
	}
} public class Main {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner kb = new Scanner(System.in);
		int a = kb.nextInt();
		int b = kb.nextInt();
		int c = kb.nextInt();
		int k = kb.nextInt();
		int count = 0;
		if (k <= a) {
			count += k;
		} else {
			count += a;
			k -= a;
			if (k <= b) {
			} else {
				k -= b;
				if (k <= c) {
					count -= k;
				} else {
					count -= c;
					k -= c;
				}
			}
		}

		System.out.println(count);
		kb.close();
	}
} Main
import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner kb = new Scanner(System.in);
		int a = kb.nextInt();
		int b = kb.nextInt();
		int c = kb.nextInt();
		int k = kb.nextInt();
		int count = 0;
		if (k <= a) {
			count += k;
		} else {
			count += a;
			k -= a;
			if (k <= b) {
			} else {
				k -= b;
				if (k <= c) {
					count -= k;
				} else {
					count -= c;
					k -= c;
				}
			}
		}

		System.out.println(count);
		kb.close();
	}
}
