import java.util.Scanner;

/**
 * B - Easy Linear Programming
 * 1 が書かれたカードが A 枚、0 が書かれたカードが B 枚、−1 が書かれたカードが C 枚あります。
 * これらのカードから、ちょうど K 枚を選んで取るとき、取ったカードに書かれた数の和として、 ありうる値の最大値はいくつですか。
 */
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 result = 0;


        if( k <= a ) {
            result = k;
        }
        else if( k <= a + b ) {
            result = a;
        }
        else if( k <= a + b + c ) {
            result = a - ( k - a - b );
        }

        System.out.println( result );

        sc.close();
    }
} 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.nextLong();
        long b = sc.nextLong();
        long c = sc.nextLong();
        long k = sc.nextLong();

        long result = 0;


        if( k <= a ) {
            result = k;
        }
        else if( k <= a + b ) {
            result = a;
        }
        else if( k <= a + b + c ) {
            result = a - ( k - a - b );
        }

        System.out.println( result );

        sc.close();
    }
} 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 result = 0;


        if( k <= a ) {
            result = k;
        }
        else if( k <= a + b ) {
            result = a;
        }
        else if( k <= a + b + c ) {
            result = a - ( k - a - b );
        }

        System.out.println( result );

        sc.close();
    } main {

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

        long result = 0;


        if( k <= a ) {
            result = k;
        }
        else if( k <= a + b ) {
            result = a;
        }
        else if( k <= a + b + c ) {
            result = a - ( k - a - b );
        }

        System.out.println( result );

        sc.close();
    } 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 result = 0; result 0 if( k <= a ) {
            result = k;
        }
        else if( k <= a + b ) {
            result = a;
        }
        else if( k <= a + b + c ) {
            result = a - ( k - a - b );
        } k <= a k a {
            result = k;
        } result = k result k if( k <= a + b ) {
            result = a;
        }
        else if( k <= a + b + c ) {
            result = a - ( k - a - b );
        } k <= a + b k a + b a b {
            result = a;
        } result = a result a if( k <= a + b + c ) {
            result = a - ( k - a - b );
        } k <= a + b + c k a + b + c a + b + c a b c {
            result = a - ( k - a - b );
        } result = a - ( k - a - b ) result a - ( k - a - b ) a ( k - a - b ) k - a - b k a b System.out.println( result ) System.out.println System.out System System.out result sc.close() sc.close sc 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 result = 0;


        if( k <= a ) {
            result = k;
        }
        else if( k <= a + b ) {
            result = a;
        }
        else if( k <= a + b + c ) {
            result = a - ( k - a - b );
        }

        System.out.println( result );

        sc.close();
    }
} 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 result = 0;


        if( k <= a ) {
            result = k;
        }
        else if( k <= a + b ) {
            result = a;
        }
        else if( k <= a + b + c ) {
            result = a - ( k - a - b );
        }

        System.out.println( result );

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

/**
 * B - Easy Linear Programming
 * 1 が書かれたカードが A 枚、0 が書かれたカードが B 枚、−1 が書かれたカードが C 枚あります。
 * これらのカードから、ちょうど K 枚を選んで取るとき、取ったカードに書かれた数の和として、 ありうる値の最大値はいくつですか。
 */
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 result = 0;


        if( k <= a ) {
            result = k;
        }
        else if( k <= a + b ) {
            result = a;
        }
        else if( k <= a + b + c ) {
            result = a - ( k - a - b );
        }

        System.out.println( result );

        sc.close();
    }
}
