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 ans = 0;
	int b = K-A;
    int c = K-A-B;
	if(A==0 && C==0){
		System.out.println("0");
    }else if(K>=A && c<1){
		System.out.println(A);
	}else if(K>=A && c>=1){
		System.out.println(A+(c*-1));
	}else if(K<A){
        System.out.println(K);
      }
}
} 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 ans = 0;
	int b = K-A;
    int c = K-A-B;
	if(A==0 && C==0){
		System.out.println("0");
    }else if(K>=A && c<1){
		System.out.println(A);
	}else if(K>=A && c>=1){
		System.out.println(A+(c*-1));
	}else if(K<A){
        System.out.println(K);
      }
}
} 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 ans = 0;
	int b = K-A;
    int c = K-A-B;
	if(A==0 && C==0){
		System.out.println("0");
    }else if(K>=A && c<1){
		System.out.println(A);
	}else if(K>=A && c>=1){
		System.out.println(A+(c*-1));
	}else if(K<A){
        System.out.println(K);
      }
} main {
    
    Scanner sc = new Scanner(System.in);
    int A = sc.nextInt();
    int B = sc.nextInt();
    int C = sc.nextInt();
    int K = sc.nextInt();
    	int ans = 0;
	int b = K-A;
    int c = K-A-B;
	if(A==0 && C==0){
		System.out.println("0");
    }else if(K>=A && c<1){
		System.out.println(A);
	}else if(K>=A && c>=1){
		System.out.println(A+(c*-1));
	}else if(K<A){
        System.out.println(K);
      }
} 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 ans = 0; ans 0 int b = K-A; b K-A K A int c = K-A-B; c K-A-B K-A-B K A B if(A==0 && C==0){
		System.out.println("0");
    }else if(K>=A && c<1){
		System.out.println(A);
	}else if(K>=A && c>=1){
		System.out.println(A+(c*-1));
	}else if(K<A){
        System.out.println(K);
      } A==0 && C==0 A==0 A 0 C==0 C 0 {
		System.out.println("0");
    } System.out.println("0") System.out.println System.out System System.out "0" if(K>=A && c<1){
		System.out.println(A);
	}else if(K>=A && c>=1){
		System.out.println(A+(c*-1));
	}else if(K<A){
        System.out.println(K);
      } K>=A && c<1 K>=A K A c<1 c 1 {
		System.out.println(A);
	} System.out.println(A) System.out.println System.out System System.out A if(K>=A && c>=1){
		System.out.println(A+(c*-1));
	}else if(K<A){
        System.out.println(K);
      } K>=A && c>=1 K>=A K A c>=1 c 1 {
		System.out.println(A+(c*-1));
	} System.out.println(A+(c*-1)) System.out.println System.out System System.out A+(c*-1) A (c*-1) c -1 1 if(K<A){
        System.out.println(K);
      } K<A K A {
        System.out.println(K);
      } System.out.println(K) System.out.println System.out System System.out K 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 ans = 0;
	int b = K-A;
    int c = K-A-B;
	if(A==0 && C==0){
		System.out.println("0");
    }else if(K>=A && c<1){
		System.out.println(A);
	}else if(K>=A && c>=1){
		System.out.println(A+(c*-1));
	}else if(K<A){
        System.out.println(K);
      }
}
} 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 ans = 0;
	int b = K-A;
    int c = K-A-B;
	if(A==0 && C==0){
		System.out.println("0");
    }else if(K>=A && c<1){
		System.out.println(A);
	}else if(K>=A && c>=1){
		System.out.println(A+(c*-1));
	}else if(K<A){
        System.out.println(K);
      }
}
} 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 ans = 0;
	int b = K-A;
    int c = K-A-B;
	if(A==0 && C==0){
		System.out.println("0");
    }else if(K>=A && c<1){
		System.out.println(A);
	}else if(K>=A && c>=1){
		System.out.println(A+(c*-1));
	}else if(K<A){
        System.out.println(K);
      }
}
}