import java.util.Scanner;
public class Main {
	public static void main(String[] args) {
		Scanner stdIn = new Scanner(System.in);

		int A= stdIn.nextInt();
		int B= stdIn.nextInt();
		int C= stdIn.nextInt();
		int K= stdIn.nextInt();
		int counter = 0;

		if(A>=K) {
			counter += K;
		}else if(K>A) {
			counter += A;
			K -= A;
			if(K>B) {
				K -= B;
				if(K>0) {
					counter -= K;
				}
			}
		}
		System.out.println(counter);

		stdIn.close();
	}

} import java.util.Scanner; Scanner java.util public class Main {
	public static void main(String[] args) {
		Scanner stdIn = new Scanner(System.in);

		int A= stdIn.nextInt();
		int B= stdIn.nextInt();
		int C= stdIn.nextInt();
		int K= stdIn.nextInt();
		int counter = 0;

		if(A>=K) {
			counter += K;
		}else if(K>A) {
			counter += A;
			K -= A;
			if(K>B) {
				K -= B;
				if(K>0) {
					counter -= K;
				}
			}
		}
		System.out.println(counter);

		stdIn.close();
	}

} Main public static void main(String[] args) {
		Scanner stdIn = new Scanner(System.in);

		int A= stdIn.nextInt();
		int B= stdIn.nextInt();
		int C= stdIn.nextInt();
		int K= stdIn.nextInt();
		int counter = 0;

		if(A>=K) {
			counter += K;
		}else if(K>A) {
			counter += A;
			K -= A;
			if(K>B) {
				K -= B;
				if(K>0) {
					counter -= K;
				}
			}
		}
		System.out.println(counter);

		stdIn.close();
	} main {
		Scanner stdIn = new Scanner(System.in);

		int A= stdIn.nextInt();
		int B= stdIn.nextInt();
		int C= stdIn.nextInt();
		int K= stdIn.nextInt();
		int counter = 0;

		if(A>=K) {
			counter += K;
		}else if(K>A) {
			counter += A;
			K -= A;
			if(K>B) {
				K -= B;
				if(K>0) {
					counter -= K;
				}
			}
		}
		System.out.println(counter);

		stdIn.close();
	} Scanner stdIn = new Scanner(System.in); stdIn new Scanner(System.in) int A= stdIn.nextInt(); A stdIn.nextInt() stdIn.nextInt stdIn int B= stdIn.nextInt(); B stdIn.nextInt() stdIn.nextInt stdIn int C= stdIn.nextInt(); C stdIn.nextInt() stdIn.nextInt stdIn int K= stdIn.nextInt(); K stdIn.nextInt() stdIn.nextInt stdIn int counter = 0; counter 0 if(A>=K) {
			counter += K;
		}else if(K>A) {
			counter += A;
			K -= A;
			if(K>B) {
				K -= B;
				if(K>0) {
					counter -= K;
				}
			}
		} A>=K A K {
			counter += K;
		} counter += K counter K if(K>A) {
			counter += A;
			K -= A;
			if(K>B) {
				K -= B;
				if(K>0) {
					counter -= K;
				}
			}
		} K>A K A {
			counter += A;
			K -= A;
			if(K>B) {
				K -= B;
				if(K>0) {
					counter -= K;
				}
			}
		} counter += A counter A K -= A K A if(K>B) {
				K -= B;
				if(K>0) {
					counter -= K;
				}
			} K>B K B {
				K -= B;
				if(K>0) {
					counter -= K;
				}
			} K -= B K B if(K>0) {
					counter -= K;
				} K>0 K 0 {
					counter -= K;
				} counter -= K counter K System.out.println(counter) System.out.println System.out System System.out counter stdIn.close() stdIn.close stdIn String[] args args public class Main {
	public static void main(String[] args) {
		Scanner stdIn = new Scanner(System.in);

		int A= stdIn.nextInt();
		int B= stdIn.nextInt();
		int C= stdIn.nextInt();
		int K= stdIn.nextInt();
		int counter = 0;

		if(A>=K) {
			counter += K;
		}else if(K>A) {
			counter += A;
			K -= A;
			if(K>B) {
				K -= B;
				if(K>0) {
					counter -= K;
				}
			}
		}
		System.out.println(counter);

		stdIn.close();
	}

} public class Main {
	public static void main(String[] args) {
		Scanner stdIn = new Scanner(System.in);

		int A= stdIn.nextInt();
		int B= stdIn.nextInt();
		int C= stdIn.nextInt();
		int K= stdIn.nextInt();
		int counter = 0;

		if(A>=K) {
			counter += K;
		}else if(K>A) {
			counter += A;
			K -= A;
			if(K>B) {
				K -= B;
				if(K>0) {
					counter -= K;
				}
			}
		}
		System.out.println(counter);

		stdIn.close();
	}

} Main
import java.util.Scanner;
public class Main {
	public static void main(String[] args) {
		Scanner stdIn = new Scanner(System.in);

		int A= stdIn.nextInt();
		int B= stdIn.nextInt();
		int C= stdIn.nextInt();
		int K= stdIn.nextInt();
		int counter = 0;

		if(A>=K) {
			counter += K;
		}else if(K>A) {
			counter += A;
			K -= A;
			if(K>B) {
				K -= B;
				if(K>0) {
					counter -= K;
				}
			}
		}
		System.out.println(counter);

		stdIn.close();
	}

}
