import java.util.Scanner;

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 count=0;
   
   	if(A>=K){
    count=K;
    }
   	else if(A<K&&A+B>=K){
    count=A;}
    else if(A+B<K){
    count=A-(K-A-B);}
 
  System.out.println(count);
 }
} import java.util.Scanner; Scanner java.util 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 count=0;
   
   	if(A>=K){
    count=K;
    }
   	else if(A<K&&A+B>=K){
    count=A;}
    else if(A+B<K){
    count=A-(K-A-B);}
 
  System.out.println(count);
 }
} 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 count=0;
   
   	if(A>=K){
    count=K;
    }
   	else if(A<K&&A+B>=K){
    count=A;}
    else if(A+B<K){
    count=A-(K-A-B);}
 
  System.out.println(count);
 } main {
   Scanner sc = new Scanner(System.in);
   	int A = sc.nextInt();
    int B = sc.nextInt();
   	int C = sc.nextInt();
   	int K = sc.nextInt();
 	int count=0;
   
   	if(A>=K){
    count=K;
    }
   	else if(A<K&&A+B>=K){
    count=A;}
    else if(A+B<K){
    count=A-(K-A-B);}
 
  System.out.println(count);
 } 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 count=0; count 0 if(A>=K){
    count=K;
    }
   	else if(A<K&&A+B>=K){
    count=A;}
    else if(A+B<K){
    count=A-(K-A-B);} A>=K A K {
    count=K;
    } count=K count K if(A<K&&A+B>=K){
    count=A;}
    else if(A+B<K){
    count=A-(K-A-B);} A<K&&A+B>=K A<K A K A+B>=K A+B A B K {
    count=A;} count=A count A if(A+B<K){
    count=A-(K-A-B);} A+B<K A+B A B K {
    count=A-(K-A-B);} count=A-(K-A-B) count A-(K-A-B) A (K-A-B) K-A-B K A B System.out.println(count) System.out.println System.out System System.out count String args[] args
import java.util.Scanner;

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 count=0;
   
   	if(A>=K){
    count=K;
    }
   	else if(A<K&&A+B>=K){
    count=A;}
    else if(A+B<K){
    count=A-(K-A-B);}
 
  System.out.println(count);
 }
}
