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();
		int count =0;
		
		
			if(A<=k && 0<k){
			count += A;
			k=k-A;
			A=0;
			}
			
			else if(A>k){
			count +=k;
			A=0;
			}
			
			if(A==0 && 0<k){
				k=k-B;
				B=0;
			}
			
			if(A==0 && B==0 && 0<k ){
				count=count-k;
			}
			
			
			System.out.println(count);
		
	}
}
				
				 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();
		int count =0;
		
		
			if(A<=k && 0<k){
			count += A;
			k=k-A;
			A=0;
			}
			
			else if(A>k){
			count +=k;
			A=0;
			}
			
			if(A==0 && 0<k){
				k=k-B;
				B=0;
			}
			
			if(A==0 && B==0 && 0<k ){
				count=count-k;
			}
			
			
			System.out.println(count);
		
	}
} 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();
		int count =0;
		
		
			if(A<=k && 0<k){
			count += A;
			k=k-A;
			A=0;
			}
			
			else if(A>k){
			count +=k;
			A=0;
			}
			
			if(A==0 && 0<k){
				k=k-B;
				B=0;
			}
			
			if(A==0 && B==0 && 0<k ){
				count=count-k;
			}
			
			
			System.out.println(count);
		
	} main {
		
		Scanner sc = new Scanner(System.in);
		
		int A = sc.nextInt();
		int B = sc.nextInt();
		int C = sc.nextInt();
		
		int k = sc.nextInt();
		int count =0;
		
		
			if(A<=k && 0<k){
			count += A;
			k=k-A;
			A=0;
			}
			
			else if(A>k){
			count +=k;
			A=0;
			}
			
			if(A==0 && 0<k){
				k=k-B;
				B=0;
			}
			
			if(A==0 && B==0 && 0<k ){
				count=count-k;
			}
			
			
			System.out.println(count);
		
	} 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 int count =0; count 0 if(A<=k && 0<k){
			count += A;
			k=k-A;
			A=0;
			}
			
			else if(A>k){
			count +=k;
			A=0;
			} A<=k && 0<k A<=k A k 0<k 0 k {
			count += A;
			k=k-A;
			A=0;
			} count += A count A k=k-A k k-A k A A=0 A 0 if(A>k){
			count +=k;
			A=0;
			} A>k A k {
			count +=k;
			A=0;
			} count +=k count k A=0 A 0 if(A==0 && 0<k){
				k=k-B;
				B=0;
			} A==0 && 0<k A==0 A 0 0<k 0 k {
				k=k-B;
				B=0;
			} k=k-B k k-B k B B=0 B 0 if(A==0 && B==0 && 0<k ){
				count=count-k;
			} A==0 && B==0 && 0<k A==0 && B==0 && 0<k A==0 A 0 B==0 B 0 0<k 0 k {
				count=count-k;
			} count=count-k count count-k count k System.out.println(count) System.out.println System.out System System.out count 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();
		int count =0;
		
		
			if(A<=k && 0<k){
			count += A;
			k=k-A;
			A=0;
			}
			
			else if(A>k){
			count +=k;
			A=0;
			}
			
			if(A==0 && 0<k){
				k=k-B;
				B=0;
			}
			
			if(A==0 && B==0 && 0<k ){
				count=count-k;
			}
			
			
			System.out.println(count);
		
	}
} 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();
		int count =0;
		
		
			if(A<=k && 0<k){
			count += A;
			k=k-A;
			A=0;
			}
			
			else if(A>k){
			count +=k;
			A=0;
			}
			
			if(A==0 && 0<k){
				k=k-B;
				B=0;
			}
			
			if(A==0 && B==0 && 0<k ){
				count=count-k;
			}
			
			
			System.out.println(count);
		
	}
} 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();
		int count =0;
		
		
			if(A<=k && 0<k){
			count += A;
			k=k-A;
			A=0;
			}
			
			else if(A>k){
			count +=k;
			A=0;
			}
			
			if(A==0 && 0<k){
				k=k-B;
				B=0;
			}
			
			if(A==0 && B==0 && 0<k ){
				count=count-k;
			}
			
			
			System.out.println(count);
		
	}
}
				
				