import java.util.*;

public 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();
                
      if (a > k) {
         System.out.println(k);
      } else if (a == k) {
         System.out.println(a);
      } else {
         if (b >= (k - a)) {
            System.out.println(a);
         } else {
            System.out.println(a - (k - (a + b)));
         }
      }      
   }
} import java.util.*; util.* java public 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();
                
      if (a > k) {
         System.out.println(k);
      } else if (a == k) {
         System.out.println(a);
      } else {
         if (b >= (k - a)) {
            System.out.println(a);
         } else {
            System.out.println(a - (k - (a + b)));
         }
      }      
   }
} 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();
                
      if (a > k) {
         System.out.println(k);
      } else if (a == k) {
         System.out.println(a);
      } else {
         if (b >= (k - a)) {
            System.out.println(a);
         } else {
            System.out.println(a - (k - (a + b)));
         }
      }      
   } main {
      Scanner sc = new Scanner(System.in);
      
      
      long a = sc.nextLong();
      long b = sc.nextLong();
      long c = sc.nextLong();
      long k = sc.nextLong();
                
      if (a > k) {
         System.out.println(k);
      } else if (a == k) {
         System.out.println(a);
      } else {
         if (b >= (k - a)) {
            System.out.println(a);
         } else {
            System.out.println(a - (k - (a + b)));
         }
      }      
   } 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 if (a > k) {
         System.out.println(k);
      } else if (a == k) {
         System.out.println(a);
      } else {
         if (b >= (k - a)) {
            System.out.println(a);
         } else {
            System.out.println(a - (k - (a + b)));
         }
      } a > k a k {
         System.out.println(k);
      } System.out.println(k) System.out.println System.out System System.out k if (a == k) {
         System.out.println(a);
      } else {
         if (b >= (k - a)) {
            System.out.println(a);
         } else {
            System.out.println(a - (k - (a + b)));
         }
      } a == k a k {
         System.out.println(a);
      } System.out.println(a) System.out.println System.out System System.out a {
         if (b >= (k - a)) {
            System.out.println(a);
         } else {
            System.out.println(a - (k - (a + b)));
         }
      } if (b >= (k - a)) {
            System.out.println(a);
         } else {
            System.out.println(a - (k - (a + b)));
         } b >= (k - a) b (k - a) k a {
            System.out.println(a);
         } System.out.println(a) System.out.println System.out System System.out a {
            System.out.println(a - (k - (a + b)));
         } System.out.println(a - (k - (a + b))) System.out.println System.out System System.out a - (k - (a + b)) a (k - (a + b)) k (a + b) a b String[] args args public 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();
                
      if (a > k) {
         System.out.println(k);
      } else if (a == k) {
         System.out.println(a);
      } else {
         if (b >= (k - a)) {
            System.out.println(a);
         } else {
            System.out.println(a - (k - (a + b)));
         }
      }      
   }
} public 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();
                
      if (a > k) {
         System.out.println(k);
      } else if (a == k) {
         System.out.println(a);
      } else {
         if (b >= (k - a)) {
            System.out.println(a);
         } else {
            System.out.println(a - (k - (a + b)));
         }
      }      
   }
} Main
import java.util.*;

public 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();
                
      if (a > k) {
         System.out.println(k);
      } else if (a == k) {
         System.out.println(a);
      } else {
         if (b >= (k - a)) {
            System.out.println(a);
         } else {
            System.out.println(a - (k - (a + b)));
         }
      }      
   }
}