import java.util.Scanner;

public class Main {
    public static void main(final 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());

        if (k <= a + b) {
            if (k >= a) {
                System.out.println(a);
            } else {
                System.out.println(k);
            }
        } else {
            System.out.println(a - (k - a - b));
        }

        sc.close();
    }
} import java.util.Scanner; Scanner java.util public class Main {
    public static void main(final 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());

        if (k <= a + b) {
            if (k >= a) {
                System.out.println(a);
            } else {
                System.out.println(k);
            }
        } else {
            System.out.println(a - (k - a - b));
        }

        sc.close();
    }
} Main public static void main(final 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());

        if (k <= a + b) {
            if (k >= a) {
                System.out.println(a);
            } else {
                System.out.println(k);
            }
        } else {
            System.out.println(a - (k - a - b));
        }

        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());

        if (k <= a + b) {
            if (k >= a) {
                System.out.println(a);
            } else {
                System.out.println(k);
            }
        } else {
            System.out.println(a - (k - a - b));
        }

        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 if (k <= a + b) {
            if (k >= a) {
                System.out.println(a);
            } else {
                System.out.println(k);
            }
        } else {
            System.out.println(a - (k - a - b));
        } k <= a + b k a + b a b {
            if (k >= a) {
                System.out.println(a);
            } else {
                System.out.println(k);
            }
        } if (k >= a) {
                System.out.println(a);
            } else {
                System.out.println(k);
            } k >= a k a {
                System.out.println(a);
            } System.out.println(a) System.out.println System.out System System.out a {
                System.out.println(k);
            } System.out.println(k) System.out.println System.out System System.out k {
            System.out.println(a - (k - a - b));
        } System.out.println(a - (k - a - b)) System.out.println System.out System System.out a - (k - a - b) a (k - a - b) k - a - b k a b sc.close() sc.close sc final String[] args args public class Main {
    public static void main(final 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());

        if (k <= a + b) {
            if (k >= a) {
                System.out.println(a);
            } else {
                System.out.println(k);
            }
        } else {
            System.out.println(a - (k - a - b));
        }

        sc.close();
    }
} public class Main {
    public static void main(final 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());

        if (k <= a + b) {
            if (k >= a) {
                System.out.println(a);
            } else {
                System.out.println(k);
            }
        } else {
            System.out.println(a - (k - a - b));
        }

        sc.close();
    }
} Main
import java.util.Scanner;

public class Main {
    public static void main(final 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());

        if (k <= a + b) {
            if (k >= a) {
                System.out.println(a);
            } else {
                System.out.println(k);
            }
        } else {
            System.out.println(a - (k - a - b));
        }

        sc.close();
    }
}