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

        Long A = scanner.nextLong();
        Long B = scanner.nextLong();
        Long C = scanner.nextLong();
        Long K = scanner.nextLong();
        Long count = 0L;
        if(K >= A){
            K = K - A;
            count += A;
        }else{
            count += K;
            K = 0L;
        }
        K = K - B;
        if(K > 0){
            count = count - K;
        }

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

        Long A = scanner.nextLong();
        Long B = scanner.nextLong();
        Long C = scanner.nextLong();
        Long K = scanner.nextLong();
        Long count = 0L;
        if(K >= A){
            K = K - A;
            count += A;
        }else{
            count += K;
            K = 0L;
        }
        K = K - B;
        if(K > 0){
            count = count - K;
        }

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

        Long A = scanner.nextLong();
        Long B = scanner.nextLong();
        Long C = scanner.nextLong();
        Long K = scanner.nextLong();
        Long count = 0L;
        if(K >= A){
            K = K - A;
            count += A;
        }else{
            count += K;
            K = 0L;
        }
        K = K - B;
        if(K > 0){
            count = count - K;
        }

        System.out.println(count);
        
	} main {
        Scanner scanner = new Scanner(System.in);

        Long A = scanner.nextLong();
        Long B = scanner.nextLong();
        Long C = scanner.nextLong();
        Long K = scanner.nextLong();
        Long count = 0L;
        if(K >= A){
            K = K - A;
            count += A;
        }else{
            count += K;
            K = 0L;
        }
        K = K - B;
        if(K > 0){
            count = count - K;
        }

        System.out.println(count);
        
	} Scanner scanner = new Scanner(System.in); scanner new Scanner(System.in) Long A = scanner.nextLong(); A scanner.nextLong() scanner.nextLong scanner Long B = scanner.nextLong(); B scanner.nextLong() scanner.nextLong scanner Long C = scanner.nextLong(); C scanner.nextLong() scanner.nextLong scanner Long K = scanner.nextLong(); K scanner.nextLong() scanner.nextLong scanner Long count = 0L; count 0L if(K >= A){
            K = K - A;
            count += A;
        }else{
            count += K;
            K = 0L;
        } K >= A K A {
            K = K - A;
            count += A;
        } K = K - A K K - A K A count += A count A {
            count += K;
            K = 0L;
        } count += K count K K = 0L K 0L K = K - B K K - B K B if(K > 0){
            count = count - K;
        } K > 0 K 0 {
            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 scanner = new Scanner(System.in);

        Long A = scanner.nextLong();
        Long B = scanner.nextLong();
        Long C = scanner.nextLong();
        Long K = scanner.nextLong();
        Long count = 0L;
        if(K >= A){
            K = K - A;
            count += A;
        }else{
            count += K;
            K = 0L;
        }
        K = K - B;
        if(K > 0){
            count = count - K;
        }

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

        Long A = scanner.nextLong();
        Long B = scanner.nextLong();
        Long C = scanner.nextLong();
        Long K = scanner.nextLong();
        Long count = 0L;
        if(K >= A){
            K = K - A;
            count += A;
        }else{
            count += K;
            K = 0L;
        }
        K = K - B;
        if(K > 0){
            count = count - K;
        }

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

        Long A = scanner.nextLong();
        Long B = scanner.nextLong();
        Long C = scanner.nextLong();
        Long K = scanner.nextLong();
        Long count = 0L;
        if(K >= A){
            K = K - A;
            count += A;
        }else{
            count += K;
            K = 0L;
        }
        K = K - B;
        if(K > 0){
            count = count - K;
        }

        System.out.println(count);
        
	}
}