
import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
	// write your code here
        Scanner scanner = new Scanner(System.in);

        int A = scanner.nextInt();
        int B = scanner.nextInt();
        int C = scanner.nextInt();
        int K = scanner.nextInt();

        if(A > K) {
            System.out.println(K);
            return;
        }


        if(A == K) {
            System.out.println(A);
            return;
        }

        if(A + B == K) {
            System.out.println(A);
            return;
        }
        int ans = K - (A+B);
        System.out.println( A - ans );
    }
} import java.util.Scanner; Scanner java.util public class Main {

    public static void main(String[] args) {
	// write your code here
        Scanner scanner = new Scanner(System.in);

        int A = scanner.nextInt();
        int B = scanner.nextInt();
        int C = scanner.nextInt();
        int K = scanner.nextInt();

        if(A > K) {
            System.out.println(K);
            return;
        }


        if(A == K) {
            System.out.println(A);
            return;
        }

        if(A + B == K) {
            System.out.println(A);
            return;
        }
        int ans = K - (A+B);
        System.out.println( A - ans );
    }
} Main public static void main(String[] args) {
	// write your code here
        Scanner scanner = new Scanner(System.in);

        int A = scanner.nextInt();
        int B = scanner.nextInt();
        int C = scanner.nextInt();
        int K = scanner.nextInt();

        if(A > K) {
            System.out.println(K);
            return;
        }


        if(A == K) {
            System.out.println(A);
            return;
        }

        if(A + B == K) {
            System.out.println(A);
            return;
        }
        int ans = K - (A+B);
        System.out.println( A - ans );
    } main {
	// write your code here
        Scanner scanner = new Scanner(System.in);

        int A = scanner.nextInt();
        int B = scanner.nextInt();
        int C = scanner.nextInt();
        int K = scanner.nextInt();

        if(A > K) {
            System.out.println(K);
            return;
        }


        if(A == K) {
            System.out.println(A);
            return;
        }

        if(A + B == K) {
            System.out.println(A);
            return;
        }
        int ans = K - (A+B);
        System.out.println( A - ans );
    } Scanner scanner = new Scanner(System.in); scanner new Scanner(System.in) int A = scanner.nextInt(); A scanner.nextInt() scanner.nextInt scanner int B = scanner.nextInt(); B scanner.nextInt() scanner.nextInt scanner int C = scanner.nextInt(); C scanner.nextInt() scanner.nextInt scanner int K = scanner.nextInt(); K scanner.nextInt() scanner.nextInt scanner if(A > K) {
            System.out.println(K);
            return;
        } A > K A K {
            System.out.println(K);
            return;
        } System.out.println(K) System.out.println System.out System System.out K return; if(A == K) {
            System.out.println(A);
            return;
        } A == K A K {
            System.out.println(A);
            return;
        } System.out.println(A) System.out.println System.out System System.out A return; if(A + B == K) {
            System.out.println(A);
            return;
        } A + B == K A + B A B K {
            System.out.println(A);
            return;
        } System.out.println(A) System.out.println System.out System System.out A return; int ans = K - (A+B); ans K - (A+B) K (A+B) A B System.out.println( A - ans ) System.out.println System.out System System.out A - ans A ans String[] args args public class Main {

    public static void main(String[] args) {
	// write your code here
        Scanner scanner = new Scanner(System.in);

        int A = scanner.nextInt();
        int B = scanner.nextInt();
        int C = scanner.nextInt();
        int K = scanner.nextInt();

        if(A > K) {
            System.out.println(K);
            return;
        }


        if(A == K) {
            System.out.println(A);
            return;
        }

        if(A + B == K) {
            System.out.println(A);
            return;
        }
        int ans = K - (A+B);
        System.out.println( A - ans );
    }
} public class Main {

    public static void main(String[] args) {
	// write your code here
        Scanner scanner = new Scanner(System.in);

        int A = scanner.nextInt();
        int B = scanner.nextInt();
        int C = scanner.nextInt();
        int K = scanner.nextInt();

        if(A > K) {
            System.out.println(K);
            return;
        }


        if(A == K) {
            System.out.println(A);
            return;
        }

        if(A + B == K) {
            System.out.println(A);
            return;
        }
        int ans = K - (A+B);
        System.out.println( A - ans );
    }
} Main

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
	// write your code here
        Scanner scanner = new Scanner(System.in);

        int A = scanner.nextInt();
        int B = scanner.nextInt();
        int C = scanner.nextInt();
        int K = scanner.nextInt();

        if(A > K) {
            System.out.println(K);
            return;
        }


        if(A == K) {
            System.out.println(A);
            return;
        }

        if(A + B == K) {
            System.out.println(A);
            return;
        }
        int ans = K - (A+B);
        System.out.println( A - ans );
    }
}
