import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        long A = scanner.nextLong();
        long B = scanner.nextLong();
        long C = scanner.nextLong();

        long K = scanner.nextLong();

        long result;

        if (A >= K) {
            result = K;
        }
        else if (B >= K - A) {
            result = A;
        } else {
            result = -1 * (K - A - B) + A;
        }

        System.out.println(result);
    }
} import java.util.Scanner; Scanner java.util public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        long A = scanner.nextLong();
        long B = scanner.nextLong();
        long C = scanner.nextLong();

        long K = scanner.nextLong();

        long result;

        if (A >= K) {
            result = K;
        }
        else if (B >= K - A) {
            result = A;
        } else {
            result = -1 * (K - A - B) + A;
        }

        System.out.println(result);
    }
} Main public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        long A = scanner.nextLong();
        long B = scanner.nextLong();
        long C = scanner.nextLong();

        long K = scanner.nextLong();

        long result;

        if (A >= K) {
            result = K;
        }
        else if (B >= K - A) {
            result = A;
        } else {
            result = -1 * (K - A - B) + A;
        }

        System.out.println(result);
    } main {
        Scanner scanner = new Scanner(System.in);
        long A = scanner.nextLong();
        long B = scanner.nextLong();
        long C = scanner.nextLong();

        long K = scanner.nextLong();

        long result;

        if (A >= K) {
            result = K;
        }
        else if (B >= K - A) {
            result = A;
        } else {
            result = -1 * (K - A - B) + A;
        }

        System.out.println(result);
    } Scanner scanner = new Scanner(System.in); scanner new Scanner(System.in) long A = scanner.nextLong(); A scanner.nextLong() scanner.nextLong scanner long B = scanner.nextLong(); B scanner.nextLong() scanner.nextLong scanner long C = scanner.nextLong(); C scanner.nextLong() scanner.nextLong scanner long K = scanner.nextLong(); K scanner.nextLong() scanner.nextLong scanner long result; result if (A >= K) {
            result = K;
        }
        else if (B >= K - A) {
            result = A;
        } else {
            result = -1 * (K - A - B) + A;
        } A >= K A K {
            result = K;
        } result = K result K if (B >= K - A) {
            result = A;
        } else {
            result = -1 * (K - A - B) + A;
        } B >= K - A B K - A K A {
            result = A;
        } result = A result A {
            result = -1 * (K - A - B) + A;
        } result = -1 * (K - A - B) + A result -1 * (K - A - B) + A -1 * (K - A - B) -1 1 (K - A - B) K - A - B K A B A System.out.println(result) System.out.println System.out System System.out result String[] args args public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        long A = scanner.nextLong();
        long B = scanner.nextLong();
        long C = scanner.nextLong();

        long K = scanner.nextLong();

        long result;

        if (A >= K) {
            result = K;
        }
        else if (B >= K - A) {
            result = A;
        } else {
            result = -1 * (K - A - B) + A;
        }

        System.out.println(result);
    }
} public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        long A = scanner.nextLong();
        long B = scanner.nextLong();
        long C = scanner.nextLong();

        long K = scanner.nextLong();

        long result;

        if (A >= K) {
            result = K;
        }
        else if (B >= K - A) {
            result = A;
        } else {
            result = -1 * (K - A - B) + A;
        }

        System.out.println(result);
    }
} Main
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        long A = scanner.nextLong();
        long B = scanner.nextLong();
        long C = scanner.nextLong();

        long K = scanner.nextLong();

        long result;

        if (A >= K) {
            result = K;
        }
        else if (B >= K - A) {
            result = A;
        } else {
            result = -1 * (K - A - B) + A;
        }

        System.out.println(result);
    }
}
