import java.util.*;

public class Main {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        long A = sc.nextLong();
        long B = sc.nextLong();
        long C = sc.nextLong();
        long K = sc.nextLong();
        sc.close();

        long aCount = 0;
        long bCount = 0;
        long cCount = 0;

        if (K <= A) {
            aCount = K;
        } else {
            aCount = A;
            K -= A;
            if (K <= B) {
                bCount = K;
            } else {
                bCount = B;
                K -= B;
                cCount = K;
            }
        }

        System.out.println(1 * aCount + (-1) * cCount);
    }

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

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        long A = sc.nextLong();
        long B = sc.nextLong();
        long C = sc.nextLong();
        long K = sc.nextLong();
        sc.close();

        long aCount = 0;
        long bCount = 0;
        long cCount = 0;

        if (K <= A) {
            aCount = K;
        } else {
            aCount = A;
            K -= A;
            if (K <= B) {
                bCount = K;
            } else {
                bCount = B;
                K -= B;
                cCount = K;
            }
        }

        System.out.println(1 * aCount + (-1) * cCount);
    }

} Main public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        long A = sc.nextLong();
        long B = sc.nextLong();
        long C = sc.nextLong();
        long K = sc.nextLong();
        sc.close();

        long aCount = 0;
        long bCount = 0;
        long cCount = 0;

        if (K <= A) {
            aCount = K;
        } else {
            aCount = A;
            K -= A;
            if (K <= B) {
                bCount = K;
            } else {
                bCount = B;
                K -= B;
                cCount = K;
            }
        }

        System.out.println(1 * aCount + (-1) * cCount);
    } main {
        Scanner sc = new Scanner(System.in);
        long A = sc.nextLong();
        long B = sc.nextLong();
        long C = sc.nextLong();
        long K = sc.nextLong();
        sc.close();

        long aCount = 0;
        long bCount = 0;
        long cCount = 0;

        if (K <= A) {
            aCount = K;
        } else {
            aCount = A;
            K -= A;
            if (K <= B) {
                bCount = K;
            } else {
                bCount = B;
                K -= B;
                cCount = K;
            }
        }

        System.out.println(1 * aCount + (-1) * cCount);
    } Scanner sc = new Scanner(System.in); sc new Scanner(System.in) long A = sc.nextLong(); A sc.nextLong() sc.nextLong sc long B = sc.nextLong(); B sc.nextLong() sc.nextLong sc long C = sc.nextLong(); C sc.nextLong() sc.nextLong sc long K = sc.nextLong(); K sc.nextLong() sc.nextLong sc sc.close() sc.close sc long aCount = 0; aCount 0 long bCount = 0; bCount 0 long cCount = 0; cCount 0 if (K <= A) {
            aCount = K;
        } else {
            aCount = A;
            K -= A;
            if (K <= B) {
                bCount = K;
            } else {
                bCount = B;
                K -= B;
                cCount = K;
            }
        } K <= A K A {
            aCount = K;
        } aCount = K aCount K {
            aCount = A;
            K -= A;
            if (K <= B) {
                bCount = K;
            } else {
                bCount = B;
                K -= B;
                cCount = K;
            }
        } aCount = A aCount A K -= A K A if (K <= B) {
                bCount = K;
            } else {
                bCount = B;
                K -= B;
                cCount = K;
            } K <= B K B {
                bCount = K;
            } bCount = K bCount K {
                bCount = B;
                K -= B;
                cCount = K;
            } bCount = B bCount B K -= B K B cCount = K cCount K System.out.println(1 * aCount + (-1) * cCount) System.out.println System.out System System.out 1 * aCount + (-1) * cCount 1 * aCount 1 aCount (-1) * cCount (-1) 1 cCount String[] args args public class Main {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        long A = sc.nextLong();
        long B = sc.nextLong();
        long C = sc.nextLong();
        long K = sc.nextLong();
        sc.close();

        long aCount = 0;
        long bCount = 0;
        long cCount = 0;

        if (K <= A) {
            aCount = K;
        } else {
            aCount = A;
            K -= A;
            if (K <= B) {
                bCount = K;
            } else {
                bCount = B;
                K -= B;
                cCount = K;
            }
        }

        System.out.println(1 * aCount + (-1) * cCount);
    }

} public class Main {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        long A = sc.nextLong();
        long B = sc.nextLong();
        long C = sc.nextLong();
        long K = sc.nextLong();
        sc.close();

        long aCount = 0;
        long bCount = 0;
        long cCount = 0;

        if (K <= A) {
            aCount = K;
        } else {
            aCount = A;
            K -= A;
            if (K <= B) {
                bCount = K;
            } else {
                bCount = B;
                K -= B;
                cCount = K;
            }
        }

        System.out.println(1 * aCount + (-1) * cCount);
    }

} Main
import java.util.*;

public class Main {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        long A = sc.nextLong();
        long B = sc.nextLong();
        long C = sc.nextLong();
        long K = sc.nextLong();
        sc.close();

        long aCount = 0;
        long bCount = 0;
        long cCount = 0;

        if (K <= A) {
            aCount = K;
        } else {
            aCount = A;
            K -= A;
            if (K <= B) {
                bCount = K;
            } else {
                bCount = B;
                K -= B;
                cCount = K;
            }
        }

        System.out.println(1 * aCount + (-1) * cCount);
    }

}
