import java.util.Scanner;

/*
問題文

*/

public class Main {
    public static void main(String[] args){

        Scanner sc = new Scanner(System.in);
        long a = sc.nextInt();
        long b = sc.nextInt();
        long c = sc.nextInt();
        long k = sc.nextInt();

        long ans = 0;
        boolean flg = false;
        if (k <= a) {
            ans = k;
            flg = true;
        }

        k = k-a;

        if ( !flg && k <= b) {
            ans = a;
            flg = true;
        }

        k = k-b;

        if (!flg && k-c <= 0) {
            ans = a + (-1 * k);
        }
            System.out.println(ans);
    }
} import java.util.Scanner; Scanner java.util public class Main {
    public static void main(String[] args){

        Scanner sc = new Scanner(System.in);
        long a = sc.nextInt();
        long b = sc.nextInt();
        long c = sc.nextInt();
        long k = sc.nextInt();

        long ans = 0;
        boolean flg = false;
        if (k <= a) {
            ans = k;
            flg = true;
        }

        k = k-a;

        if ( !flg && k <= b) {
            ans = a;
            flg = true;
        }

        k = k-b;

        if (!flg && k-c <= 0) {
            ans = a + (-1 * k);
        }
            System.out.println(ans);
    }
} Main public static void main(String[] args){

        Scanner sc = new Scanner(System.in);
        long a = sc.nextInt();
        long b = sc.nextInt();
        long c = sc.nextInt();
        long k = sc.nextInt();

        long ans = 0;
        boolean flg = false;
        if (k <= a) {
            ans = k;
            flg = true;
        }

        k = k-a;

        if ( !flg && k <= b) {
            ans = a;
            flg = true;
        }

        k = k-b;

        if (!flg && k-c <= 0) {
            ans = a + (-1 * k);
        }
            System.out.println(ans);
    } main {

        Scanner sc = new Scanner(System.in);
        long a = sc.nextInt();
        long b = sc.nextInt();
        long c = sc.nextInt();
        long k = sc.nextInt();

        long ans = 0;
        boolean flg = false;
        if (k <= a) {
            ans = k;
            flg = true;
        }

        k = k-a;

        if ( !flg && k <= b) {
            ans = a;
            flg = true;
        }

        k = k-b;

        if (!flg && k-c <= 0) {
            ans = a + (-1 * k);
        }
            System.out.println(ans);
    } Scanner sc = new Scanner(System.in); sc new Scanner(System.in) long a = sc.nextInt(); a sc.nextInt() sc.nextInt sc long b = sc.nextInt(); b sc.nextInt() sc.nextInt sc long c = sc.nextInt(); c sc.nextInt() sc.nextInt sc long k = sc.nextInt(); k sc.nextInt() sc.nextInt sc long ans = 0; ans 0 boolean flg = false; flg false if (k <= a) {
            ans = k;
            flg = true;
        } k <= a k a {
            ans = k;
            flg = true;
        } ans = k ans k flg = true flg true k = k-a k k-a k a if ( !flg && k <= b) {
            ans = a;
            flg = true;
        } !flg && k <= b !flg flg k <= b k b {
            ans = a;
            flg = true;
        } ans = a ans a flg = true flg true k = k-b k k-b k b if (!flg && k-c <= 0) {
            ans = a + (-1 * k);
        } !flg && k-c <= 0 !flg flg k-c <= 0 k-c k c 0 {
            ans = a + (-1 * k);
        } ans = a + (-1 * k) ans a + (-1 * k) a (-1 * k) -1 1 k System.out.println(ans) System.out.println System.out System System.out ans String[] args args public class Main {
    public static void main(String[] args){

        Scanner sc = new Scanner(System.in);
        long a = sc.nextInt();
        long b = sc.nextInt();
        long c = sc.nextInt();
        long k = sc.nextInt();

        long ans = 0;
        boolean flg = false;
        if (k <= a) {
            ans = k;
            flg = true;
        }

        k = k-a;

        if ( !flg && k <= b) {
            ans = a;
            flg = true;
        }

        k = k-b;

        if (!flg && k-c <= 0) {
            ans = a + (-1 * k);
        }
            System.out.println(ans);
    }
} public class Main {
    public static void main(String[] args){

        Scanner sc = new Scanner(System.in);
        long a = sc.nextInt();
        long b = sc.nextInt();
        long c = sc.nextInt();
        long k = sc.nextInt();

        long ans = 0;
        boolean flg = false;
        if (k <= a) {
            ans = k;
            flg = true;
        }

        k = k-a;

        if ( !flg && k <= b) {
            ans = a;
            flg = true;
        }

        k = k-b;

        if (!flg && k-c <= 0) {
            ans = a + (-1 * k);
        }
            System.out.println(ans);
    }
} Main
import java.util.Scanner;

/*
問題文

*/

public class Main {
    public static void main(String[] args){

        Scanner sc = new Scanner(System.in);
        long a = sc.nextInt();
        long b = sc.nextInt();
        long c = sc.nextInt();
        long k = sc.nextInt();

        long ans = 0;
        boolean flg = false;
        if (k <= a) {
            ans = k;
            flg = true;
        }

        k = k-a;

        if ( !flg && k <= b) {
            ans = a;
            flg = true;
        }

        k = k-b;

        if (!flg && k-c <= 0) {
            ans = a + (-1 * k);
        }
            System.out.println(ans);
    }
}