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 D = sc.nextInt();
    sc.close();
    
    int result = 0;
    
     for(int i = 0; i < D; i++){
       if (A > 0){
         result += 1;
         A -= 1;
       } else if ( B > 0){
         B -= 1;
       } else {
         result -= 1;
         C -= 1;
       }
     }
    System.out.println(result);
  } 
} 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 D = sc.nextInt();
    sc.close();
    
    int result = 0;
    
     for(int i = 0; i < D; i++){
       if (A > 0){
         result += 1;
         A -= 1;
       } else if ( B > 0){
         B -= 1;
       } else {
         result -= 1;
         C -= 1;
       }
     }
    System.out.println(result);
  } 
} 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 D = sc.nextInt();
    sc.close();
    
    int result = 0;
    
     for(int i = 0; i < D; i++){
       if (A > 0){
         result += 1;
         A -= 1;
       } else if ( B > 0){
         B -= 1;
       } else {
         result -= 1;
         C -= 1;
       }
     }
    System.out.println(result);
  } main {
    Scanner sc = new Scanner(System.in);
    int A = sc.nextInt();
    int B = sc.nextInt();
    int C = sc.nextInt();
    int D = sc.nextInt();
    sc.close();
    
    int result = 0;
    
     for(int i = 0; i < D; i++){
       if (A > 0){
         result += 1;
         A -= 1;
       } else if ( B > 0){
         B -= 1;
       } else {
         result -= 1;
         C -= 1;
       }
     }
    System.out.println(result);
  } 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 D = sc.nextInt(); D sc.nextInt() sc.nextInt sc sc.close() sc.close sc int result = 0; result 0 for(int i = 0; i < D; i++){
       if (A > 0){
         result += 1;
         A -= 1;
       } else if ( B > 0){
         B -= 1;
       } else {
         result -= 1;
         C -= 1;
       }
     } int i = 0; int i = 0; i 0 i < D i D i++ i++ i {
       if (A > 0){
         result += 1;
         A -= 1;
       } else if ( B > 0){
         B -= 1;
       } else {
         result -= 1;
         C -= 1;
       }
     } {
       if (A > 0){
         result += 1;
         A -= 1;
       } else if ( B > 0){
         B -= 1;
       } else {
         result -= 1;
         C -= 1;
       }
     } if (A > 0){
         result += 1;
         A -= 1;
       } else if ( B > 0){
         B -= 1;
       } else {
         result -= 1;
         C -= 1;
       } A > 0 A 0 {
         result += 1;
         A -= 1;
       } result += 1 result 1 A -= 1 A 1 if ( B > 0){
         B -= 1;
       } else {
         result -= 1;
         C -= 1;
       } B > 0 B 0 {
         B -= 1;
       } B -= 1 B 1 {
         result -= 1;
         C -= 1;
       } result -= 1 result 1 C -= 1 C 1 System.out.println(result) System.out.println System.out System System.out result 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 D = sc.nextInt();
    sc.close();
    
    int result = 0;
    
     for(int i = 0; i < D; i++){
       if (A > 0){
         result += 1;
         A -= 1;
       } else if ( B > 0){
         B -= 1;
       } else {
         result -= 1;
         C -= 1;
       }
     }
    System.out.println(result);
  } 
} 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 D = sc.nextInt();
    sc.close();
    
    int result = 0;
    
     for(int i = 0; i < D; i++){
       if (A > 0){
         result += 1;
         A -= 1;
       } else if ( B > 0){
         B -= 1;
       } else {
         result -= 1;
         C -= 1;
       }
     }
    System.out.println(result);
  } 
} 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 D = sc.nextInt();
    sc.close();
    
    int result = 0;
    
     for(int i = 0; i < D; i++){
       if (A > 0){
         result += 1;
         A -= 1;
       } else if ( B > 0){
         B -= 1;
       } else {
         result -= 1;
         C -= 1;
       }
     }
    System.out.println(result);
  } 
}
