import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int a = Integer.parseInt(sc.next());
        int b = Integer.parseInt(sc.next());
        int c = Integer.parseInt(sc.next());
        int k = Integer.parseInt(sc.next());
        int ans = 0;
        if (k <= a) {
            ans += k;
            System.out.println(ans);
        } else {
            k -= a;
            ans += a;
            if (k <= b) {
                System.out.println(ans);
            } else {
                k -= b;
                if (k <= c) {
                    ans += k * -1;
                    System.out.println(ans);
                } else {
                    ans += k * -1;
                    System.out.println(ans);
                }
            }
        }

        sc.close();
    }

} import java.util.Scanner; Scanner java.util public class Main {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int a = Integer.parseInt(sc.next());
        int b = Integer.parseInt(sc.next());
        int c = Integer.parseInt(sc.next());
        int k = Integer.parseInt(sc.next());
        int ans = 0;
        if (k <= a) {
            ans += k;
            System.out.println(ans);
        } else {
            k -= a;
            ans += a;
            if (k <= b) {
                System.out.println(ans);
            } else {
                k -= b;
                if (k <= c) {
                    ans += k * -1;
                    System.out.println(ans);
                } else {
                    ans += k * -1;
                    System.out.println(ans);
                }
            }
        }

        sc.close();
    }

} Main public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int a = Integer.parseInt(sc.next());
        int b = Integer.parseInt(sc.next());
        int c = Integer.parseInt(sc.next());
        int k = Integer.parseInt(sc.next());
        int ans = 0;
        if (k <= a) {
            ans += k;
            System.out.println(ans);
        } else {
            k -= a;
            ans += a;
            if (k <= b) {
                System.out.println(ans);
            } else {
                k -= b;
                if (k <= c) {
                    ans += k * -1;
                    System.out.println(ans);
                } else {
                    ans += k * -1;
                    System.out.println(ans);
                }
            }
        }

        sc.close();
    } main {
        Scanner sc = new Scanner(System.in);
        int a = Integer.parseInt(sc.next());
        int b = Integer.parseInt(sc.next());
        int c = Integer.parseInt(sc.next());
        int k = Integer.parseInt(sc.next());
        int ans = 0;
        if (k <= a) {
            ans += k;
            System.out.println(ans);
        } else {
            k -= a;
            ans += a;
            if (k <= b) {
                System.out.println(ans);
            } else {
                k -= b;
                if (k <= c) {
                    ans += k * -1;
                    System.out.println(ans);
                } else {
                    ans += k * -1;
                    System.out.println(ans);
                }
            }
        }

        sc.close();
    } Scanner sc = new Scanner(System.in); sc new Scanner(System.in) int a = Integer.parseInt(sc.next()); a Integer.parseInt(sc.next()) Integer.parseInt Integer sc.next() sc.next sc int b = Integer.parseInt(sc.next()); b Integer.parseInt(sc.next()) Integer.parseInt Integer sc.next() sc.next sc int c = Integer.parseInt(sc.next()); c Integer.parseInt(sc.next()) Integer.parseInt Integer sc.next() sc.next sc int k = Integer.parseInt(sc.next()); k Integer.parseInt(sc.next()) Integer.parseInt Integer sc.next() sc.next sc int ans = 0; ans 0 if (k <= a) {
            ans += k;
            System.out.println(ans);
        } else {
            k -= a;
            ans += a;
            if (k <= b) {
                System.out.println(ans);
            } else {
                k -= b;
                if (k <= c) {
                    ans += k * -1;
                    System.out.println(ans);
                } else {
                    ans += k * -1;
                    System.out.println(ans);
                }
            }
        } k <= a k a {
            ans += k;
            System.out.println(ans);
        } ans += k ans k System.out.println(ans) System.out.println System.out System System.out ans {
            k -= a;
            ans += a;
            if (k <= b) {
                System.out.println(ans);
            } else {
                k -= b;
                if (k <= c) {
                    ans += k * -1;
                    System.out.println(ans);
                } else {
                    ans += k * -1;
                    System.out.println(ans);
                }
            }
        } k -= a k a ans += a ans a if (k <= b) {
                System.out.println(ans);
            } else {
                k -= b;
                if (k <= c) {
                    ans += k * -1;
                    System.out.println(ans);
                } else {
                    ans += k * -1;
                    System.out.println(ans);
                }
            } k <= b k b {
                System.out.println(ans);
            } System.out.println(ans) System.out.println System.out System System.out ans {
                k -= b;
                if (k <= c) {
                    ans += k * -1;
                    System.out.println(ans);
                } else {
                    ans += k * -1;
                    System.out.println(ans);
                }
            } k -= b k b if (k <= c) {
                    ans += k * -1;
                    System.out.println(ans);
                } else {
                    ans += k * -1;
                    System.out.println(ans);
                } k <= c k c {
                    ans += k * -1;
                    System.out.println(ans);
                } ans += k * -1 ans k * -1 k -1 1 System.out.println(ans) System.out.println System.out System System.out ans {
                    ans += k * -1;
                    System.out.println(ans);
                } ans += k * -1 ans k * -1 k -1 1 System.out.println(ans) System.out.println System.out System System.out ans sc.close() sc.close sc String[] args args public class Main {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int a = Integer.parseInt(sc.next());
        int b = Integer.parseInt(sc.next());
        int c = Integer.parseInt(sc.next());
        int k = Integer.parseInt(sc.next());
        int ans = 0;
        if (k <= a) {
            ans += k;
            System.out.println(ans);
        } else {
            k -= a;
            ans += a;
            if (k <= b) {
                System.out.println(ans);
            } else {
                k -= b;
                if (k <= c) {
                    ans += k * -1;
                    System.out.println(ans);
                } else {
                    ans += k * -1;
                    System.out.println(ans);
                }
            }
        }

        sc.close();
    }

} public class Main {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int a = Integer.parseInt(sc.next());
        int b = Integer.parseInt(sc.next());
        int c = Integer.parseInt(sc.next());
        int k = Integer.parseInt(sc.next());
        int ans = 0;
        if (k <= a) {
            ans += k;
            System.out.println(ans);
        } else {
            k -= a;
            ans += a;
            if (k <= b) {
                System.out.println(ans);
            } else {
                k -= b;
                if (k <= c) {
                    ans += k * -1;
                    System.out.println(ans);
                } else {
                    ans += k * -1;
                    System.out.println(ans);
                }
            }
        }

        sc.close();
    }

} Main
import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int a = Integer.parseInt(sc.next());
        int b = Integer.parseInt(sc.next());
        int c = Integer.parseInt(sc.next());
        int k = Integer.parseInt(sc.next());
        int ans = 0;
        if (k <= a) {
            ans += k;
            System.out.println(ans);
        } else {
            k -= a;
            ans += a;
            if (k <= b) {
                System.out.println(ans);
            } else {
                k -= b;
                if (k <= c) {
                    ans += k * -1;
                    System.out.println(ans);
                } else {
                    ans += k * -1;
                    System.out.println(ans);
                }
            }
        }

        sc.close();
    }

}
