import java.util.*;
import java.util.List;
import java.util.ArrayList;
import java.math.*;
class Main{
    public static void main(String[]args){
        Scanner scanner = new Scanner(System.in);
        int A = scanner.nextInt();
        int B = scanner.nextInt();
        int C = scanner.nextInt();
        int K = scanner.nextInt();

        if(A+B<=K){
            System.out.println(A+(-1)*(K-A-B));
        }
        else{
            if(A>=K) System.out.println(K);
            else System.out.println(A);
        }

    }

} import java.util.*; util.* java import java.util.List; List java.util import java.util.ArrayList; ArrayList java.util import java.math.*; math.* java class Main{
    public static void main(String[]args){
        Scanner scanner = new Scanner(System.in);
        int A = scanner.nextInt();
        int B = scanner.nextInt();
        int C = scanner.nextInt();
        int K = scanner.nextInt();

        if(A+B<=K){
            System.out.println(A+(-1)*(K-A-B));
        }
        else{
            if(A>=K) System.out.println(K);
            else System.out.println(A);
        }

    }

} Main public static void main(String[]args){
        Scanner scanner = new Scanner(System.in);
        int A = scanner.nextInt();
        int B = scanner.nextInt();
        int C = scanner.nextInt();
        int K = scanner.nextInt();

        if(A+B<=K){
            System.out.println(A+(-1)*(K-A-B));
        }
        else{
            if(A>=K) System.out.println(K);
            else System.out.println(A);
        }

    } main {
        Scanner scanner = new Scanner(System.in);
        int A = scanner.nextInt();
        int B = scanner.nextInt();
        int C = scanner.nextInt();
        int K = scanner.nextInt();

        if(A+B<=K){
            System.out.println(A+(-1)*(K-A-B));
        }
        else{
            if(A>=K) System.out.println(K);
            else System.out.println(A);
        }

    } Scanner scanner = new Scanner(System.in); scanner new Scanner(System.in) int A = scanner.nextInt(); A scanner.nextInt() scanner.nextInt scanner int B = scanner.nextInt(); B scanner.nextInt() scanner.nextInt scanner int C = scanner.nextInt(); C scanner.nextInt() scanner.nextInt scanner int K = scanner.nextInt(); K scanner.nextInt() scanner.nextInt scanner if(A+B<=K){
            System.out.println(A+(-1)*(K-A-B));
        }
        else{
            if(A>=K) System.out.println(K);
            else System.out.println(A);
        } A+B<=K A+B A B K {
            System.out.println(A+(-1)*(K-A-B));
        } System.out.println(A+(-1)*(K-A-B)) System.out.println System.out System System.out A+(-1)*(K-A-B) A (-1)*(K-A-B) (-1) 1 (K-A-B) K-A-B K A B {
            if(A>=K) System.out.println(K);
            else System.out.println(A);
        } if(A>=K) System.out.println(K);
            else System.out.println(A); A>=K A K System.out.println(K) System.out.println System.out System System.out K System.out.println(A) System.out.println System.out System System.out A String[]args args
import java.util.*;
import java.util.List;
import java.util.ArrayList;
import java.math.*;
class Main{
    public static void main(String[]args){
        Scanner scanner = new Scanner(System.in);
        int A = scanner.nextInt();
        int B = scanner.nextInt();
        int C = scanner.nextInt();
        int K = scanner.nextInt();

        if(A+B<=K){
            System.out.println(A+(-1)*(K-A-B));
        }
        else{
            if(A>=K) System.out.println(K);
            else System.out.println(A);
        }

    }

}