
import java.util.*;

public class Main {
    public static void main(final String[] args) {
        final Scanner sc = new Scanner(System.in);
        // 整数の入力
        // final int a = sc.nextInt();
        // 文字列の入力
        // final String s1 = sc.next();
        final int a = sc.nextInt();
        final int b = sc.nextInt();
        final int c = sc.nextInt();
        final int k = sc.nextInt();
        sc.close();
        int result = 0;
        if (a >= k) {
            result = k;
        } else if (b >= k - a) {
            result = a;
        } else if (c >= k - b - a) {
            result = 2 * a + b - k;
        }

        System.out.println(result);
    }
} import java.util.*; util.* java public class Main {
    public static void main(final String[] args) {
        final Scanner sc = new Scanner(System.in);
        // 整数の入力
        // final int a = sc.nextInt();
        // 文字列の入力
        // final String s1 = sc.next();
        final int a = sc.nextInt();
        final int b = sc.nextInt();
        final int c = sc.nextInt();
        final int k = sc.nextInt();
        sc.close();
        int result = 0;
        if (a >= k) {
            result = k;
        } else if (b >= k - a) {
            result = a;
        } else if (c >= k - b - a) {
            result = 2 * a + b - k;
        }

        System.out.println(result);
    }
} Main public static void main(final String[] args) {
        final Scanner sc = new Scanner(System.in);
        // 整数の入力
        // final int a = sc.nextInt();
        // 文字列の入力
        // final String s1 = sc.next();
        final int a = sc.nextInt();
        final int b = sc.nextInt();
        final int c = sc.nextInt();
        final int k = sc.nextInt();
        sc.close();
        int result = 0;
        if (a >= k) {
            result = k;
        } else if (b >= k - a) {
            result = a;
        } else if (c >= k - b - a) {
            result = 2 * a + b - k;
        }

        System.out.println(result);
    } main {
        final Scanner sc = new Scanner(System.in);
        // 整数の入力
        // final int a = sc.nextInt();
        // 文字列の入力
        // final String s1 = sc.next();
        final int a = sc.nextInt();
        final int b = sc.nextInt();
        final int c = sc.nextInt();
        final int k = sc.nextInt();
        sc.close();
        int result = 0;
        if (a >= k) {
            result = k;
        } else if (b >= k - a) {
            result = a;
        } else if (c >= k - b - a) {
            result = 2 * a + b - k;
        }

        System.out.println(result);
    } final Scanner sc = new Scanner(System.in); sc new Scanner(System.in) final int a = sc.nextInt(); a sc.nextInt() sc.nextInt sc final int b = sc.nextInt(); b sc.nextInt() sc.nextInt sc final int c = sc.nextInt(); c sc.nextInt() sc.nextInt sc final int k = sc.nextInt(); k sc.nextInt() sc.nextInt sc sc.close() sc.close sc int result = 0; result 0 if (a >= k) {
            result = k;
        } else if (b >= k - a) {
            result = a;
        } else if (c >= k - b - a) {
            result = 2 * a + b - k;
        } a >= k a k {
            result = k;
        } result = k result k if (b >= k - a) {
            result = a;
        } else if (c >= k - b - a) {
            result = 2 * a + b - k;
        } b >= k - a b k - a k a {
            result = a;
        } result = a result a if (c >= k - b - a) {
            result = 2 * a + b - k;
        } c >= k - b - a c k - b - a k - b - a k b a {
            result = 2 * a + b - k;
        } result = 2 * a + b - k result 2 * a + b - k 2 * a + b 2 * a 2 a b k System.out.println(result) System.out.println System.out System System.out result final String[] args args public class Main {
    public static void main(final String[] args) {
        final Scanner sc = new Scanner(System.in);
        // 整数の入力
        // final int a = sc.nextInt();
        // 文字列の入力
        // final String s1 = sc.next();
        final int a = sc.nextInt();
        final int b = sc.nextInt();
        final int c = sc.nextInt();
        final int k = sc.nextInt();
        sc.close();
        int result = 0;
        if (a >= k) {
            result = k;
        } else if (b >= k - a) {
            result = a;
        } else if (c >= k - b - a) {
            result = 2 * a + b - k;
        }

        System.out.println(result);
    }
} public class Main {
    public static void main(final String[] args) {
        final Scanner sc = new Scanner(System.in);
        // 整数の入力
        // final int a = sc.nextInt();
        // 文字列の入力
        // final String s1 = sc.next();
        final int a = sc.nextInt();
        final int b = sc.nextInt();
        final int c = sc.nextInt();
        final int k = sc.nextInt();
        sc.close();
        int result = 0;
        if (a >= k) {
            result = k;
        } else if (b >= k - a) {
            result = a;
        } else if (c >= k - b - a) {
            result = 2 * a + b - k;
        }

        System.out.println(result);
    }
} Main

import java.util.*;

public class Main {
    public static void main(final String[] args) {
        final Scanner sc = new Scanner(System.in);
        // 整数の入力
        // final int a = sc.nextInt();
        // 文字列の入力
        // final String s1 = sc.next();
        final int a = sc.nextInt();
        final int b = sc.nextInt();
        final int c = sc.nextInt();
        final int k = sc.nextInt();
        sc.close();
        int result = 0;
        if (a >= k) {
            result = k;
        } else if (b >= k - a) {
            result = a;
        } else if (c >= k - b - a) {
            result = 2 * a + b - k;
        }

        System.out.println(result);
    }
}
