import java.util.*;


public class Main{
    public static void main(String[] args) throws Exception{
        
        Scanner sc = new Scanner(System.in);
        
        int a = sc.nextInt();
        int b = sc.nextInt();
        int c = sc.nextInt();
        int k = sc.nextInt();//3
        int sum_number = 0;

        int[] numbers = {1,0,-1};
        int[] cards = {a,b,c}; //2,1,1

        for(int i = 0; i<=2; i++){
            if(cards[i]<=k){
                sum_number = sum_number + (numbers[i] * cards[i]);
                k = k - cards[i];

            }else{
                sum_number = sum_number + (numbers[i] * k);
                break;
            }
        }
        System.out.println(sum_number);
        return;
    }    
} import java.util.*; util.* java public class Main{
    public static void main(String[] args) throws Exception{
        
        Scanner sc = new Scanner(System.in);
        
        int a = sc.nextInt();
        int b = sc.nextInt();
        int c = sc.nextInt();
        int k = sc.nextInt();//3
        int sum_number = 0;

        int[] numbers = {1,0,-1};
        int[] cards = {a,b,c}; //2,1,1

        for(int i = 0; i<=2; i++){
            if(cards[i]<=k){
                sum_number = sum_number + (numbers[i] * cards[i]);
                k = k - cards[i];

            }else{
                sum_number = sum_number + (numbers[i] * k);
                break;
            }
        }
        System.out.println(sum_number);
        return;
    }    
} Main public static void main(String[] args) throws Exception{
        
        Scanner sc = new Scanner(System.in);
        
        int a = sc.nextInt();
        int b = sc.nextInt();
        int c = sc.nextInt();
        int k = sc.nextInt();//3
        int sum_number = 0;

        int[] numbers = {1,0,-1};
        int[] cards = {a,b,c}; //2,1,1

        for(int i = 0; i<=2; i++){
            if(cards[i]<=k){
                sum_number = sum_number + (numbers[i] * cards[i]);
                k = k - cards[i];

            }else{
                sum_number = sum_number + (numbers[i] * k);
                break;
            }
        }
        System.out.println(sum_number);
        return;
    } main {
        
        Scanner sc = new Scanner(System.in);
        
        int a = sc.nextInt();
        int b = sc.nextInt();
        int c = sc.nextInt();
        int k = sc.nextInt();//3
        int sum_number = 0;

        int[] numbers = {1,0,-1};
        int[] cards = {a,b,c}; //2,1,1

        for(int i = 0; i<=2; i++){
            if(cards[i]<=k){
                sum_number = sum_number + (numbers[i] * cards[i]);
                k = k - cards[i];

            }else{
                sum_number = sum_number + (numbers[i] * k);
                break;
            }
        }
        System.out.println(sum_number);
        return;
    } 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 sum_number = 0; sum_number 0 int[] numbers = {1,0,-1}; numbers {1,0,-1} 1 0 -1 1 int[] cards = {a,b,c}; cards {a,b,c} a b c for(int i = 0; i<=2; i++){
            if(cards[i]<=k){
                sum_number = sum_number + (numbers[i] * cards[i]);
                k = k - cards[i];

            }else{
                sum_number = sum_number + (numbers[i] * k);
                break;
            }
        } int i = 0; int i = 0; i 0 i<=2 i 2 i++ i++ i {
            if(cards[i]<=k){
                sum_number = sum_number + (numbers[i] * cards[i]);
                k = k - cards[i];

            }else{
                sum_number = sum_number + (numbers[i] * k);
                break;
            }
        } {
            if(cards[i]<=k){
                sum_number = sum_number + (numbers[i] * cards[i]);
                k = k - cards[i];

            }else{
                sum_number = sum_number + (numbers[i] * k);
                break;
            }
        } if(cards[i]<=k){
                sum_number = sum_number + (numbers[i] * cards[i]);
                k = k - cards[i];

            }else{
                sum_number = sum_number + (numbers[i] * k);
                break;
            } cards[i]<=k cards[i] cards i k {
                sum_number = sum_number + (numbers[i] * cards[i]);
                k = k - cards[i];

            } sum_number = sum_number + (numbers[i] * cards[i]) sum_number sum_number + (numbers[i] * cards[i]) sum_number (numbers[i] * cards[i]) numbers[i] numbers i cards[i] cards i k = k - cards[i] k k - cards[i] k cards[i] cards i {
                sum_number = sum_number + (numbers[i] * k);
                break;
            } sum_number = sum_number + (numbers[i] * k) sum_number sum_number + (numbers[i] * k) sum_number (numbers[i] * k) numbers[i] numbers i k break; System.out.println(sum_number) System.out.println System.out System System.out sum_number return; String[] args args public class Main{
    public static void main(String[] args) throws Exception{
        
        Scanner sc = new Scanner(System.in);
        
        int a = sc.nextInt();
        int b = sc.nextInt();
        int c = sc.nextInt();
        int k = sc.nextInt();//3
        int sum_number = 0;

        int[] numbers = {1,0,-1};
        int[] cards = {a,b,c}; //2,1,1

        for(int i = 0; i<=2; i++){
            if(cards[i]<=k){
                sum_number = sum_number + (numbers[i] * cards[i]);
                k = k - cards[i];

            }else{
                sum_number = sum_number + (numbers[i] * k);
                break;
            }
        }
        System.out.println(sum_number);
        return;
    }    
} public class Main{
    public static void main(String[] args) throws Exception{
        
        Scanner sc = new Scanner(System.in);
        
        int a = sc.nextInt();
        int b = sc.nextInt();
        int c = sc.nextInt();
        int k = sc.nextInt();//3
        int sum_number = 0;

        int[] numbers = {1,0,-1};
        int[] cards = {a,b,c}; //2,1,1

        for(int i = 0; i<=2; i++){
            if(cards[i]<=k){
                sum_number = sum_number + (numbers[i] * cards[i]);
                k = k - cards[i];

            }else{
                sum_number = sum_number + (numbers[i] * k);
                break;
            }
        }
        System.out.println(sum_number);
        return;
    }    
} Main
import java.util.*;


public class Main{
    public static void main(String[] args) throws Exception{
        
        Scanner sc = new Scanner(System.in);
        
        int a = sc.nextInt();
        int b = sc.nextInt();
        int c = sc.nextInt();
        int k = sc.nextInt();//3
        int sum_number = 0;

        int[] numbers = {1,0,-1};
        int[] cards = {a,b,c}; //2,1,1

        for(int i = 0; i<=2; i++){
            if(cards[i]<=k){
                sum_number = sum_number + (numbers[i] * cards[i]);
                k = k - cards[i];

            }else{
                sum_number = sum_number + (numbers[i] * k);
                break;
            }
        }
        System.out.println(sum_number);
        return;
    }    
}