import 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 score = 0;
      while(K > 0){
        if(A > 0){
          A--;
          K--;
          score++;
        }
        else if(B > 0){
          B--;
          K--;
        }
        else if(C > 0){
          C--;
          K--;
          score--;
        }
      }
      System.out.println(score);
	
	}
} import java.util.*; util.* java 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 score = 0;
      while(K > 0){
        if(A > 0){
          A--;
          K--;
          score++;
        }
        else if(B > 0){
          B--;
          K--;
        }
        else if(C > 0){
          C--;
          K--;
          score--;
        }
      }
      System.out.println(score);
	
	}
} 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 score = 0;
      while(K > 0){
        if(A > 0){
          A--;
          K--;
          score++;
        }
        else if(B > 0){
          B--;
          K--;
        }
        else if(C > 0){
          C--;
          K--;
          score--;
        }
      }
      System.out.println(score);
	
	} main {
	Scanner sc = new Scanner(System.in);
	int A = sc.nextInt();
    int B = sc.nextInt();
    int C = sc.nextInt();
    int K = sc.nextInt();
      int score = 0;
      while(K > 0){
        if(A > 0){
          A--;
          K--;
          score++;
        }
        else if(B > 0){
          B--;
          K--;
        }
        else if(C > 0){
          C--;
          K--;
          score--;
        }
      }
      System.out.println(score);
	
	} 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 score = 0; score 0 while(K > 0){
        if(A > 0){
          A--;
          K--;
          score++;
        }
        else if(B > 0){
          B--;
          K--;
        }
        else if(C > 0){
          C--;
          K--;
          score--;
        }
      } K > 0 K 0 {
        if(A > 0){
          A--;
          K--;
          score++;
        }
        else if(B > 0){
          B--;
          K--;
        }
        else if(C > 0){
          C--;
          K--;
          score--;
        }
      } if(A > 0){
          A--;
          K--;
          score++;
        }
        else if(B > 0){
          B--;
          K--;
        }
        else if(C > 0){
          C--;
          K--;
          score--;
        } A > 0 A 0 {
          A--;
          K--;
          score++;
        } A-- A K-- K score++ score if(B > 0){
          B--;
          K--;
        }
        else if(C > 0){
          C--;
          K--;
          score--;
        } B > 0 B 0 {
          B--;
          K--;
        } B-- B K-- K if(C > 0){
          C--;
          K--;
          score--;
        } C > 0 C 0 {
          C--;
          K--;
          score--;
        } C-- C K-- K score-- score System.out.println(score) System.out.println System.out System System.out score 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 score = 0;
      while(K > 0){
        if(A > 0){
          A--;
          K--;
          score++;
        }
        else if(B > 0){
          B--;
          K--;
        }
        else if(C > 0){
          C--;
          K--;
          score--;
        }
      }
      System.out.println(score);
	
	}
} 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 score = 0;
      while(K > 0){
        if(A > 0){
          A--;
          K--;
          score++;
        }
        else if(B > 0){
          B--;
          K--;
        }
        else if(C > 0){
          C--;
          K--;
          score--;
        }
      }
      System.out.println(score);
	
	}
} Main
import 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 score = 0;
      while(K > 0){
        if(A > 0){
          A--;
          K--;
          score++;
        }
        else if(B > 0){
          B--;
          K--;
        }
        else if(C > 0){
          C--;
          K--;
          score--;
        }
      }
      System.out.println(score);
	
	}
}