import java.util.Scanner;
 
class Main {
	public static void main(String[] args) {
 
	    Scanner sc = new Scanner(System.in);
	    long A = sc.nextLong();
	    long B = sc.nextLong();
	    long C = sc.nextLong();
	    long K = sc.nextLong();
	    long max = 0;
	    
	    if (K > 0 && K >= A) {
	      max += (A * 1);
	      K -= A;
	    } else if (K > 0 && K < A) {
	      max += (K * 1);
	      K = 0;
	    }
	    
	    if (K > 0 && K >= B) {
	      K -= B;
	    } else if (K > 0 && K < B) {
	      K = 0;
	    }
	    
	    if(K > 0 && K >= C) {
	      max += (C * -1);
	      K -= C;
	    } else if (K > 0 && K < C) {
	      max += (K * -1);
	      K = 0;
	    }
	    
	    System.out.println(max);
	    
	  }
} import java.util.Scanner; Scanner java.util class Main {
	public static void main(String[] args) {
 
	    Scanner sc = new Scanner(System.in);
	    long A = sc.nextLong();
	    long B = sc.nextLong();
	    long C = sc.nextLong();
	    long K = sc.nextLong();
	    long max = 0;
	    
	    if (K > 0 && K >= A) {
	      max += (A * 1);
	      K -= A;
	    } else if (K > 0 && K < A) {
	      max += (K * 1);
	      K = 0;
	    }
	    
	    if (K > 0 && K >= B) {
	      K -= B;
	    } else if (K > 0 && K < B) {
	      K = 0;
	    }
	    
	    if(K > 0 && K >= C) {
	      max += (C * -1);
	      K -= C;
	    } else if (K > 0 && K < C) {
	      max += (K * -1);
	      K = 0;
	    }
	    
	    System.out.println(max);
	    
	  }
} Main public static void main(String[] args) {
 
	    Scanner sc = new Scanner(System.in);
	    long A = sc.nextLong();
	    long B = sc.nextLong();
	    long C = sc.nextLong();
	    long K = sc.nextLong();
	    long max = 0;
	    
	    if (K > 0 && K >= A) {
	      max += (A * 1);
	      K -= A;
	    } else if (K > 0 && K < A) {
	      max += (K * 1);
	      K = 0;
	    }
	    
	    if (K > 0 && K >= B) {
	      K -= B;
	    } else if (K > 0 && K < B) {
	      K = 0;
	    }
	    
	    if(K > 0 && K >= C) {
	      max += (C * -1);
	      K -= C;
	    } else if (K > 0 && K < C) {
	      max += (K * -1);
	      K = 0;
	    }
	    
	    System.out.println(max);
	    
	  } main {
 
	    Scanner sc = new Scanner(System.in);
	    long A = sc.nextLong();
	    long B = sc.nextLong();
	    long C = sc.nextLong();
	    long K = sc.nextLong();
	    long max = 0;
	    
	    if (K > 0 && K >= A) {
	      max += (A * 1);
	      K -= A;
	    } else if (K > 0 && K < A) {
	      max += (K * 1);
	      K = 0;
	    }
	    
	    if (K > 0 && K >= B) {
	      K -= B;
	    } else if (K > 0 && K < B) {
	      K = 0;
	    }
	    
	    if(K > 0 && K >= C) {
	      max += (C * -1);
	      K -= C;
	    } else if (K > 0 && K < C) {
	      max += (K * -1);
	      K = 0;
	    }
	    
	    System.out.println(max);
	    
	  } Scanner sc = new Scanner(System.in); sc new Scanner(System.in) long A = sc.nextLong(); A sc.nextLong() sc.nextLong sc long B = sc.nextLong(); B sc.nextLong() sc.nextLong sc long C = sc.nextLong(); C sc.nextLong() sc.nextLong sc long K = sc.nextLong(); K sc.nextLong() sc.nextLong sc long max = 0; max 0 if (K > 0 && K >= A) {
	      max += (A * 1);
	      K -= A;
	    } else if (K > 0 && K < A) {
	      max += (K * 1);
	      K = 0;
	    } K > 0 && K >= A K > 0 K 0 K >= A K A {
	      max += (A * 1);
	      K -= A;
	    } max += (A * 1) max (A * 1) A 1 K -= A K A if (K > 0 && K < A) {
	      max += (K * 1);
	      K = 0;
	    } K > 0 && K < A K > 0 K 0 K < A K A {
	      max += (K * 1);
	      K = 0;
	    } max += (K * 1) max (K * 1) K 1 K = 0 K 0 if (K > 0 && K >= B) {
	      K -= B;
	    } else if (K > 0 && K < B) {
	      K = 0;
	    } K > 0 && K >= B K > 0 K 0 K >= B K B {
	      K -= B;
	    } K -= B K B if (K > 0 && K < B) {
	      K = 0;
	    } K > 0 && K < B K > 0 K 0 K < B K B {
	      K = 0;
	    } K = 0 K 0 if(K > 0 && K >= C) {
	      max += (C * -1);
	      K -= C;
	    } else if (K > 0 && K < C) {
	      max += (K * -1);
	      K = 0;
	    } K > 0 && K >= C K > 0 K 0 K >= C K C {
	      max += (C * -1);
	      K -= C;
	    } max += (C * -1) max (C * -1) C -1 1 K -= C K C if (K > 0 && K < C) {
	      max += (K * -1);
	      K = 0;
	    } K > 0 && K < C K > 0 K 0 K < C K C {
	      max += (K * -1);
	      K = 0;
	    } max += (K * -1) max (K * -1) K -1 1 K = 0 K 0 System.out.println(max) System.out.println System.out System System.out max String[] args args
import java.util.Scanner;
 
class Main {
	public static void main(String[] args) {
 
	    Scanner sc = new Scanner(System.in);
	    long A = sc.nextLong();
	    long B = sc.nextLong();
	    long C = sc.nextLong();
	    long K = sc.nextLong();
	    long max = 0;
	    
	    if (K > 0 && K >= A) {
	      max += (A * 1);
	      K -= A;
	    } else if (K > 0 && K < A) {
	      max += (K * 1);
	      K = 0;
	    }
	    
	    if (K > 0 && K >= B) {
	      K -= B;
	    } else if (K > 0 && K < B) {
	      K = 0;
	    }
	    
	    if(K > 0 && K >= C) {
	      max += (C * -1);
	      K -= C;
	    } else if (K > 0 && K < C) {
	      max += (K * -1);
	      K = 0;
	    }
	    
	    System.out.println(max);
	    
	  }
}