import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int a = scan.nextInt();
        int b = scan.nextInt();
        int c = scan.nextInt();
        int k = scan.nextInt();

        int result = 0;

        if(a >= k) {
            result += k;
        } else if(a + b >= k) {
            result += a;
        } else {
            result += ( a - (k - a - b));
        }
        
        System.out.println(String.valueOf(result));

    }
} import java.util.Scanner; Scanner java.util public class Main {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int a = scan.nextInt();
        int b = scan.nextInt();
        int c = scan.nextInt();
        int k = scan.nextInt();

        int result = 0;

        if(a >= k) {
            result += k;
        } else if(a + b >= k) {
            result += a;
        } else {
            result += ( a - (k - a - b));
        }
        
        System.out.println(String.valueOf(result));

    }
} Main public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int a = scan.nextInt();
        int b = scan.nextInt();
        int c = scan.nextInt();
        int k = scan.nextInt();

        int result = 0;

        if(a >= k) {
            result += k;
        } else if(a + b >= k) {
            result += a;
        } else {
            result += ( a - (k - a - b));
        }
        
        System.out.println(String.valueOf(result));

    } main {
        Scanner scan = new Scanner(System.in);
        int a = scan.nextInt();
        int b = scan.nextInt();
        int c = scan.nextInt();
        int k = scan.nextInt();

        int result = 0;

        if(a >= k) {
            result += k;
        } else if(a + b >= k) {
            result += a;
        } else {
            result += ( a - (k - a - b));
        }
        
        System.out.println(String.valueOf(result));

    } Scanner scan = new Scanner(System.in); scan new Scanner(System.in) int a = scan.nextInt(); a scan.nextInt() scan.nextInt scan int b = scan.nextInt(); b scan.nextInt() scan.nextInt scan int c = scan.nextInt(); c scan.nextInt() scan.nextInt scan int k = scan.nextInt(); k scan.nextInt() scan.nextInt scan int result = 0; result 0 if(a >= k) {
            result += k;
        } else if(a + b >= k) {
            result += a;
        } else {
            result += ( a - (k - a - b));
        } a >= k a k {
            result += k;
        } result += k result k if(a + b >= k) {
            result += a;
        } else {
            result += ( a - (k - a - b));
        } a + b >= k a + b a b k {
            result += a;
        } result += a result a {
            result += ( a - (k - a - b));
        } result += ( a - (k - a - b)) result ( a - (k - a - b)) a (k - a - b) k - a - b k a b System.out.println(String.valueOf(result)) System.out.println System.out System System.out String.valueOf(result) String.valueOf String result String[] args args public class Main {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int a = scan.nextInt();
        int b = scan.nextInt();
        int c = scan.nextInt();
        int k = scan.nextInt();

        int result = 0;

        if(a >= k) {
            result += k;
        } else if(a + b >= k) {
            result += a;
        } else {
            result += ( a - (k - a - b));
        }
        
        System.out.println(String.valueOf(result));

    }
} public class Main {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int a = scan.nextInt();
        int b = scan.nextInt();
        int c = scan.nextInt();
        int k = scan.nextInt();

        int result = 0;

        if(a >= k) {
            result += k;
        } else if(a + b >= k) {
            result += a;
        } else {
            result += ( a - (k - a - b));
        }
        
        System.out.println(String.valueOf(result));

    }
} Main
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int a = scan.nextInt();
        int b = scan.nextInt();
        int c = scan.nextInt();
        int k = scan.nextInt();

        int result = 0;

        if(a >= k) {
            result += k;
        } else if(a + b >= k) {
            result += a;
        } else {
            result += ( a - (k - a - b));
        }
        
        System.out.println(String.valueOf(result));

    }
}