import java.util.*;

class Main {
    private static Scanner sc;

    public static void main(final String[] args) {
        sc = new Scanner(System.in);
        long a = sc.nextLong(),b = sc.nextLong(),c = sc.nextLong(),k = sc.nextLong(),ans;
        if(k <= a) ans = k;
        else if(k > a && k-a <= b) ans = a;
        else ans = a - (k-a-b);
        System.out.println(ans);
    }
}





 import java.util.*; util.* java class Main {
    private static Scanner sc;

    public static void main(final String[] args) {
        sc = new Scanner(System.in);
        long a = sc.nextLong(),b = sc.nextLong(),c = sc.nextLong(),k = sc.nextLong(),ans;
        if(k <= a) ans = k;
        else if(k > a && k-a <= b) ans = a;
        else ans = a - (k-a-b);
        System.out.println(ans);
    }
} Main private static Scanner sc; sc public static void main(final String[] args) {
        sc = new Scanner(System.in);
        long a = sc.nextLong(),b = sc.nextLong(),c = sc.nextLong(),k = sc.nextLong(),ans;
        if(k <= a) ans = k;
        else if(k > a && k-a <= b) ans = a;
        else ans = a - (k-a-b);
        System.out.println(ans);
    } main {
        sc = new Scanner(System.in);
        long a = sc.nextLong(),b = sc.nextLong(),c = sc.nextLong(),k = sc.nextLong(),ans;
        if(k <= a) ans = k;
        else if(k > a && k-a <= b) ans = a;
        else ans = a - (k-a-b);
        System.out.println(ans);
    } sc = new Scanner(System.in) sc new Scanner(System.in) long a = sc.nextLong() a sc.nextLong() sc.nextLong sc b = sc.nextLong() b sc.nextLong() sc.nextLong sc c = sc.nextLong() c sc.nextLong() sc.nextLong sc k = sc.nextLong() k sc.nextLong() sc.nextLong sc ans; ans if(k <= a) ans = k;
        else if(k > a && k-a <= b) ans = a;
        else ans = a - (k-a-b); k <= a k a ans = k ans k if(k > a && k-a <= b) ans = a;
        else ans = a - (k-a-b); k > a && k-a <= b k > a k a k-a <= b k-a k a b ans = a ans a ans = a - (k-a-b) ans a - (k-a-b) a (k-a-b) k-a-b k a b System.out.println(ans) System.out.println System.out System System.out ans final String[] args args
import java.util.*;

class Main {
    private static Scanner sc;

    public static void main(final String[] args) {
        sc = new Scanner(System.in);
        long a = sc.nextLong(),b = sc.nextLong(),c = sc.nextLong(),k = sc.nextLong(),ans;
        if(k <= a) ans = k;
        else if(k > a && k-a <= b) ans = a;
        else ans = a - (k-a-b);
        System.out.println(ans);
    }
}






