
import java.util.Scanner;

public class Main {
    static Scanner sc = new Scanner(System.in);
    static long a = sc.nextLong();
    static long b = sc.nextLong();
    static long c = sc.nextLong();
    static long k = sc.nextLong();

    public static void main(String[] args) {
        long result = 0;
        long tmp = k - a;
        if (tmp > 0){
            result = a;
            tmp = tmp - b;
            if (tmp > 0){
                result = result - tmp;
            } else {
                result = a;
            }
        } else {
            result = k;
        }
        System.out.println(result);

    }
} import java.util.Scanner; Scanner java.util public class Main {
    static Scanner sc = new Scanner(System.in);
    static long a = sc.nextLong();
    static long b = sc.nextLong();
    static long c = sc.nextLong();
    static long k = sc.nextLong();

    public static void main(String[] args) {
        long result = 0;
        long tmp = k - a;
        if (tmp > 0){
            result = a;
            tmp = tmp - b;
            if (tmp > 0){
                result = result - tmp;
            } else {
                result = a;
            }
        } else {
            result = k;
        }
        System.out.println(result);

    }
} Main static Scanner sc = new Scanner(System.in); sc new Scanner(System.in) static long a = sc.nextLong(); a sc.nextLong() sc.nextLong sc static long b = sc.nextLong(); b sc.nextLong() sc.nextLong sc static long c = sc.nextLong(); c sc.nextLong() sc.nextLong sc static long k = sc.nextLong(); k sc.nextLong() sc.nextLong sc public static void main(String[] args) {
        long result = 0;
        long tmp = k - a;
        if (tmp > 0){
            result = a;
            tmp = tmp - b;
            if (tmp > 0){
                result = result - tmp;
            } else {
                result = a;
            }
        } else {
            result = k;
        }
        System.out.println(result);

    } main {
        long result = 0;
        long tmp = k - a;
        if (tmp > 0){
            result = a;
            tmp = tmp - b;
            if (tmp > 0){
                result = result - tmp;
            } else {
                result = a;
            }
        } else {
            result = k;
        }
        System.out.println(result);

    } long result = 0; result 0 long tmp = k - a; tmp k - a k a if (tmp > 0){
            result = a;
            tmp = tmp - b;
            if (tmp > 0){
                result = result - tmp;
            } else {
                result = a;
            }
        } else {
            result = k;
        } tmp > 0 tmp 0 {
            result = a;
            tmp = tmp - b;
            if (tmp > 0){
                result = result - tmp;
            } else {
                result = a;
            }
        } result = a result a tmp = tmp - b tmp tmp - b tmp b if (tmp > 0){
                result = result - tmp;
            } else {
                result = a;
            } tmp > 0 tmp 0 {
                result = result - tmp;
            } result = result - tmp result result - tmp result tmp {
                result = a;
            } result = a result a {
            result = k;
        } result = k result k System.out.println(result) System.out.println System.out System System.out result String[] args args public class Main {
    static Scanner sc = new Scanner(System.in);
    static long a = sc.nextLong();
    static long b = sc.nextLong();
    static long c = sc.nextLong();
    static long k = sc.nextLong();

    public static void main(String[] args) {
        long result = 0;
        long tmp = k - a;
        if (tmp > 0){
            result = a;
            tmp = tmp - b;
            if (tmp > 0){
                result = result - tmp;
            } else {
                result = a;
            }
        } else {
            result = k;
        }
        System.out.println(result);

    }
} public class Main {
    static Scanner sc = new Scanner(System.in);
    static long a = sc.nextLong();
    static long b = sc.nextLong();
    static long c = sc.nextLong();
    static long k = sc.nextLong();

    public static void main(String[] args) {
        long result = 0;
        long tmp = k - a;
        if (tmp > 0){
            result = a;
            tmp = tmp - b;
            if (tmp > 0){
                result = result - tmp;
            } else {
                result = a;
            }
        } else {
            result = k;
        }
        System.out.println(result);

    }
} Main

import java.util.Scanner;

public class Main {
    static Scanner sc = new Scanner(System.in);
    static long a = sc.nextLong();
    static long b = sc.nextLong();
    static long c = sc.nextLong();
    static long k = sc.nextLong();

    public static void main(String[] args) {
        long result = 0;
        long tmp = k - a;
        if (tmp > 0){
            result = a;
            tmp = tmp - b;
            if (tmp > 0){
                result = result - tmp;
            } else {
                result = a;
            }
        } else {
            result = k;
        }
        System.out.println(result);

    }
}
