import java.util.*;

public class Main {
    public static void main(String[] args) throws Exception {
        // Your code here!
        Scanner sc = new Scanner(System.in);
        int a = sc.nextInt();
        int b = sc.nextInt();
        int c = sc.nextInt();
        int k = sc.nextInt();
        
        int ans = 0;
        if (k >= a){
            ans += a;
            k = k - a;
        }else{
            ans += k;
            k = 0;
            System.out.println(ans);
            return;
        }
        
        if (k >= b){
            k = k - b;
        }else{
            k = 0;
            System.out.println(ans);
            return;
        }
        
        if (k >= c){
            ans -= c;            
        }else{
            ans -= k;
        }

        System.out.println(ans);

    }
} import java.util.*; util.* java public class Main {
    public static void main(String[] args) throws Exception {
        // Your code here!
        Scanner sc = new Scanner(System.in);
        int a = sc.nextInt();
        int b = sc.nextInt();
        int c = sc.nextInt();
        int k = sc.nextInt();
        
        int ans = 0;
        if (k >= a){
            ans += a;
            k = k - a;
        }else{
            ans += k;
            k = 0;
            System.out.println(ans);
            return;
        }
        
        if (k >= b){
            k = k - b;
        }else{
            k = 0;
            System.out.println(ans);
            return;
        }
        
        if (k >= c){
            ans -= c;            
        }else{
            ans -= k;
        }

        System.out.println(ans);

    }
} Main public static void main(String[] args) throws Exception {
        // Your code here!
        Scanner sc = new Scanner(System.in);
        int a = sc.nextInt();
        int b = sc.nextInt();
        int c = sc.nextInt();
        int k = sc.nextInt();
        
        int ans = 0;
        if (k >= a){
            ans += a;
            k = k - a;
        }else{
            ans += k;
            k = 0;
            System.out.println(ans);
            return;
        }
        
        if (k >= b){
            k = k - b;
        }else{
            k = 0;
            System.out.println(ans);
            return;
        }
        
        if (k >= c){
            ans -= c;            
        }else{
            ans -= k;
        }

        System.out.println(ans);

    } main {
        // Your code here!
        Scanner sc = new Scanner(System.in);
        int a = sc.nextInt();
        int b = sc.nextInt();
        int c = sc.nextInt();
        int k = sc.nextInt();
        
        int ans = 0;
        if (k >= a){
            ans += a;
            k = k - a;
        }else{
            ans += k;
            k = 0;
            System.out.println(ans);
            return;
        }
        
        if (k >= b){
            k = k - b;
        }else{
            k = 0;
            System.out.println(ans);
            return;
        }
        
        if (k >= c){
            ans -= c;            
        }else{
            ans -= k;
        }

        System.out.println(ans);

    } 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 int ans = 0; ans 0 if (k >= a){
            ans += a;
            k = k - a;
        }else{
            ans += k;
            k = 0;
            System.out.println(ans);
            return;
        } k >= a k a {
            ans += a;
            k = k - a;
        } ans += a ans a k = k - a k k - a k a {
            ans += k;
            k = 0;
            System.out.println(ans);
            return;
        } ans += k ans k k = 0 k 0 System.out.println(ans) System.out.println System.out System System.out ans return; if (k >= b){
            k = k - b;
        }else{
            k = 0;
            System.out.println(ans);
            return;
        } k >= b k b {
            k = k - b;
        } k = k - b k k - b k b {
            k = 0;
            System.out.println(ans);
            return;
        } k = 0 k 0 System.out.println(ans) System.out.println System.out System System.out ans return; if (k >= c){
            ans -= c;            
        }else{
            ans -= k;
        } k >= c k c {
            ans -= c;            
        } ans -= c ans c {
            ans -= k;
        } ans -= k ans k System.out.println(ans) System.out.println System.out System System.out ans String[] args args public class Main {
    public static void main(String[] args) throws Exception {
        // Your code here!
        Scanner sc = new Scanner(System.in);
        int a = sc.nextInt();
        int b = sc.nextInt();
        int c = sc.nextInt();
        int k = sc.nextInt();
        
        int ans = 0;
        if (k >= a){
            ans += a;
            k = k - a;
        }else{
            ans += k;
            k = 0;
            System.out.println(ans);
            return;
        }
        
        if (k >= b){
            k = k - b;
        }else{
            k = 0;
            System.out.println(ans);
            return;
        }
        
        if (k >= c){
            ans -= c;            
        }else{
            ans -= k;
        }

        System.out.println(ans);

    }
} public class Main {
    public static void main(String[] args) throws Exception {
        // Your code here!
        Scanner sc = new Scanner(System.in);
        int a = sc.nextInt();
        int b = sc.nextInt();
        int c = sc.nextInt();
        int k = sc.nextInt();
        
        int ans = 0;
        if (k >= a){
            ans += a;
            k = k - a;
        }else{
            ans += k;
            k = 0;
            System.out.println(ans);
            return;
        }
        
        if (k >= b){
            k = k - b;
        }else{
            k = 0;
            System.out.println(ans);
            return;
        }
        
        if (k >= c){
            ans -= c;            
        }else{
            ans -= k;
        }

        System.out.println(ans);

    }
} Main
import java.util.*;

public class Main {
    public static void main(String[] args) throws Exception {
        // Your code here!
        Scanner sc = new Scanner(System.in);
        int a = sc.nextInt();
        int b = sc.nextInt();
        int c = sc.nextInt();
        int k = sc.nextInt();
        
        int ans = 0;
        if (k >= a){
            ans += a;
            k = k - a;
        }else{
            ans += k;
            k = 0;
            System.out.println(ans);
            return;
        }
        
        if (k >= b){
            k = k - b;
        }else{
            k = 0;
            System.out.println(ans);
            return;
        }
        
        if (k >= c){
            ans -= c;            
        }else{
            ans -= k;
        }

        System.out.println(ans);

    }
}
