import java.util.*;

public 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>=K){
            System.out.println(K);
        }else if(A<K && A+B>=K){
            System.out.println(A);
        }else if(A+B<K){
            System.out.println(A-(K-A-B));
        }

    }
} import java.util.*; util.* java public 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>=K){
            System.out.println(K);
        }else if(A<K && A+B>=K){
            System.out.println(A);
        }else if(A+B<K){
            System.out.println(A-(K-A-B));
        }

    }
} 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>=K){
            System.out.println(K);
        }else if(A<K && A+B>=K){
            System.out.println(A);
        }else if(A+B<K){
            System.out.println(A-(K-A-B));
        }

    } 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>=K){
            System.out.println(K);
        }else if(A<K && A+B>=K){
            System.out.println(A);
        }else if(A+B<K){
            System.out.println(A-(K-A-B));
        }

    } 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>=K){
            System.out.println(K);
        }else if(A<K && A+B>=K){
            System.out.println(A);
        }else if(A+B<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<K && A+B>=K){
            System.out.println(A);
        }else if(A+B<K){
            System.out.println(A-(K-A-B));
        } A<K && A+B>=K A<K A K 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<K){
            System.out.println(A-(K-A-B));
        } A+B<K A+B A B 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 K A B String[] args args public 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>=K){
            System.out.println(K);
        }else if(A<K && A+B>=K){
            System.out.println(A);
        }else if(A+B<K){
            System.out.println(A-(K-A-B));
        }

    }
} public 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>=K){
            System.out.println(K);
        }else if(A<K && A+B>=K){
            System.out.println(A);
        }else if(A+B<K){
            System.out.println(A-(K-A-B));
        }

    }
} Main
import java.util.*;

public 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>=K){
            System.out.println(K);
        }else if(A<K && A+B>=K){
            System.out.println(A);
        }else if(A+B<K){
            System.out.println(A-(K-A-B));
        }

    }
}
