import java.util.*;

public class Main {
    public static void main(String[] args) {
        Main main = new Main();
        main.run();
    }
    
    // 変数一覧
    long a;
    long b;
    long c;
    long k;
    long ans;

    public void run() {
        // 入力情報取得
        Scanner sc = new Scanner(System.in);
        a = sc.nextLong();
        b = sc.nextLong();
        c = sc.nextLong();
        k = sc.nextLong();
        sc.close();

        // 解答処理
        solve();
    }
    
    private void solve() {
        if (a < k) {
            ans = a;
            k -= a;                
        } else {
            ans = k;
            k = 0;
        }
        if (b < k && k > 0) {
            k -= b;
        } else {
            k = 0;
        }
        if (k > 0) {
            ans -= k;
        }
        System.out.println(ans);
    }
} import java.util.*; util.* java public class Main {
    public static void main(String[] args) {
        Main main = new Main();
        main.run();
    }
    
    // 変数一覧
    long a;
    long b;
    long c;
    long k;
    long ans;

    public void run() {
        // 入力情報取得
        Scanner sc = new Scanner(System.in);
        a = sc.nextLong();
        b = sc.nextLong();
        c = sc.nextLong();
        k = sc.nextLong();
        sc.close();

        // 解答処理
        solve();
    }
    
    private void solve() {
        if (a < k) {
            ans = a;
            k -= a;                
        } else {
            ans = k;
            k = 0;
        }
        if (b < k && k > 0) {
            k -= b;
        } else {
            k = 0;
        }
        if (k > 0) {
            ans -= k;
        }
        System.out.println(ans);
    }
} Main // 変数一覧
    long a; a long b; b long c; c long k; k long ans; ans public static void main(String[] args) {
        Main main = new Main();
        main.run();
    } main {
        Main main = new Main();
        main.run();
    } Main main = new Main(); main new Main() main.run() main.run main String[] args args public void run() {
        // 入力情報取得
        Scanner sc = new Scanner(System.in);
        a = sc.nextLong();
        b = sc.nextLong();
        c = sc.nextLong();
        k = sc.nextLong();
        sc.close();

        // 解答処理
        solve();
    } run {
        // 入力情報取得
        Scanner sc = new Scanner(System.in);
        a = sc.nextLong();
        b = sc.nextLong();
        c = sc.nextLong();
        k = sc.nextLong();
        sc.close();

        // 解答処理
        solve();
    } Scanner sc = new Scanner(System.in); sc new Scanner(System.in) 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 sc.close() sc.close sc solve() solve private void solve() {
        if (a < k) {
            ans = a;
            k -= a;                
        } else {
            ans = k;
            k = 0;
        }
        if (b < k && k > 0) {
            k -= b;
        } else {
            k = 0;
        }
        if (k > 0) {
            ans -= k;
        }
        System.out.println(ans);
    } solve {
        if (a < k) {
            ans = a;
            k -= a;                
        } else {
            ans = k;
            k = 0;
        }
        if (b < k && k > 0) {
            k -= b;
        } else {
            k = 0;
        }
        if (k > 0) {
            ans -= k;
        }
        System.out.println(ans);
    } if (a < k) {
            ans = a;
            k -= a;                
        } else {
            ans = k;
            k = 0;
        } a < k a k {
            ans = a;
            k -= a;                
        } ans = a ans a k -= a k a {
            ans = k;
            k = 0;
        } ans = k ans k k = 0 k 0 if (b < k && k > 0) {
            k -= b;
        } else {
            k = 0;
        } b < k && k > 0 b < k b k k > 0 k 0 {
            k -= b;
        } k -= b k b {
            k = 0;
        } k = 0 k 0 if (k > 0) {
            ans -= k;
        } k > 0 k 0 {
            ans -= k;
        } ans -= k ans k System.out.println(ans) System.out.println System.out System System.out ans public class Main {
    public static void main(String[] args) {
        Main main = new Main();
        main.run();
    }
    
    // 変数一覧
    long a;
    long b;
    long c;
    long k;
    long ans;

    public void run() {
        // 入力情報取得
        Scanner sc = new Scanner(System.in);
        a = sc.nextLong();
        b = sc.nextLong();
        c = sc.nextLong();
        k = sc.nextLong();
        sc.close();

        // 解答処理
        solve();
    }
    
    private void solve() {
        if (a < k) {
            ans = a;
            k -= a;                
        } else {
            ans = k;
            k = 0;
        }
        if (b < k && k > 0) {
            k -= b;
        } else {
            k = 0;
        }
        if (k > 0) {
            ans -= k;
        }
        System.out.println(ans);
    }
} public class Main {
    public static void main(String[] args) {
        Main main = new Main();
        main.run();
    }
    
    // 変数一覧
    long a;
    long b;
    long c;
    long k;
    long ans;

    public void run() {
        // 入力情報取得
        Scanner sc = new Scanner(System.in);
        a = sc.nextLong();
        b = sc.nextLong();
        c = sc.nextLong();
        k = sc.nextLong();
        sc.close();

        // 解答処理
        solve();
    }
    
    private void solve() {
        if (a < k) {
            ans = a;
            k -= a;                
        } else {
            ans = k;
            k = 0;
        }
        if (b < k && k > 0) {
            k -= b;
        } else {
            k = 0;
        }
        if (k > 0) {
            ans -= k;
        }
        System.out.println(ans);
    }
} Main
import java.util.*;

public class Main {
    public static void main(String[] args) {
        Main main = new Main();
        main.run();
    }
    
    // 変数一覧
    long a;
    long b;
    long c;
    long k;
    long ans;

    public void run() {
        // 入力情報取得
        Scanner sc = new Scanner(System.in);
        a = sc.nextLong();
        b = sc.nextLong();
        c = sc.nextLong();
        k = sc.nextLong();
        sc.close();

        // 解答処理
        solve();
    }
    
    private void solve() {
        if (a < k) {
            ans = a;
            k -= a;                
        } else {
            ans = k;
            k = 0;
        }
        if (b < k && k > 0) {
            k -= b;
        } else {
            k = 0;
        }
        if (k > 0) {
            ans -= k;
        }
        System.out.println(ans);
    }
}