import java.util.Scanner;
 
public class Main {
  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
	int num1 = Integer.parseInt(sc.next());
	int num0 = Integer.parseInt(sc.next());
    int numMinus1 = Integer.parseInt(sc.next());
    int numToSelect = Integer.parseInt(sc.next());

    int sum = 0;
	if(numToSelect <= num1){
    	sum = numToSelect * 1;
    }else if(num1 < numToSelect && numToSelect <= (num1+num0)){
        sum = num1 * 1;
    }else if((num1+num0) < numToSelect && numToSelect <= (num1+num0+numMinus1)){
        sum = num1 * 1 + (numToSelect-(num1+num0)) * -1;
    }else if((num1+num0+numMinus1) < numToSelect){
      	sum = num1 * 1 + numMinus1 * -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 num1 = Integer.parseInt(sc.next());
	int num0 = Integer.parseInt(sc.next());
    int numMinus1 = Integer.parseInt(sc.next());
    int numToSelect = Integer.parseInt(sc.next());

    int sum = 0;
	if(numToSelect <= num1){
    	sum = numToSelect * 1;
    }else if(num1 < numToSelect && numToSelect <= (num1+num0)){
        sum = num1 * 1;
    }else if((num1+num0) < numToSelect && numToSelect <= (num1+num0+numMinus1)){
        sum = num1 * 1 + (numToSelect-(num1+num0)) * -1;
    }else if((num1+num0+numMinus1) < numToSelect){
      	sum = num1 * 1 + numMinus1 * -1;
    }

    System.out.println(sum);
  }
} Main public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
	int num1 = Integer.parseInt(sc.next());
	int num0 = Integer.parseInt(sc.next());
    int numMinus1 = Integer.parseInt(sc.next());
    int numToSelect = Integer.parseInt(sc.next());

    int sum = 0;
	if(numToSelect <= num1){
    	sum = numToSelect * 1;
    }else if(num1 < numToSelect && numToSelect <= (num1+num0)){
        sum = num1 * 1;
    }else if((num1+num0) < numToSelect && numToSelect <= (num1+num0+numMinus1)){
        sum = num1 * 1 + (numToSelect-(num1+num0)) * -1;
    }else if((num1+num0+numMinus1) < numToSelect){
      	sum = num1 * 1 + numMinus1 * -1;
    }

    System.out.println(sum);
  } main {
    Scanner sc = new Scanner(System.in);
	int num1 = Integer.parseInt(sc.next());
	int num0 = Integer.parseInt(sc.next());
    int numMinus1 = Integer.parseInt(sc.next());
    int numToSelect = Integer.parseInt(sc.next());

    int sum = 0;
	if(numToSelect <= num1){
    	sum = numToSelect * 1;
    }else if(num1 < numToSelect && numToSelect <= (num1+num0)){
        sum = num1 * 1;
    }else if((num1+num0) < numToSelect && numToSelect <= (num1+num0+numMinus1)){
        sum = num1 * 1 + (numToSelect-(num1+num0)) * -1;
    }else if((num1+num0+numMinus1) < numToSelect){
      	sum = num1 * 1 + numMinus1 * -1;
    }

    System.out.println(sum);
  } Scanner sc = new Scanner(System.in); sc new Scanner(System.in) int num1 = Integer.parseInt(sc.next()); num1 Integer.parseInt(sc.next()) Integer.parseInt Integer sc.next() sc.next sc int num0 = Integer.parseInt(sc.next()); num0 Integer.parseInt(sc.next()) Integer.parseInt Integer sc.next() sc.next sc int numMinus1 = Integer.parseInt(sc.next()); numMinus1 Integer.parseInt(sc.next()) Integer.parseInt Integer sc.next() sc.next sc int numToSelect = Integer.parseInt(sc.next()); numToSelect Integer.parseInt(sc.next()) Integer.parseInt Integer sc.next() sc.next sc int sum = 0; sum 0 if(numToSelect <= num1){
    	sum = numToSelect * 1;
    }else if(num1 < numToSelect && numToSelect <= (num1+num0)){
        sum = num1 * 1;
    }else if((num1+num0) < numToSelect && numToSelect <= (num1+num0+numMinus1)){
        sum = num1 * 1 + (numToSelect-(num1+num0)) * -1;
    }else if((num1+num0+numMinus1) < numToSelect){
      	sum = num1 * 1 + numMinus1 * -1;
    } numToSelect <= num1 numToSelect num1 {
    	sum = numToSelect * 1;
    } sum = numToSelect * 1 sum numToSelect * 1 numToSelect 1 if(num1 < numToSelect && numToSelect <= (num1+num0)){
        sum = num1 * 1;
    }else if((num1+num0) < numToSelect && numToSelect <= (num1+num0+numMinus1)){
        sum = num1 * 1 + (numToSelect-(num1+num0)) * -1;
    }else if((num1+num0+numMinus1) < numToSelect){
      	sum = num1 * 1 + numMinus1 * -1;
    } num1 < numToSelect && numToSelect <= (num1+num0) num1 < numToSelect num1 numToSelect numToSelect <= (num1+num0) numToSelect (num1+num0) num1 num0 {
        sum = num1 * 1;
    } sum = num1 * 1 sum num1 * 1 num1 1 if((num1+num0) < numToSelect && numToSelect <= (num1+num0+numMinus1)){
        sum = num1 * 1 + (numToSelect-(num1+num0)) * -1;
    }else if((num1+num0+numMinus1) < numToSelect){
      	sum = num1 * 1 + numMinus1 * -1;
    } (num1+num0) < numToSelect && numToSelect <= (num1+num0+numMinus1) (num1+num0) < numToSelect (num1+num0) num1 num0 numToSelect numToSelect <= (num1+num0+numMinus1) numToSelect (num1+num0+numMinus1) num1+num0+numMinus1 num1 num0 numMinus1 {
        sum = num1 * 1 + (numToSelect-(num1+num0)) * -1;
    } sum = num1 * 1 + (numToSelect-(num1+num0)) * -1 sum num1 * 1 + (numToSelect-(num1+num0)) * -1 num1 * 1 num1 1 (numToSelect-(num1+num0)) * -1 (numToSelect-(num1+num0)) numToSelect (num1+num0) num1 num0 -1 1 if((num1+num0+numMinus1) < numToSelect){
      	sum = num1 * 1 + numMinus1 * -1;
    } (num1+num0+numMinus1) < numToSelect (num1+num0+numMinus1) num1+num0+numMinus1 num1 num0 numMinus1 numToSelect {
      	sum = num1 * 1 + numMinus1 * -1;
    } sum = num1 * 1 + numMinus1 * -1 sum num1 * 1 + numMinus1 * -1 num1 * 1 num1 1 numMinus1 * -1 numMinus1 -1 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 num1 = Integer.parseInt(sc.next());
	int num0 = Integer.parseInt(sc.next());
    int numMinus1 = Integer.parseInt(sc.next());
    int numToSelect = Integer.parseInt(sc.next());

    int sum = 0;
	if(numToSelect <= num1){
    	sum = numToSelect * 1;
    }else if(num1 < numToSelect && numToSelect <= (num1+num0)){
        sum = num1 * 1;
    }else if((num1+num0) < numToSelect && numToSelect <= (num1+num0+numMinus1)){
        sum = num1 * 1 + (numToSelect-(num1+num0)) * -1;
    }else if((num1+num0+numMinus1) < numToSelect){
      	sum = num1 * 1 + numMinus1 * -1;
    }

    System.out.println(sum);
  }
} public class Main {
  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
	int num1 = Integer.parseInt(sc.next());
	int num0 = Integer.parseInt(sc.next());
    int numMinus1 = Integer.parseInt(sc.next());
    int numToSelect = Integer.parseInt(sc.next());

    int sum = 0;
	if(numToSelect <= num1){
    	sum = numToSelect * 1;
    }else if(num1 < numToSelect && numToSelect <= (num1+num0)){
        sum = num1 * 1;
    }else if((num1+num0) < numToSelect && numToSelect <= (num1+num0+numMinus1)){
        sum = num1 * 1 + (numToSelect-(num1+num0)) * -1;
    }else if((num1+num0+numMinus1) < numToSelect){
      	sum = num1 * 1 + numMinus1 * -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 num1 = Integer.parseInt(sc.next());
	int num0 = Integer.parseInt(sc.next());
    int numMinus1 = Integer.parseInt(sc.next());
    int numToSelect = Integer.parseInt(sc.next());

    int sum = 0;
	if(numToSelect <= num1){
    	sum = numToSelect * 1;
    }else if(num1 < numToSelect && numToSelect <= (num1+num0)){
        sum = num1 * 1;
    }else if((num1+num0) < numToSelect && numToSelect <= (num1+num0+numMinus1)){
        sum = num1 * 1 + (numToSelect-(num1+num0)) * -1;
    }else if((num1+num0+numMinus1) < numToSelect){
      	sum = num1 * 1 + numMinus1 * -1;
    }

    System.out.println(sum);
  }
}