import java.io.BufferedReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.StringTokenizer;

public class Main {
    
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new java.io.InputStreamReader(System.in));
        PrintWriter pw = new PrintWriter(System.out);
        StringTokenizer st = new StringTokenizer(br.readLine());

        int A = Integer.parseInt(st.nextToken());
        int B = Integer.parseInt(st.nextToken());
        int C = Integer.parseInt(st.nextToken());
        int K = Integer.parseInt(st.nextToken());

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

        int A = Integer.parseInt(st.nextToken());
        int B = Integer.parseInt(st.nextToken());
        int C = Integer.parseInt(st.nextToken());
        int K = Integer.parseInt(st.nextToken());

        if(A >= K) {
            pw.println(K);
        } else if(A + B >= K) {
            pw.println(A);
        } else if(A + B + C >= K) {
            pw.println(A - (K-(A+B)));
        }
       
        br.close();
        pw.close();
    }
} Main public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new java.io.InputStreamReader(System.in));
        PrintWriter pw = new PrintWriter(System.out);
        StringTokenizer st = new StringTokenizer(br.readLine());

        int A = Integer.parseInt(st.nextToken());
        int B = Integer.parseInt(st.nextToken());
        int C = Integer.parseInt(st.nextToken());
        int K = Integer.parseInt(st.nextToken());

        if(A >= K) {
            pw.println(K);
        } else if(A + B >= K) {
            pw.println(A);
        } else if(A + B + C >= K) {
            pw.println(A - (K-(A+B)));
        }
       
        br.close();
        pw.close();
    } main {
        BufferedReader br = new BufferedReader(new java.io.InputStreamReader(System.in));
        PrintWriter pw = new PrintWriter(System.out);
        StringTokenizer st = new StringTokenizer(br.readLine());

        int A = Integer.parseInt(st.nextToken());
        int B = Integer.parseInt(st.nextToken());
        int C = Integer.parseInt(st.nextToken());
        int K = Integer.parseInt(st.nextToken());

        if(A >= K) {
            pw.println(K);
        } else if(A + B >= K) {
            pw.println(A);
        } else if(A + B + C >= K) {
            pw.println(A - (K-(A+B)));
        }
       
        br.close();
        pw.close();
    } BufferedReader br = new BufferedReader(new java.io.InputStreamReader(System.in)); br new BufferedReader(new java.io.InputStreamReader(System.in)) PrintWriter pw = new PrintWriter(System.out); pw new PrintWriter(System.out) StringTokenizer st = new StringTokenizer(br.readLine()); st new StringTokenizer(br.readLine()) int A = Integer.parseInt(st.nextToken()); A Integer.parseInt(st.nextToken()) Integer.parseInt Integer st.nextToken() st.nextToken st int B = Integer.parseInt(st.nextToken()); B Integer.parseInt(st.nextToken()) Integer.parseInt Integer st.nextToken() st.nextToken st int C = Integer.parseInt(st.nextToken()); C Integer.parseInt(st.nextToken()) Integer.parseInt Integer st.nextToken() st.nextToken st int K = Integer.parseInt(st.nextToken()); K Integer.parseInt(st.nextToken()) Integer.parseInt Integer st.nextToken() st.nextToken st if(A >= K) {
            pw.println(K);
        } else if(A + B >= K) {
            pw.println(A);
        } else if(A + B + C >= K) {
            pw.println(A - (K-(A+B)));
        } A >= K A K {
            pw.println(K);
        } pw.println(K) pw.println pw K if(A + B >= K) {
            pw.println(A);
        } else if(A + B + C >= K) {
            pw.println(A - (K-(A+B)));
        } A + B >= K A + B A B K {
            pw.println(A);
        } pw.println(A) pw.println pw A if(A + B + C >= K) {
            pw.println(A - (K-(A+B)));
        } A + B + C >= K A + B + C A + B + C A B C K {
            pw.println(A - (K-(A+B)));
        } pw.println(A - (K-(A+B))) pw.println pw A - (K-(A+B)) A (K-(A+B)) K (A+B) A B br.close() br.close br pw.close() pw.close pw String[] args args public class Main {
    
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new java.io.InputStreamReader(System.in));
        PrintWriter pw = new PrintWriter(System.out);
        StringTokenizer st = new StringTokenizer(br.readLine());

        int A = Integer.parseInt(st.nextToken());
        int B = Integer.parseInt(st.nextToken());
        int C = Integer.parseInt(st.nextToken());
        int K = Integer.parseInt(st.nextToken());

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

        int A = Integer.parseInt(st.nextToken());
        int B = Integer.parseInt(st.nextToken());
        int C = Integer.parseInt(st.nextToken());
        int K = Integer.parseInt(st.nextToken());

        if(A >= K) {
            pw.println(K);
        } else if(A + B >= K) {
            pw.println(A);
        } else if(A + B + C >= K) {
            pw.println(A - (K-(A+B)));
        }
       
        br.close();
        pw.close();
    }
} Main
import java.io.BufferedReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.StringTokenizer;

public class Main {
    
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new java.io.InputStreamReader(System.in));
        PrintWriter pw = new PrintWriter(System.out);
        StringTokenizer st = new StringTokenizer(br.readLine());

        int A = Integer.parseInt(st.nextToken());
        int B = Integer.parseInt(st.nextToken());
        int C = Integer.parseInt(st.nextToken());
        int K = Integer.parseInt(st.nextToken());

        if(A >= K) {
            pw.println(K);
        } else if(A + B >= K) {
            pw.println(A);
        } else if(A + B + C >= K) {
            pw.println(A - (K-(A+B)));
        }
       
        br.close();
        pw.close();
    }
}