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

        long sum = 0;
        if (k <= a) {
            sum += 1 * k;
        } else if (k > a && k <= a + b) {
            sum += 1 * a;
        } else if (k > a + b) {
            sum += 1 * a - 1 * (k - a - b);
        }

        System.out.println(sum);
    }
} 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();

        long sum = 0;
        if (k <= a) {
            sum += 1 * k;
        } else if (k > a && k <= a + b) {
            sum += 1 * a;
        } else if (k > a + b) {
            sum += 1 * a - 1 * (k - a - b);
        }

        System.out.println(sum);
    }
} 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();

        long sum = 0;
        if (k <= a) {
            sum += 1 * k;
        } else if (k > a && k <= a + b) {
            sum += 1 * a;
        } else if (k > a + b) {
            sum += 1 * a - 1 * (k - a - b);
        }

        System.out.println(sum);
    } main {
        Scanner sc = new Scanner(System.in);
        long a = sc.nextLong();
        long b = sc.nextLong();
        long c = sc.nextLong();
        long k = sc.nextLong();

        long sum = 0;
        if (k <= a) {
            sum += 1 * k;
        } else if (k > a && k <= a + b) {
            sum += 1 * a;
        } else if (k > a + b) {
            sum += 1 * a - 1 * (k - a - b);
        }

        System.out.println(sum);
    } 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 long sum = 0; sum 0 if (k <= a) {
            sum += 1 * k;
        } else if (k > a && k <= a + b) {
            sum += 1 * a;
        } else if (k > a + b) {
            sum += 1 * a - 1 * (k - a - b);
        } k <= a k a {
            sum += 1 * k;
        } sum += 1 * k sum 1 * k 1 k if (k > a && k <= a + b) {
            sum += 1 * a;
        } else if (k > a + b) {
            sum += 1 * a - 1 * (k - a - b);
        } k > a && k <= a + b k > a k a k <= a + b k a + b a b {
            sum += 1 * a;
        } sum += 1 * a sum 1 * a 1 a if (k > a + b) {
            sum += 1 * a - 1 * (k - a - b);
        } k > a + b k a + b a b {
            sum += 1 * a - 1 * (k - a - b);
        } sum += 1 * a - 1 * (k - a - b) sum 1 * a - 1 * (k - a - b) 1 * a 1 a 1 * (k - a - b) 1 (k - a - b) k - a - b k a b System.out.println(sum) System.out.println System.out System System.out sum 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();

        long sum = 0;
        if (k <= a) {
            sum += 1 * k;
        } else if (k > a && k <= a + b) {
            sum += 1 * a;
        } else if (k > a + b) {
            sum += 1 * a - 1 * (k - a - b);
        }

        System.out.println(sum);
    }
} 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();

        long sum = 0;
        if (k <= a) {
            sum += 1 * k;
        } else if (k > a && k <= a + b) {
            sum += 1 * a;
        } else if (k > a + b) {
            sum += 1 * a - 1 * (k - a - b);
        }

        System.out.println(sum);
    }
} 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();

        long sum = 0;
        if (k <= a) {
            sum += 1 * k;
        } else if (k > a && k <= a + b) {
            sum += 1 * a;
        } else if (k > a + b) {
            sum += 1 * a - 1 * (k - a - b);
        }

        System.out.println(sum);
    }
}