import java.util.Scanner;

public class Main {

    private static Scanner scanner = new Scanner(System.in);

    public static void main(String[] args) {

        solve();
    }

    private static void solve() {

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


        int count = 0;
        int result = Math.min(a, k);
        count = Math.min(a, k);

        count += b;
        result -= Math.max(k - count, 0);

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

    private static Scanner scanner = new Scanner(System.in);

    public static void main(String[] args) {

        solve();
    }

    private static void solve() {

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


        int count = 0;
        int result = Math.min(a, k);
        count = Math.min(a, k);

        count += b;
        result -= Math.max(k - count, 0);

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

        solve();
    } main {

        solve();
    } solve() solve String[] args args private static void solve() {

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


        int count = 0;
        int result = Math.min(a, k);
        count = Math.min(a, k);

        count += b;
        result -= Math.max(k - count, 0);

        System.out.println(result);
    } solve {

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


        int count = 0;
        int result = Math.min(a, k);
        count = Math.min(a, k);

        count += b;
        result -= Math.max(k - count, 0);

        System.out.println(result);
    } int a = scanner.nextInt(); a scanner.nextInt() scanner.nextInt scanner int b = scanner.nextInt(); b scanner.nextInt() scanner.nextInt scanner int c = scanner.nextInt(); c scanner.nextInt() scanner.nextInt scanner int k = scanner.nextInt(); k scanner.nextInt() scanner.nextInt scanner int count = 0; count 0 int result = Math.min(a, k); result Math.min(a, k) Math.min Math a k count = Math.min(a, k) count Math.min(a, k) Math.min Math a k count += b count b result -= Math.max(k - count, 0) result Math.max(k - count, 0) Math.max Math k - count k count 0 System.out.println(result) System.out.println System.out System System.out result public class Main {

    private static Scanner scanner = new Scanner(System.in);

    public static void main(String[] args) {

        solve();
    }

    private static void solve() {

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


        int count = 0;
        int result = Math.min(a, k);
        count = Math.min(a, k);

        count += b;
        result -= Math.max(k - count, 0);

        System.out.println(result);
    }
} public class Main {

    private static Scanner scanner = new Scanner(System.in);

    public static void main(String[] args) {

        solve();
    }

    private static void solve() {

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


        int count = 0;
        int result = Math.min(a, k);
        count = Math.min(a, k);

        count += b;
        result -= Math.max(k - count, 0);

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

public class Main {

    private static Scanner scanner = new Scanner(System.in);

    public static void main(String[] args) {

        solve();
    }

    private static void solve() {

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


        int count = 0;
        int result = Math.min(a, k);
        count = Math.min(a, k);

        count += b;
        result -= Math.max(k - count, 0);

        System.out.println(result);
    }
}