import java.util.Scanner;

class Main {
  public static void main(String[] args){ 
  Scanner scan = new Scanner(System.in);
  String[] input = scan.nextLine().split(" ");
  int a = Integer.parseInt(input[0]);
  int b = Integer.parseInt(input[1]);
  int c = Integer.parseInt(input[2]);
  int k = Integer.parseInt(input[3]);
   int out = 0;
   if((k>=a)){
   for(int i = a; i > 0; i--){
   out+=1;
   
   }
   
   for(int i = k-a-b; i > 0; i--){
   out+=-1;
   
   }
   } else {
   out = 1*k;
   }
   System.out.println(out);
  }


} import java.util.Scanner; Scanner java.util class Main {
  public static void main(String[] args){ 
  Scanner scan = new Scanner(System.in);
  String[] input = scan.nextLine().split(" ");
  int a = Integer.parseInt(input[0]);
  int b = Integer.parseInt(input[1]);
  int c = Integer.parseInt(input[2]);
  int k = Integer.parseInt(input[3]);
   int out = 0;
   if((k>=a)){
   for(int i = a; i > 0; i--){
   out+=1;
   
   }
   
   for(int i = k-a-b; i > 0; i--){
   out+=-1;
   
   }
   } else {
   out = 1*k;
   }
   System.out.println(out);
  }


} Main public static void main(String[] args){ 
  Scanner scan = new Scanner(System.in);
  String[] input = scan.nextLine().split(" ");
  int a = Integer.parseInt(input[0]);
  int b = Integer.parseInt(input[1]);
  int c = Integer.parseInt(input[2]);
  int k = Integer.parseInt(input[3]);
   int out = 0;
   if((k>=a)){
   for(int i = a; i > 0; i--){
   out+=1;
   
   }
   
   for(int i = k-a-b; i > 0; i--){
   out+=-1;
   
   }
   } else {
   out = 1*k;
   }
   System.out.println(out);
  } main { 
  Scanner scan = new Scanner(System.in);
  String[] input = scan.nextLine().split(" ");
  int a = Integer.parseInt(input[0]);
  int b = Integer.parseInt(input[1]);
  int c = Integer.parseInt(input[2]);
  int k = Integer.parseInt(input[3]);
   int out = 0;
   if((k>=a)){
   for(int i = a; i > 0; i--){
   out+=1;
   
   }
   
   for(int i = k-a-b; i > 0; i--){
   out+=-1;
   
   }
   } else {
   out = 1*k;
   }
   System.out.println(out);
  } Scanner scan = new Scanner(System.in); scan new Scanner(System.in) String[] input = scan.nextLine().split(" "); input scan.nextLine().split(" ") scan.nextLine().split scan.nextLine() scan.nextLine scan " " int a = Integer.parseInt(input[0]); a Integer.parseInt(input[0]) Integer.parseInt Integer input[0] input 0 int b = Integer.parseInt(input[1]); b Integer.parseInt(input[1]) Integer.parseInt Integer input[1] input 1 int c = Integer.parseInt(input[2]); c Integer.parseInt(input[2]) Integer.parseInt Integer input[2] input 2 int k = Integer.parseInt(input[3]); k Integer.parseInt(input[3]) Integer.parseInt Integer input[3] input 3 int out = 0; out 0 if((k>=a)){
   for(int i = a; i > 0; i--){
   out+=1;
   
   }
   
   for(int i = k-a-b; i > 0; i--){
   out+=-1;
   
   }
   } else {
   out = 1*k;
   } (k>=a) k a {
   for(int i = a; i > 0; i--){
   out+=1;
   
   }
   
   for(int i = k-a-b; i > 0; i--){
   out+=-1;
   
   }
   } for(int i = a; i > 0; i--){
   out+=1;
   
   } int i = a; int i = a; i a i > 0 i 0 i-- i-- i {
   out+=1;
   
   } {
   out+=1;
   
   } out+=1 out 1 for(int i = k-a-b; i > 0; i--){
   out+=-1;
   
   } int i = k-a-b; int i = k-a-b; i k-a-b k-a-b k a b i > 0 i 0 i-- i-- i {
   out+=-1;
   
   } {
   out+=-1;
   
   } out+=-1 out -1 1 {
   out = 1*k;
   } out = 1*k out 1*k 1 k System.out.println(out) System.out.println System.out System System.out out String[] args args
import java.util.Scanner;

class Main {
  public static void main(String[] args){ 
  Scanner scan = new Scanner(System.in);
  String[] input = scan.nextLine().split(" ");
  int a = Integer.parseInt(input[0]);
  int b = Integer.parseInt(input[1]);
  int c = Integer.parseInt(input[2]);
  int k = Integer.parseInt(input[3]);
   int out = 0;
   if((k>=a)){
   for(int i = a; i > 0; i--){
   out+=1;
   
   }
   
   for(int i = k-a-b; i > 0; i--){
   out+=-1;
   
   }
   } else {
   out = 1*k;
   }
   System.out.println(out);
  }


}