import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int a = sc.nextInt();
        int b = sc.nextInt();
        int c = sc.nextInt();
        int k = sc.nextInt();
        long ans = 0;
        ans = Math.min(a, k);
        k = k - a;
        if(k <= 0) {
            System.out.println(ans);
            return;
        }
        k = k - b;
        if(k <= 0) {
            System.out.println(ans);
            return;
        }
        k = Math.min(c, k);
        System.out.println(ans - k);
    }
} 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();
        int b = sc.nextInt();
        int c = sc.nextInt();
        int k = sc.nextInt();
        long ans = 0;
        ans = Math.min(a, k);
        k = k - a;
        if(k <= 0) {
            System.out.println(ans);
            return;
        }
        k = k - b;
        if(k <= 0) {
            System.out.println(ans);
            return;
        }
        k = Math.min(c, k);
        System.out.println(ans - k);
    }
} Main public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int a = sc.nextInt();
        int b = sc.nextInt();
        int c = sc.nextInt();
        int k = sc.nextInt();
        long ans = 0;
        ans = Math.min(a, k);
        k = k - a;
        if(k <= 0) {
            System.out.println(ans);
            return;
        }
        k = k - b;
        if(k <= 0) {
            System.out.println(ans);
            return;
        }
        k = Math.min(c, k);
        System.out.println(ans - k);
    } main {
        Scanner sc = new Scanner(System.in);
        int a = sc.nextInt();
        int b = sc.nextInt();
        int c = sc.nextInt();
        int k = sc.nextInt();
        long ans = 0;
        ans = Math.min(a, k);
        k = k - a;
        if(k <= 0) {
            System.out.println(ans);
            return;
        }
        k = k - b;
        if(k <= 0) {
            System.out.println(ans);
            return;
        }
        k = Math.min(c, k);
        System.out.println(ans - k);
    } Scanner sc = new Scanner(System.in); sc new Scanner(System.in) 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 ans = 0; ans 0 ans = Math.min(a, k) ans Math.min(a, k) Math.min Math a k k = k - a k k - a k a if(k <= 0) {
            System.out.println(ans);
            return;
        } k <= 0 k 0 {
            System.out.println(ans);
            return;
        } System.out.println(ans) System.out.println System.out System System.out ans return; k = k - b k k - b k b if(k <= 0) {
            System.out.println(ans);
            return;
        } k <= 0 k 0 {
            System.out.println(ans);
            return;
        } System.out.println(ans) System.out.println System.out System System.out ans return; k = Math.min(c, k) k Math.min(c, k) Math.min Math c k System.out.println(ans - k) System.out.println System.out System System.out ans - k ans k String[] args args public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int a = sc.nextInt();
        int b = sc.nextInt();
        int c = sc.nextInt();
        int k = sc.nextInt();
        long ans = 0;
        ans = Math.min(a, k);
        k = k - a;
        if(k <= 0) {
            System.out.println(ans);
            return;
        }
        k = k - b;
        if(k <= 0) {
            System.out.println(ans);
            return;
        }
        k = Math.min(c, k);
        System.out.println(ans - k);
    }
} public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int a = sc.nextInt();
        int b = sc.nextInt();
        int c = sc.nextInt();
        int k = sc.nextInt();
        long ans = 0;
        ans = Math.min(a, k);
        k = k - a;
        if(k <= 0) {
            System.out.println(ans);
            return;
        }
        k = k - b;
        if(k <= 0) {
            System.out.println(ans);
            return;
        }
        k = Math.min(c, k);
        System.out.println(ans - k);
    }
} Main
import java.util.Scanner;

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