import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        long A = sc.nextLong();
        long B = sc.nextLong();
        long C = sc.nextLong();
        long K = sc.nextLong();
        long sum = 0;
        
        if(K<A){
            sum = K;
        }
        else if(K<=A+B){
            sum = A;
        }
        
        else{
            sum = A - (K-A-B);
        }
        System.out.println(sum);
    }
} import java.util.*; util.* java public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        long A = sc.nextLong();
        long B = sc.nextLong();
        long C = sc.nextLong();
        long K = sc.nextLong();
        long sum = 0;
        
        if(K<A){
            sum = K;
        }
        else if(K<=A+B){
            sum = A;
        }
        
        else{
            sum = A - (K-A-B);
        }
        System.out.println(sum);
    }
} Main public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        long A = sc.nextLong();
        long B = sc.nextLong();
        long C = sc.nextLong();
        long K = sc.nextLong();
        long sum = 0;
        
        if(K<A){
            sum = K;
        }
        else if(K<=A+B){
            sum = A;
        }
        
        else{
            sum = A - (K-A-B);
        }
        System.out.println(sum);
    } main {
        Scanner sc = new Scanner(System.in);
        long A = sc.nextLong();
        long B = sc.nextLong();
        long C = sc.nextLong();
        long K = sc.nextLong();
        long sum = 0;
        
        if(K<A){
            sum = K;
        }
        else if(K<=A+B){
            sum = A;
        }
        
        else{
            sum = A - (K-A-B);
        }
        System.out.println(sum);
    } Scanner sc = new Scanner(System.in); sc new Scanner(System.in) long A = sc.nextLong(); A sc.nextLong() sc.nextLong sc long B = sc.nextLong(); B sc.nextLong() sc.nextLong sc long C = sc.nextLong(); C sc.nextLong() sc.nextLong sc long K = sc.nextLong(); K sc.nextLong() sc.nextLong sc long sum = 0; sum 0 if(K<A){
            sum = K;
        }
        else if(K<=A+B){
            sum = A;
        }
        
        else{
            sum = A - (K-A-B);
        } K<A K A {
            sum = K;
        } sum = K sum K if(K<=A+B){
            sum = A;
        }
        
        else{
            sum = A - (K-A-B);
        } K<=A+B K A+B A B {
            sum = A;
        } sum = A sum A {
            sum = A - (K-A-B);
        } sum = A - (K-A-B) sum A - (K-A-B) A (K-A-B) K-A-B K A B System.out.println(sum) System.out.println System.out System System.out sum String[] args args public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        long A = sc.nextLong();
        long B = sc.nextLong();
        long C = sc.nextLong();
        long K = sc.nextLong();
        long sum = 0;
        
        if(K<A){
            sum = K;
        }
        else if(K<=A+B){
            sum = A;
        }
        
        else{
            sum = A - (K-A-B);
        }
        System.out.println(sum);
    }
} public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        long A = sc.nextLong();
        long B = sc.nextLong();
        long C = sc.nextLong();
        long K = sc.nextLong();
        long sum = 0;
        
        if(K<A){
            sum = K;
        }
        else if(K<=A+B){
            sum = A;
        }
        
        else{
            sum = A - (K-A-B);
        }
        System.out.println(sum);
    }
} Main
import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        long A = sc.nextLong();
        long B = sc.nextLong();
        long C = sc.nextLong();
        long K = sc.nextLong();
        long sum = 0;
        
        if(K<A){
            sum = K;
        }
        else if(K<=A+B){
            sum = A;
        }
        
        else{
            sum = A - (K-A-B);
        }
        System.out.println(sum);
    }
}
