
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String str[] = br.readLine().split(" ");

        int A = Integer.parseInt(str[0]);
        int B = Integer.parseInt(str[1]);
        int C = Integer.parseInt(str[2]);
        int K = Integer.parseInt(str[3]);

        if(A >= K){
            System.out.println(K);
        }
        else if(A+B >= K){
            System.out.println(A);
        }
        else if(A+B+C >= K){
            System.out.println(A-(K - (A+B)));
        }
    }
} import java.io.BufferedReader; BufferedReader java.io import java.io.IOException; IOException java.io import java.io.InputStreamReader; InputStreamReader java.io public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String str[] = br.readLine().split(" ");

        int A = Integer.parseInt(str[0]);
        int B = Integer.parseInt(str[1]);
        int C = Integer.parseInt(str[2]);
        int K = Integer.parseInt(str[3]);

        if(A >= K){
            System.out.println(K);
        }
        else if(A+B >= K){
            System.out.println(A);
        }
        else if(A+B+C >= K){
            System.out.println(A-(K - (A+B)));
        }
    }
} Main public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String str[] = br.readLine().split(" ");

        int A = Integer.parseInt(str[0]);
        int B = Integer.parseInt(str[1]);
        int C = Integer.parseInt(str[2]);
        int K = Integer.parseInt(str[3]);

        if(A >= K){
            System.out.println(K);
        }
        else if(A+B >= K){
            System.out.println(A);
        }
        else if(A+B+C >= K){
            System.out.println(A-(K - (A+B)));
        }
    } main {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String str[] = br.readLine().split(" ");

        int A = Integer.parseInt(str[0]);
        int B = Integer.parseInt(str[1]);
        int C = Integer.parseInt(str[2]);
        int K = Integer.parseInt(str[3]);

        if(A >= K){
            System.out.println(K);
        }
        else if(A+B >= K){
            System.out.println(A);
        }
        else if(A+B+C >= K){
            System.out.println(A-(K - (A+B)));
        }
    } BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); br new BufferedReader(new InputStreamReader(System.in)) String str[] = br.readLine().split(" "); str br.readLine().split(" ") br.readLine().split br.readLine() br.readLine br " " int A = Integer.parseInt(str[0]); A Integer.parseInt(str[0]) Integer.parseInt Integer str[0] str 0 int B = Integer.parseInt(str[1]); B Integer.parseInt(str[1]) Integer.parseInt Integer str[1] str 1 int C = Integer.parseInt(str[2]); C Integer.parseInt(str[2]) Integer.parseInt Integer str[2] str 2 int K = Integer.parseInt(str[3]); K Integer.parseInt(str[3]) Integer.parseInt Integer str[3] str 3 if(A >= K){
            System.out.println(K);
        }
        else if(A+B >= K){
            System.out.println(A);
        }
        else if(A+B+C >= K){
            System.out.println(A-(K - (A+B)));
        } A >= K A K {
            System.out.println(K);
        } System.out.println(K) System.out.println System.out System System.out K if(A+B >= K){
            System.out.println(A);
        }
        else if(A+B+C >= K){
            System.out.println(A-(K - (A+B)));
        } 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 if(A+B+C >= K){
            System.out.println(A-(K - (A+B)));
        } A+B+C >= K A+B+C A+B+C A B C K {
            System.out.println(A-(K - (A+B)));
        } System.out.println(A-(K - (A+B))) System.out.println System.out System System.out A-(K - (A+B)) A (K - (A+B)) K (A+B) A B String[] args args public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String str[] = br.readLine().split(" ");

        int A = Integer.parseInt(str[0]);
        int B = Integer.parseInt(str[1]);
        int C = Integer.parseInt(str[2]);
        int K = Integer.parseInt(str[3]);

        if(A >= K){
            System.out.println(K);
        }
        else if(A+B >= K){
            System.out.println(A);
        }
        else if(A+B+C >= K){
            System.out.println(A-(K - (A+B)));
        }
    }
} public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String str[] = br.readLine().split(" ");

        int A = Integer.parseInt(str[0]);
        int B = Integer.parseInt(str[1]);
        int C = Integer.parseInt(str[2]);
        int K = Integer.parseInt(str[3]);

        if(A >= K){
            System.out.println(K);
        }
        else if(A+B >= K){
            System.out.println(A);
        }
        else if(A+B+C >= K){
            System.out.println(A-(K - (A+B)));
        }
    }
} Main

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String str[] = br.readLine().split(" ");

        int A = Integer.parseInt(str[0]);
        int B = Integer.parseInt(str[1]);
        int C = Integer.parseInt(str[2]);
        int K = Integer.parseInt(str[3]);

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