import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int[] count = new int[3];

        count[0] = sc.nextInt();
        count[1] = sc.nextInt();
        count[2] = sc.nextInt();
        int k = sc.nextInt();
        int sum = 0;
        
        if (count[0] >= k) {
            sum = k;
        } else if (count[0] + count[1] >= k) {
            sum = count[0];
        } else {
            sum = count[0] - (k - (count[0] + count[1]));
        }
        
        System.out.println(sum);
    }
} import java.util.Scanner; Scanner java.util public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int[] count = new int[3];

        count[0] = sc.nextInt();
        count[1] = sc.nextInt();
        count[2] = sc.nextInt();
        int k = sc.nextInt();
        int sum = 0;
        
        if (count[0] >= k) {
            sum = k;
        } else if (count[0] + count[1] >= k) {
            sum = count[0];
        } else {
            sum = count[0] - (k - (count[0] + count[1]));
        }
        
        System.out.println(sum);
    }
} Main public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int[] count = new int[3];

        count[0] = sc.nextInt();
        count[1] = sc.nextInt();
        count[2] = sc.nextInt();
        int k = sc.nextInt();
        int sum = 0;
        
        if (count[0] >= k) {
            sum = k;
        } else if (count[0] + count[1] >= k) {
            sum = count[0];
        } else {
            sum = count[0] - (k - (count[0] + count[1]));
        }
        
        System.out.println(sum);
    } main {
        Scanner sc = new Scanner(System.in);
        int[] count = new int[3];

        count[0] = sc.nextInt();
        count[1] = sc.nextInt();
        count[2] = sc.nextInt();
        int k = sc.nextInt();
        int sum = 0;
        
        if (count[0] >= k) {
            sum = k;
        } else if (count[0] + count[1] >= k) {
            sum = count[0];
        } else {
            sum = count[0] - (k - (count[0] + count[1]));
        }
        
        System.out.println(sum);
    } Scanner sc = new Scanner(System.in); sc new Scanner(System.in) int[] count = new int[3]; count new int[3] 3 count[0] = sc.nextInt() count[0] count 0 sc.nextInt() sc.nextInt sc count[1] = sc.nextInt() count[1] count 1 sc.nextInt() sc.nextInt sc count[2] = sc.nextInt() count[2] count 2 sc.nextInt() sc.nextInt sc int k = sc.nextInt(); k sc.nextInt() sc.nextInt sc int sum = 0; sum 0 if (count[0] >= k) {
            sum = k;
        } else if (count[0] + count[1] >= k) {
            sum = count[0];
        } else {
            sum = count[0] - (k - (count[0] + count[1]));
        } count[0] >= k count[0] count 0 k {
            sum = k;
        } sum = k sum k if (count[0] + count[1] >= k) {
            sum = count[0];
        } else {
            sum = count[0] - (k - (count[0] + count[1]));
        } count[0] + count[1] >= k count[0] + count[1] count[0] count 0 count[1] count 1 k {
            sum = count[0];
        } sum = count[0] sum count[0] count 0 {
            sum = count[0] - (k - (count[0] + count[1]));
        } sum = count[0] - (k - (count[0] + count[1])) sum count[0] - (k - (count[0] + count[1])) count[0] count 0 (k - (count[0] + count[1])) k (count[0] + count[1]) count[0] count 0 count[1] count 1 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);
        int[] count = new int[3];

        count[0] = sc.nextInt();
        count[1] = sc.nextInt();
        count[2] = sc.nextInt();
        int k = sc.nextInt();
        int sum = 0;
        
        if (count[0] >= k) {
            sum = k;
        } else if (count[0] + count[1] >= k) {
            sum = count[0];
        } else {
            sum = count[0] - (k - (count[0] + count[1]));
        }
        
        System.out.println(sum);
    }
} public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int[] count = new int[3];

        count[0] = sc.nextInt();
        count[1] = sc.nextInt();
        count[2] = sc.nextInt();
        int k = sc.nextInt();
        int sum = 0;
        
        if (count[0] >= k) {
            sum = k;
        } else if (count[0] + count[1] >= k) {
            sum = count[0];
        } else {
            sum = count[0] - (k - (count[0] + count[1]));
        }
        
        System.out.println(sum);
    }
} Main
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int[] count = new int[3];

        count[0] = sc.nextInt();
        count[1] = sc.nextInt();
        count[2] = sc.nextInt();
        int k = sc.nextInt();
        int sum = 0;
        
        if (count[0] >= k) {
            sum = k;
        } else if (count[0] + count[1] >= k) {
            sum = count[0];
        } else {
            sum = count[0] - (k - (count[0] + count[1]));
        }
        
        System.out.println(sum);
    }
}