import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int a = sc.nextInt();
        int b = sc.nextInt();
        int c = sc.nextInt();
        int k = sc.nextInt();
        int max = 0;
        
        for(int i=0; i<k; i++){
            if(a > 0){
                max++;
                a--;
            }
            else if(b > 0){
                b--;
            }
            else{
                max--;
                c--;
            }
        }
        System.out.println(max);
    }
} import java.util.Scanner; Scanner java.util public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int a = sc.nextInt();
        int b = sc.nextInt();
        int c = sc.nextInt();
        int k = sc.nextInt();
        int max = 0;
        
        for(int i=0; i<k; i++){
            if(a > 0){
                max++;
                a--;
            }
            else if(b > 0){
                b--;
            }
            else{
                max--;
                c--;
            }
        }
        System.out.println(max);
    }
} Main public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int a = sc.nextInt();
        int b = sc.nextInt();
        int c = sc.nextInt();
        int k = sc.nextInt();
        int max = 0;
        
        for(int i=0; i<k; i++){
            if(a > 0){
                max++;
                a--;
            }
            else if(b > 0){
                b--;
            }
            else{
                max--;
                c--;
            }
        }
        System.out.println(max);
    } main {
        Scanner sc = new Scanner(System.in);
        int a = sc.nextInt();
        int b = sc.nextInt();
        int c = sc.nextInt();
        int k = sc.nextInt();
        int max = 0;
        
        for(int i=0; i<k; i++){
            if(a > 0){
                max++;
                a--;
            }
            else if(b > 0){
                b--;
            }
            else{
                max--;
                c--;
            }
        }
        System.out.println(max);
    } Scanner sc = new Scanner(System.in); sc new Scanner(System.in) int a = sc.nextInt(); a sc.nextInt() sc.nextInt sc int b = sc.nextInt(); b sc.nextInt() sc.nextInt sc int c = sc.nextInt(); c sc.nextInt() sc.nextInt sc int k = sc.nextInt(); k sc.nextInt() sc.nextInt sc int max = 0; max 0 for(int i=0; i<k; i++){
            if(a > 0){
                max++;
                a--;
            }
            else if(b > 0){
                b--;
            }
            else{
                max--;
                c--;
            }
        } int i=0; int i=0; i 0 i<k i k i++ i++ i {
            if(a > 0){
                max++;
                a--;
            }
            else if(b > 0){
                b--;
            }
            else{
                max--;
                c--;
            }
        } {
            if(a > 0){
                max++;
                a--;
            }
            else if(b > 0){
                b--;
            }
            else{
                max--;
                c--;
            }
        } if(a > 0){
                max++;
                a--;
            }
            else if(b > 0){
                b--;
            }
            else{
                max--;
                c--;
            } a > 0 a 0 {
                max++;
                a--;
            } max++ max a-- a if(b > 0){
                b--;
            }
            else{
                max--;
                c--;
            } b > 0 b 0 {
                b--;
            } b-- b {
                max--;
                c--;
            } max-- max c-- c System.out.println(max) System.out.println System.out System System.out max String[] args args public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int a = sc.nextInt();
        int b = sc.nextInt();
        int c = sc.nextInt();
        int k = sc.nextInt();
        int max = 0;
        
        for(int i=0; i<k; i++){
            if(a > 0){
                max++;
                a--;
            }
            else if(b > 0){
                b--;
            }
            else{
                max--;
                c--;
            }
        }
        System.out.println(max);
    }
} public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int a = sc.nextInt();
        int b = sc.nextInt();
        int c = sc.nextInt();
        int k = sc.nextInt();
        int max = 0;
        
        for(int i=0; i<k; i++){
            if(a > 0){
                max++;
                a--;
            }
            else if(b > 0){
                b--;
            }
            else{
                max--;
                c--;
            }
        }
        System.out.println(max);
    }
} Main
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int a = sc.nextInt();
        int b = sc.nextInt();
        int c = sc.nextInt();
        int k = sc.nextInt();
        int max = 0;
        
        for(int i=0; i<k; i++){
            if(a > 0){
                max++;
                a--;
            }
            else if(b > 0){
                b--;
            }
            else{
                max--;
                c--;
            }
        }
        System.out.println(max);
    }
}