import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int a = sc.nextInt(), b = sc.nextInt(), c = sc.nextInt() , k = sc.nextInt();
        String s = "a";
        int result = 0;
        
        int minA =  Math.min(a, k);
        result += minA;
        
        k -= minA;
        if (k < 0) {
            System.out.println(result);
            return;
        }
        
        k -= b;
        if (k < 0) {
            System.out.println(result);
            return;
        }

        int minC =  Math.min(c, k);
        result -= minC;

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

        int minC =  Math.min(c, k);
        result -= minC;

        System.out.println(result);
    }
} Main public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int a = sc.nextInt(), b = sc.nextInt(), c = sc.nextInt() , k = sc.nextInt();
        String s = "a";
        int result = 0;
        
        int minA =  Math.min(a, k);
        result += minA;
        
        k -= minA;
        if (k < 0) {
            System.out.println(result);
            return;
        }
        
        k -= b;
        if (k < 0) {
            System.out.println(result);
            return;
        }

        int minC =  Math.min(c, k);
        result -= minC;

        System.out.println(result);
    } main {
        Scanner sc = new Scanner(System.in);
        int a = sc.nextInt(), b = sc.nextInt(), c = sc.nextInt() , k = sc.nextInt();
        String s = "a";
        int result = 0;
        
        int minA =  Math.min(a, k);
        result += minA;
        
        k -= minA;
        if (k < 0) {
            System.out.println(result);
            return;
        }
        
        k -= b;
        if (k < 0) {
            System.out.println(result);
            return;
        }

        int minC =  Math.min(c, k);
        result -= minC;

        System.out.println(result);
    } Scanner sc = new Scanner(System.in); sc new Scanner(System.in) int a = sc.nextInt() a sc.nextInt() sc.nextInt sc b = sc.nextInt() b sc.nextInt() sc.nextInt sc c = sc.nextInt() c sc.nextInt() sc.nextInt sc k = sc.nextInt(); k sc.nextInt() sc.nextInt sc String s = "a"; s "a" int result = 0; result 0 int minA =  Math.min(a, k); minA Math.min(a, k) Math.min Math a k result += minA result minA k -= minA k minA if (k < 0) {
            System.out.println(result);
            return;
        } k < 0 k 0 {
            System.out.println(result);
            return;
        } System.out.println(result) System.out.println System.out System System.out result return; k -= b k b if (k < 0) {
            System.out.println(result);
            return;
        } k < 0 k 0 {
            System.out.println(result);
            return;
        } System.out.println(result) System.out.println System.out System System.out result return; int minC =  Math.min(c, k); minC Math.min(c, k) Math.min Math c k result -= minC result minC System.out.println(result) System.out.println System.out System System.out result String[] args args public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int a = sc.nextInt(), b = sc.nextInt(), c = sc.nextInt() , k = sc.nextInt();
        String s = "a";
        int result = 0;
        
        int minA =  Math.min(a, k);
        result += minA;
        
        k -= minA;
        if (k < 0) {
            System.out.println(result);
            return;
        }
        
        k -= b;
        if (k < 0) {
            System.out.println(result);
            return;
        }

        int minC =  Math.min(c, k);
        result -= minC;

        System.out.println(result);
    }
} public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int a = sc.nextInt(), b = sc.nextInt(), c = sc.nextInt() , k = sc.nextInt();
        String s = "a";
        int result = 0;
        
        int minA =  Math.min(a, k);
        result += minA;
        
        k -= minA;
        if (k < 0) {
            System.out.println(result);
            return;
        }
        
        k -= b;
        if (k < 0) {
            System.out.println(result);
            return;
        }

        int minC =  Math.min(c, k);
        result -= minC;

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

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int a = sc.nextInt(), b = sc.nextInt(), c = sc.nextInt() , k = sc.nextInt();
        String s = "a";
        int result = 0;
        
        int minA =  Math.min(a, k);
        result += minA;
        
        k -= minA;
        if (k < 0) {
            System.out.println(result);
            return;
        }
        
        k -= b;
        if (k < 0) {
            System.out.println(result);
            return;
        }

        int minC =  Math.min(c, k);
        result -= minC;

        System.out.println(result);
    }
}