import java.io.BufferedInputStream;
import java.util.Scanner;

public class Main {
    private static Scanner sc = new Scanner(new BufferedInputStream(System.in));

    public static void main(String[] args) {
        int a = sc.nextInt();
        int b = sc.nextInt();
        int c = sc.nextInt();
        int k = sc.nextInt();
        long sum = 0;
        if (a >= k) {
            sum += k;
            k = 0;
        } else {
            sum += a;
            k -= a;
        }
        k -= b;
        if (k > 0) {
            sum -= k;
        }
        System.out.println(sum);
    }
} import java.io.BufferedInputStream; BufferedInputStream java.io import java.util.Scanner; Scanner java.util public class Main {
    private static Scanner sc = new Scanner(new BufferedInputStream(System.in));

    public static void main(String[] args) {
        int a = sc.nextInt();
        int b = sc.nextInt();
        int c = sc.nextInt();
        int k = sc.nextInt();
        long sum = 0;
        if (a >= k) {
            sum += k;
            k = 0;
        } else {
            sum += a;
            k -= a;
        }
        k -= b;
        if (k > 0) {
            sum -= k;
        }
        System.out.println(sum);
    }
} Main private static Scanner sc = new Scanner(new BufferedInputStream(System.in)); sc new Scanner(new BufferedInputStream(System.in)) public static void main(String[] args) {
        int a = sc.nextInt();
        int b = sc.nextInt();
        int c = sc.nextInt();
        int k = sc.nextInt();
        long sum = 0;
        if (a >= k) {
            sum += k;
            k = 0;
        } else {
            sum += a;
            k -= a;
        }
        k -= b;
        if (k > 0) {
            sum -= k;
        }
        System.out.println(sum);
    } main {
        int a = sc.nextInt();
        int b = sc.nextInt();
        int c = sc.nextInt();
        int k = sc.nextInt();
        long sum = 0;
        if (a >= k) {
            sum += k;
            k = 0;
        } else {
            sum += a;
            k -= a;
        }
        k -= b;
        if (k > 0) {
            sum -= k;
        }
        System.out.println(sum);
    } 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 long sum = 0; sum 0 if (a >= k) {
            sum += k;
            k = 0;
        } else {
            sum += a;
            k -= a;
        } a >= k a k {
            sum += k;
            k = 0;
        } sum += k sum k k = 0 k 0 {
            sum += a;
            k -= a;
        } sum += a sum a k -= a k a k -= b k b if (k > 0) {
            sum -= k;
        } k > 0 k 0 {
            sum -= k;
        } sum -= k sum k System.out.println(sum) System.out.println System.out System System.out sum String[] args args public class Main {
    private static Scanner sc = new Scanner(new BufferedInputStream(System.in));

    public static void main(String[] args) {
        int a = sc.nextInt();
        int b = sc.nextInt();
        int c = sc.nextInt();
        int k = sc.nextInt();
        long sum = 0;
        if (a >= k) {
            sum += k;
            k = 0;
        } else {
            sum += a;
            k -= a;
        }
        k -= b;
        if (k > 0) {
            sum -= k;
        }
        System.out.println(sum);
    }
} public class Main {
    private static Scanner sc = new Scanner(new BufferedInputStream(System.in));

    public static void main(String[] args) {
        int a = sc.nextInt();
        int b = sc.nextInt();
        int c = sc.nextInt();
        int k = sc.nextInt();
        long sum = 0;
        if (a >= k) {
            sum += k;
            k = 0;
        } else {
            sum += a;
            k -= a;
        }
        k -= b;
        if (k > 0) {
            sum -= k;
        }
        System.out.println(sum);
    }
} Main
import java.io.BufferedInputStream;
import java.util.Scanner;

public class Main {
    private static Scanner sc = new Scanner(new BufferedInputStream(System.in));

    public static void main(String[] args) {
        int a = sc.nextInt();
        int b = sc.nextInt();
        int c = sc.nextInt();
        int k = sc.nextInt();
        long sum = 0;
        if (a >= k) {
            sum += k;
            k = 0;
        } else {
            sum += a;
            k -= a;
        }
        k -= b;
        if (k > 0) {
            sum -= k;
        }
        System.out.println(sum);
    }
}