import java.util.Scanner;

public class Main {

    private static int sum(int aNum, int bNum, int cNum, int k) {
        int sum = 0;
        if (k <= aNum) {
            return k;
        } else {
            sum += aNum;
            k -= aNum;
        }

        if (k <= bNum) {
            return sum;
        } else {
            k -= bNum;
        }

        return sum + Math.min(k, cNum) * -1;
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int aNum = sc.nextInt(), bNum = sc.nextInt(), cNum = sc.nextInt(), k = sc.nextInt();

        System.out.println(sum(aNum, bNum, cNum, k));
        sc.close();
    }
} import java.util.Scanner; Scanner java.util public class Main {

    private static int sum(int aNum, int bNum, int cNum, int k) {
        int sum = 0;
        if (k <= aNum) {
            return k;
        } else {
            sum += aNum;
            k -= aNum;
        }

        if (k <= bNum) {
            return sum;
        } else {
            k -= bNum;
        }

        return sum + Math.min(k, cNum) * -1;
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int aNum = sc.nextInt(), bNum = sc.nextInt(), cNum = sc.nextInt(), k = sc.nextInt();

        System.out.println(sum(aNum, bNum, cNum, k));
        sc.close();
    }
} Main private static int sum(int aNum, int bNum, int cNum, int k) {
        int sum = 0;
        if (k <= aNum) {
            return k;
        } else {
            sum += aNum;
            k -= aNum;
        }

        if (k <= bNum) {
            return sum;
        } else {
            k -= bNum;
        }

        return sum + Math.min(k, cNum) * -1;
    } sum {
        int sum = 0;
        if (k <= aNum) {
            return k;
        } else {
            sum += aNum;
            k -= aNum;
        }

        if (k <= bNum) {
            return sum;
        } else {
            k -= bNum;
        }

        return sum + Math.min(k, cNum) * -1;
    } int sum = 0; sum 0 if (k <= aNum) {
            return k;
        } else {
            sum += aNum;
            k -= aNum;
        } k <= aNum k aNum {
            return k;
        } return k; k {
            sum += aNum;
            k -= aNum;
        } sum += aNum sum aNum k -= aNum k aNum if (k <= bNum) {
            return sum;
        } else {
            k -= bNum;
        } k <= bNum k bNum {
            return sum;
        } return sum; sum {
            k -= bNum;
        } k -= bNum k bNum return sum + Math.min(k, cNum) * -1; sum + Math.min(k, cNum) * -1 sum Math.min(k, cNum) * -1 Math.min(k, cNum) Math.min Math k cNum -1 1 int aNum aNum int bNum bNum int cNum cNum int k k public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int aNum = sc.nextInt(), bNum = sc.nextInt(), cNum = sc.nextInt(), k = sc.nextInt();

        System.out.println(sum(aNum, bNum, cNum, k));
        sc.close();
    } main {
        Scanner sc = new Scanner(System.in);
        int aNum = sc.nextInt(), bNum = sc.nextInt(), cNum = sc.nextInt(), k = sc.nextInt();

        System.out.println(sum(aNum, bNum, cNum, k));
        sc.close();
    } Scanner sc = new Scanner(System.in); sc new Scanner(System.in) int aNum = sc.nextInt() aNum sc.nextInt() sc.nextInt sc bNum = sc.nextInt() bNum sc.nextInt() sc.nextInt sc cNum = sc.nextInt() cNum sc.nextInt() sc.nextInt sc k = sc.nextInt(); k sc.nextInt() sc.nextInt sc System.out.println(sum(aNum, bNum, cNum, k)) System.out.println System.out System System.out sum(aNum, bNum, cNum, k) sum aNum bNum cNum k sc.close() sc.close sc String[] args args public class Main {

    private static int sum(int aNum, int bNum, int cNum, int k) {
        int sum = 0;
        if (k <= aNum) {
            return k;
        } else {
            sum += aNum;
            k -= aNum;
        }

        if (k <= bNum) {
            return sum;
        } else {
            k -= bNum;
        }

        return sum + Math.min(k, cNum) * -1;
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int aNum = sc.nextInt(), bNum = sc.nextInt(), cNum = sc.nextInt(), k = sc.nextInt();

        System.out.println(sum(aNum, bNum, cNum, k));
        sc.close();
    }
} public class Main {

    private static int sum(int aNum, int bNum, int cNum, int k) {
        int sum = 0;
        if (k <= aNum) {
            return k;
        } else {
            sum += aNum;
            k -= aNum;
        }

        if (k <= bNum) {
            return sum;
        } else {
            k -= bNum;
        }

        return sum + Math.min(k, cNum) * -1;
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int aNum = sc.nextInt(), bNum = sc.nextInt(), cNum = sc.nextInt(), k = sc.nextInt();

        System.out.println(sum(aNum, bNum, cNum, k));
        sc.close();
    }
} Main
import java.util.Scanner;

public class Main {

    private static int sum(int aNum, int bNum, int cNum, int k) {
        int sum = 0;
        if (k <= aNum) {
            return k;
        } else {
            sum += aNum;
            k -= aNum;
        }

        if (k <= bNum) {
            return sum;
        } else {
            k -= bNum;
        }

        return sum + Math.min(k, cNum) * -1;
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int aNum = sc.nextInt(), bNum = sc.nextInt(), cNum = sc.nextInt(), k = sc.nextInt();

        System.out.println(sum(aNum, bNum, cNum, k));
        sc.close();
    }
}
