import java.util.*;

public class Main {
	public static void main(String[] args){
		Scanner sc = new Scanner(System.in);
		
		int a=sc.nextInt();
		int b=sc.nextInt();
		int c=sc.nextInt();
		int k=sc.nextInt();
		
		if(a>=k){
			int n=a-k;
			System.out.println(a-n);
		}
		else if(a+b>=k){
			System.out.println(a);
		}
		else {
			k-=a;
			k-=b;
			System.out.println(a+(k*(-1)));
		}
		
	}
} import java.util.*; util.* java public class Main {
	public static void main(String[] args){
		Scanner sc = new Scanner(System.in);
		
		int a=sc.nextInt();
		int b=sc.nextInt();
		int c=sc.nextInt();
		int k=sc.nextInt();
		
		if(a>=k){
			int n=a-k;
			System.out.println(a-n);
		}
		else if(a+b>=k){
			System.out.println(a);
		}
		else {
			k-=a;
			k-=b;
			System.out.println(a+(k*(-1)));
		}
		
	}
} Main public static void main(String[] args){
		Scanner sc = new Scanner(System.in);
		
		int a=sc.nextInt();
		int b=sc.nextInt();
		int c=sc.nextInt();
		int k=sc.nextInt();
		
		if(a>=k){
			int n=a-k;
			System.out.println(a-n);
		}
		else if(a+b>=k){
			System.out.println(a);
		}
		else {
			k-=a;
			k-=b;
			System.out.println(a+(k*(-1)));
		}
		
	} main {
		Scanner sc = new Scanner(System.in);
		
		int a=sc.nextInt();
		int b=sc.nextInt();
		int c=sc.nextInt();
		int k=sc.nextInt();
		
		if(a>=k){
			int n=a-k;
			System.out.println(a-n);
		}
		else if(a+b>=k){
			System.out.println(a);
		}
		else {
			k-=a;
			k-=b;
			System.out.println(a+(k*(-1)));
		}
		
	} Scanner sc = new Scanner(System.in); sc new Scanner(System.in) int a=sc.nextInt(); a sc.nextInt() sc.nextInt sc int b=sc.nextInt(); b sc.nextInt() sc.nextInt sc int c=sc.nextInt(); c sc.nextInt() sc.nextInt sc int k=sc.nextInt(); k sc.nextInt() sc.nextInt sc if(a>=k){
			int n=a-k;
			System.out.println(a-n);
		}
		else if(a+b>=k){
			System.out.println(a);
		}
		else {
			k-=a;
			k-=b;
			System.out.println(a+(k*(-1)));
		} a>=k a k {
			int n=a-k;
			System.out.println(a-n);
		} int n=a-k; n a-k a k System.out.println(a-n) System.out.println System.out System System.out a-n a n if(a+b>=k){
			System.out.println(a);
		}
		else {
			k-=a;
			k-=b;
			System.out.println(a+(k*(-1)));
		} 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 {
			k-=a;
			k-=b;
			System.out.println(a+(k*(-1)));
		} k-=a k a k-=b k b System.out.println(a+(k*(-1))) System.out.println System.out System System.out a+(k*(-1)) a (k*(-1)) k (-1) 1 String[] args args public class Main {
	public static void main(String[] args){
		Scanner sc = new Scanner(System.in);
		
		int a=sc.nextInt();
		int b=sc.nextInt();
		int c=sc.nextInt();
		int k=sc.nextInt();
		
		if(a>=k){
			int n=a-k;
			System.out.println(a-n);
		}
		else if(a+b>=k){
			System.out.println(a);
		}
		else {
			k-=a;
			k-=b;
			System.out.println(a+(k*(-1)));
		}
		
	}
} public class Main {
	public static void main(String[] args){
		Scanner sc = new Scanner(System.in);
		
		int a=sc.nextInt();
		int b=sc.nextInt();
		int c=sc.nextInt();
		int k=sc.nextInt();
		
		if(a>=k){
			int n=a-k;
			System.out.println(a-n);
		}
		else if(a+b>=k){
			System.out.println(a);
		}
		else {
			k-=a;
			k-=b;
			System.out.println(a+(k*(-1)));
		}
		
	}
} Main
import java.util.*;

public class Main {
	public static void main(String[] args){
		Scanner sc = new Scanner(System.in);
		
		int a=sc.nextInt();
		int b=sc.nextInt();
		int c=sc.nextInt();
		int k=sc.nextInt();
		
		if(a>=k){
			int n=a-k;
			System.out.println(a-n);
		}
		else if(a+b>=k){
			System.out.println(a);
		}
		else {
			k-=a;
			k-=b;
			System.out.println(a+(k*(-1)));
		}
		
	}
}
