import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        int A = sc.nextInt();
        int B = sc.nextInt();
        int C = sc.nextInt();
        int K = sc.nextInt();

        System.out.println(solve(A, B, C, K));

        sc.close();
    }

    static int solve(int A, int B, int C, int K) {
        int result = 0;

        int posCount = Math.min(A, K);
        result += posCount;
        K -= posCount;

        int zeroCount = Math.min(B, K);
        K -= zeroCount;

        result -= K;

        return result;
    }
} import java.util.Scanner; Scanner java.util public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        int A = sc.nextInt();
        int B = sc.nextInt();
        int C = sc.nextInt();
        int K = sc.nextInt();

        System.out.println(solve(A, B, C, K));

        sc.close();
    }

    static int solve(int A, int B, int C, int K) {
        int result = 0;

        int posCount = Math.min(A, K);
        result += posCount;
        K -= posCount;

        int zeroCount = Math.min(B, K);
        K -= zeroCount;

        result -= K;

        return result;
    }
} Main public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        int A = sc.nextInt();
        int B = sc.nextInt();
        int C = sc.nextInt();
        int K = sc.nextInt();

        System.out.println(solve(A, B, C, K));

        sc.close();
    } main {
        Scanner sc = new Scanner(System.in);

        int A = sc.nextInt();
        int B = sc.nextInt();
        int C = sc.nextInt();
        int K = sc.nextInt();

        System.out.println(solve(A, B, C, K));

        sc.close();
    } Scanner sc = new Scanner(System.in); sc new Scanner(System.in) int A = sc.nextInt(); A sc.nextInt() sc.nextInt sc int B = sc.nextInt(); B sc.nextInt() sc.nextInt sc int C = sc.nextInt(); C sc.nextInt() sc.nextInt sc int K = sc.nextInt(); K sc.nextInt() sc.nextInt sc System.out.println(solve(A, B, C, K)) System.out.println System.out System System.out solve(A, B, C, K) solve A B C K sc.close() sc.close sc String[] args args static int solve(int A, int B, int C, int K) {
        int result = 0;

        int posCount = Math.min(A, K);
        result += posCount;
        K -= posCount;

        int zeroCount = Math.min(B, K);
        K -= zeroCount;

        result -= K;

        return result;
    } solve {
        int result = 0;

        int posCount = Math.min(A, K);
        result += posCount;
        K -= posCount;

        int zeroCount = Math.min(B, K);
        K -= zeroCount;

        result -= K;

        return result;
    } int result = 0; result 0 int posCount = Math.min(A, K); posCount Math.min(A, K) Math.min Math A K result += posCount result posCount K -= posCount K posCount int zeroCount = Math.min(B, K); zeroCount Math.min(B, K) Math.min Math B K K -= zeroCount K zeroCount result -= K result K return result; result int A A int B B int C C int K K public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        int A = sc.nextInt();
        int B = sc.nextInt();
        int C = sc.nextInt();
        int K = sc.nextInt();

        System.out.println(solve(A, B, C, K));

        sc.close();
    }

    static int solve(int A, int B, int C, int K) {
        int result = 0;

        int posCount = Math.min(A, K);
        result += posCount;
        K -= posCount;

        int zeroCount = Math.min(B, K);
        K -= zeroCount;

        result -= K;

        return result;
    }
} public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        int A = sc.nextInt();
        int B = sc.nextInt();
        int C = sc.nextInt();
        int K = sc.nextInt();

        System.out.println(solve(A, B, C, K));

        sc.close();
    }

    static int solve(int A, int B, int C, int K) {
        int result = 0;

        int posCount = Math.min(A, K);
        result += posCount;
        K -= posCount;

        int zeroCount = Math.min(B, K);
        K -= zeroCount;

        result -= K;

        return result;
    }
} Main
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        int A = sc.nextInt();
        int B = sc.nextInt();
        int C = sc.nextInt();
        int K = sc.nextInt();

        System.out.println(solve(A, B, C, K));

        sc.close();
    }

    static int solve(int A, int B, int C, int K) {
        int result = 0;

        int posCount = Math.min(A, K);
        result += posCount;
        K -= posCount;

        int zeroCount = Math.min(B, K);
        K -= zeroCount;

        result -= K;

        return result;
    }
}