import java.util.Scanner;

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

        a = scanner.nextInt();
        b = scanner.nextInt();
        c = scanner.nextInt();
        k = scanner.nextInt();

        if(k <= a) {
            System.out.println(k);
        }
        else if(k <= a+b) {
            System.out.println(k-b);
        }
        else if (k <= a+b+c) {
            System.out.println(a-(k-(a+b)));
        }
    }
} import java.util.Scanner; Scanner java.util public class Main {
    public static void main(String[] args) {
        final Scanner scanner = new Scanner(System.in);
        int a, b, c, k;

        a = scanner.nextInt();
        b = scanner.nextInt();
        c = scanner.nextInt();
        k = scanner.nextInt();

        if(k <= a) {
            System.out.println(k);
        }
        else if(k <= a+b) {
            System.out.println(k-b);
        }
        else if (k <= a+b+c) {
            System.out.println(a-(k-(a+b)));
        }
    }
} Main public static void main(String[] args) {
        final Scanner scanner = new Scanner(System.in);
        int a, b, c, k;

        a = scanner.nextInt();
        b = scanner.nextInt();
        c = scanner.nextInt();
        k = scanner.nextInt();

        if(k <= a) {
            System.out.println(k);
        }
        else if(k <= a+b) {
            System.out.println(k-b);
        }
        else if (k <= a+b+c) {
            System.out.println(a-(k-(a+b)));
        }
    } main {
        final Scanner scanner = new Scanner(System.in);
        int a, b, c, k;

        a = scanner.nextInt();
        b = scanner.nextInt();
        c = scanner.nextInt();
        k = scanner.nextInt();

        if(k <= a) {
            System.out.println(k);
        }
        else if(k <= a+b) {
            System.out.println(k-b);
        }
        else if (k <= a+b+c) {
            System.out.println(a-(k-(a+b)));
        }
    } final Scanner scanner = new Scanner(System.in); scanner new Scanner(System.in) int a a b b c c k; k a = scanner.nextInt() a scanner.nextInt() scanner.nextInt scanner b = scanner.nextInt() b scanner.nextInt() scanner.nextInt scanner c = scanner.nextInt() c scanner.nextInt() scanner.nextInt scanner k = scanner.nextInt() k scanner.nextInt() scanner.nextInt scanner if(k <= a) {
            System.out.println(k);
        }
        else if(k <= a+b) {
            System.out.println(k-b);
        }
        else if (k <= a+b+c) {
            System.out.println(a-(k-(a+b)));
        } k <= a k a {
            System.out.println(k);
        } System.out.println(k) System.out.println System.out System System.out k if(k <= a+b) {
            System.out.println(k-b);
        }
        else if (k <= a+b+c) {
            System.out.println(a-(k-(a+b)));
        } k <= a+b k a+b a b {
            System.out.println(k-b);
        } System.out.println(k-b) System.out.println System.out System System.out k-b k b if (k <= a+b+c) {
            System.out.println(a-(k-(a+b)));
        } k <= a+b+c k a+b+c a+b+c a b c {
            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) {
        final Scanner scanner = new Scanner(System.in);
        int a, b, c, k;

        a = scanner.nextInt();
        b = scanner.nextInt();
        c = scanner.nextInt();
        k = scanner.nextInt();

        if(k <= a) {
            System.out.println(k);
        }
        else if(k <= a+b) {
            System.out.println(k-b);
        }
        else if (k <= a+b+c) {
            System.out.println(a-(k-(a+b)));
        }
    }
} public class Main {
    public static void main(String[] args) {
        final Scanner scanner = new Scanner(System.in);
        int a, b, c, k;

        a = scanner.nextInt();
        b = scanner.nextInt();
        c = scanner.nextInt();
        k = scanner.nextInt();

        if(k <= a) {
            System.out.println(k);
        }
        else if(k <= a+b) {
            System.out.println(k-b);
        }
        else if (k <= a+b+c) {
            System.out.println(a-(k-(a+b)));
        }
    }
} Main
import java.util.Scanner;

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

        a = scanner.nextInt();
        b = scanner.nextInt();
        c = scanner.nextInt();
        k = scanner.nextInt();

        if(k <= a) {
            System.out.println(k);
        }
        else if(k <= a+b) {
            System.out.println(k-b);
        }
        else if (k <= a+b+c) {
            System.out.println(a-(k-(a+b)));
        }
    }
}