import java.util.Scanner;

public class Main {

    static int f(int a, int b, int c, int k) {
        if (k <= a) return k;
        if (k <= a + b) return a;
        return a - (k - a - b);
    }
    
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int a = in.nextInt(), b = in.nextInt(), c = in.nextInt(), k = in.nextInt();
        System.out.println(f(a, b, c, k));
    }
} import java.util.Scanner; Scanner java.util public class Main {

    static int f(int a, int b, int c, int k) {
        if (k <= a) return k;
        if (k <= a + b) return a;
        return a - (k - a - b);
    }
    
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int a = in.nextInt(), b = in.nextInt(), c = in.nextInt(), k = in.nextInt();
        System.out.println(f(a, b, c, k));
    }
} Main static int f(int a, int b, int c, int k) {
        if (k <= a) return k;
        if (k <= a + b) return a;
        return a - (k - a - b);
    } f {
        if (k <= a) return k;
        if (k <= a + b) return a;
        return a - (k - a - b);
    } if (k <= a) return k; k <= a k a return k; k if (k <= a + b) return a; k <= a + b k a + b a b return a; a return a - (k - a - b); a - (k - a - b) a (k - a - b) k - a - b k a b int a a int b b int c c int k k public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int a = in.nextInt(), b = in.nextInt(), c = in.nextInt(), k = in.nextInt();
        System.out.println(f(a, b, c, k));
    } main {
        Scanner in = new Scanner(System.in);
        int a = in.nextInt(), b = in.nextInt(), c = in.nextInt(), k = in.nextInt();
        System.out.println(f(a, b, c, k));
    } Scanner in = new Scanner(System.in); in new Scanner(System.in) int a = in.nextInt() a in.nextInt() in.nextInt in b = in.nextInt() b in.nextInt() in.nextInt in c = in.nextInt() c in.nextInt() in.nextInt in k = in.nextInt(); k in.nextInt() in.nextInt in System.out.println(f(a, b, c, k)) System.out.println System.out System System.out f(a, b, c, k) f a b c k String[] args args public class Main {

    static int f(int a, int b, int c, int k) {
        if (k <= a) return k;
        if (k <= a + b) return a;
        return a - (k - a - b);
    }
    
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int a = in.nextInt(), b = in.nextInt(), c = in.nextInt(), k = in.nextInt();
        System.out.println(f(a, b, c, k));
    }
} public class Main {

    static int f(int a, int b, int c, int k) {
        if (k <= a) return k;
        if (k <= a + b) return a;
        return a - (k - a - b);
    }
    
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int a = in.nextInt(), b = in.nextInt(), c = in.nextInt(), k = in.nextInt();
        System.out.println(f(a, b, c, k));
    }
} Main
import java.util.Scanner;

public class Main {

    static int f(int a, int b, int c, int k) {
        if (k <= a) return k;
        if (k <= a + b) return a;
        return a - (k - a - b);
    }
    
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int a = in.nextInt(), b = in.nextInt(), c = in.nextInt(), k = in.nextInt();
        System.out.println(f(a, b, c, k));
    }
}
