import java.util.*;
import java.io.*;

public class Main {
    public static void main(String args[]) {

         Scanner sc=new Scanner(System.in);
		 long a=sc.nextLong();
		 long b=sc.nextLong();
		 long c=sc.nextLong();
		 long k=sc.nextLong();
		//String t=sc.next();
long	mx=0;
		if(k>a){
		    mx=mx+a;
		    k=k-a;
		    if(k>b){
		        k=k-b;
		    }else k=0;
		    
		    if(k>0){
		        mx-=k;k=0;
		    }
		    
		}else mx=k;
		
		
		
		
		System.out.println(mx);
	




         
       // System.out.println("Hello World!");
    }
} import java.util.*; util.* java import java.io.*; io.* java public class Main {
    public static void main(String args[]) {

         Scanner sc=new Scanner(System.in);
		 long a=sc.nextLong();
		 long b=sc.nextLong();
		 long c=sc.nextLong();
		 long k=sc.nextLong();
		//String t=sc.next();
long	mx=0;
		if(k>a){
		    mx=mx+a;
		    k=k-a;
		    if(k>b){
		        k=k-b;
		    }else k=0;
		    
		    if(k>0){
		        mx-=k;k=0;
		    }
		    
		}else mx=k;
		
		
		
		
		System.out.println(mx);
	




         
       // System.out.println("Hello World!");
    }
} Main public static void main(String args[]) {

         Scanner sc=new Scanner(System.in);
		 long a=sc.nextLong();
		 long b=sc.nextLong();
		 long c=sc.nextLong();
		 long k=sc.nextLong();
		//String t=sc.next();
long	mx=0;
		if(k>a){
		    mx=mx+a;
		    k=k-a;
		    if(k>b){
		        k=k-b;
		    }else k=0;
		    
		    if(k>0){
		        mx-=k;k=0;
		    }
		    
		}else mx=k;
		
		
		
		
		System.out.println(mx);
	




         
       // System.out.println("Hello World!");
    } main {

         Scanner sc=new Scanner(System.in);
		 long a=sc.nextLong();
		 long b=sc.nextLong();
		 long c=sc.nextLong();
		 long k=sc.nextLong();
		//String t=sc.next();
long	mx=0;
		if(k>a){
		    mx=mx+a;
		    k=k-a;
		    if(k>b){
		        k=k-b;
		    }else k=0;
		    
		    if(k>0){
		        mx-=k;k=0;
		    }
		    
		}else mx=k;
		
		
		
		
		System.out.println(mx);
	




         
       // System.out.println("Hello World!");
    } 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 long	mx=0; mx 0 if(k>a){
		    mx=mx+a;
		    k=k-a;
		    if(k>b){
		        k=k-b;
		    }else k=0;
		    
		    if(k>0){
		        mx-=k;k=0;
		    }
		    
		}else mx=k; k>a k a {
		    mx=mx+a;
		    k=k-a;
		    if(k>b){
		        k=k-b;
		    }else k=0;
		    
		    if(k>0){
		        mx-=k;k=0;
		    }
		    
		} mx=mx+a mx mx+a mx a k=k-a k k-a k a if(k>b){
		        k=k-b;
		    }else k=0; k>b k b {
		        k=k-b;
		    } k=k-b k k-b k b k=0 k 0 if(k>0){
		        mx-=k;k=0;
		    } k>0 k 0 {
		        mx-=k;k=0;
		    } mx-=k mx k k=0 k 0 mx=k mx k System.out.println(mx) System.out.println System.out System System.out mx String args[] args public class Main {
    public static void main(String args[]) {

         Scanner sc=new Scanner(System.in);
		 long a=sc.nextLong();
		 long b=sc.nextLong();
		 long c=sc.nextLong();
		 long k=sc.nextLong();
		//String t=sc.next();
long	mx=0;
		if(k>a){
		    mx=mx+a;
		    k=k-a;
		    if(k>b){
		        k=k-b;
		    }else k=0;
		    
		    if(k>0){
		        mx-=k;k=0;
		    }
		    
		}else mx=k;
		
		
		
		
		System.out.println(mx);
	




         
       // System.out.println("Hello World!");
    }
} public class Main {
    public static void main(String args[]) {

         Scanner sc=new Scanner(System.in);
		 long a=sc.nextLong();
		 long b=sc.nextLong();
		 long c=sc.nextLong();
		 long k=sc.nextLong();
		//String t=sc.next();
long	mx=0;
		if(k>a){
		    mx=mx+a;
		    k=k-a;
		    if(k>b){
		        k=k-b;
		    }else k=0;
		    
		    if(k>0){
		        mx-=k;k=0;
		    }
		    
		}else mx=k;
		
		
		
		
		System.out.println(mx);
	




         
       // System.out.println("Hello World!");
    }
} Main
import java.util.*;
import java.io.*;

public class Main {
    public static void main(String args[]) {

         Scanner sc=new Scanner(System.in);
		 long a=sc.nextLong();
		 long b=sc.nextLong();
		 long c=sc.nextLong();
		 long k=sc.nextLong();
		//String t=sc.next();
long	mx=0;
		if(k>a){
		    mx=mx+a;
		    k=k-a;
		    if(k>b){
		        k=k-b;
		    }else k=0;
		    
		    if(k>0){
		        mx-=k;k=0;
		    }
		    
		}else mx=k;
		
		
		
		
		System.out.println(mx);
	




         
       // System.out.println("Hello World!");
    }
}