import java.util.Scanner;
 
public class Main {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int Amax = scan.nextInt();
        int Bmax = scan.nextInt();
        int Cmax = scan.nextInt();
        int kosu = scan.nextInt();
		int Akosu = 0;
        int Bkosu = 0;
        int Ckosu = 0;
        if (Amax < kosu) {
          Akosu = Amax;
          if ((Amax + Bmax) < kosu) {
            Bkosu = Bmax;
            Ckosu = kosu - Bmax - Amax;
          } else {
            Bkosu = kosu - Amax;
          }
        } else {
          Akosu = kosu;
        }
        System.out.println(Akosu - Ckosu);
    }
} import java.util.Scanner; Scanner java.util public class Main {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int Amax = scan.nextInt();
        int Bmax = scan.nextInt();
        int Cmax = scan.nextInt();
        int kosu = scan.nextInt();
		int Akosu = 0;
        int Bkosu = 0;
        int Ckosu = 0;
        if (Amax < kosu) {
          Akosu = Amax;
          if ((Amax + Bmax) < kosu) {
            Bkosu = Bmax;
            Ckosu = kosu - Bmax - Amax;
          } else {
            Bkosu = kosu - Amax;
          }
        } else {
          Akosu = kosu;
        }
        System.out.println(Akosu - Ckosu);
    }
} Main public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int Amax = scan.nextInt();
        int Bmax = scan.nextInt();
        int Cmax = scan.nextInt();
        int kosu = scan.nextInt();
		int Akosu = 0;
        int Bkosu = 0;
        int Ckosu = 0;
        if (Amax < kosu) {
          Akosu = Amax;
          if ((Amax + Bmax) < kosu) {
            Bkosu = Bmax;
            Ckosu = kosu - Bmax - Amax;
          } else {
            Bkosu = kosu - Amax;
          }
        } else {
          Akosu = kosu;
        }
        System.out.println(Akosu - Ckosu);
    } main {
        Scanner scan = new Scanner(System.in);
        int Amax = scan.nextInt();
        int Bmax = scan.nextInt();
        int Cmax = scan.nextInt();
        int kosu = scan.nextInt();
		int Akosu = 0;
        int Bkosu = 0;
        int Ckosu = 0;
        if (Amax < kosu) {
          Akosu = Amax;
          if ((Amax + Bmax) < kosu) {
            Bkosu = Bmax;
            Ckosu = kosu - Bmax - Amax;
          } else {
            Bkosu = kosu - Amax;
          }
        } else {
          Akosu = kosu;
        }
        System.out.println(Akosu - Ckosu);
    } Scanner scan = new Scanner(System.in); scan new Scanner(System.in) int Amax = scan.nextInt(); Amax scan.nextInt() scan.nextInt scan int Bmax = scan.nextInt(); Bmax scan.nextInt() scan.nextInt scan int Cmax = scan.nextInt(); Cmax scan.nextInt() scan.nextInt scan int kosu = scan.nextInt(); kosu scan.nextInt() scan.nextInt scan int Akosu = 0; Akosu 0 int Bkosu = 0; Bkosu 0 int Ckosu = 0; Ckosu 0 if (Amax < kosu) {
          Akosu = Amax;
          if ((Amax + Bmax) < kosu) {
            Bkosu = Bmax;
            Ckosu = kosu - Bmax - Amax;
          } else {
            Bkosu = kosu - Amax;
          }
        } else {
          Akosu = kosu;
        } Amax < kosu Amax kosu {
          Akosu = Amax;
          if ((Amax + Bmax) < kosu) {
            Bkosu = Bmax;
            Ckosu = kosu - Bmax - Amax;
          } else {
            Bkosu = kosu - Amax;
          }
        } Akosu = Amax Akosu Amax if ((Amax + Bmax) < kosu) {
            Bkosu = Bmax;
            Ckosu = kosu - Bmax - Amax;
          } else {
            Bkosu = kosu - Amax;
          } (Amax + Bmax) < kosu (Amax + Bmax) Amax Bmax kosu {
            Bkosu = Bmax;
            Ckosu = kosu - Bmax - Amax;
          } Bkosu = Bmax Bkosu Bmax Ckosu = kosu - Bmax - Amax Ckosu kosu - Bmax - Amax kosu - Bmax - Amax kosu Bmax Amax {
            Bkosu = kosu - Amax;
          } Bkosu = kosu - Amax Bkosu kosu - Amax kosu Amax {
          Akosu = kosu;
        } Akosu = kosu Akosu kosu System.out.println(Akosu - Ckosu) System.out.println System.out System System.out Akosu - Ckosu Akosu Ckosu String[] args args public class Main {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int Amax = scan.nextInt();
        int Bmax = scan.nextInt();
        int Cmax = scan.nextInt();
        int kosu = scan.nextInt();
		int Akosu = 0;
        int Bkosu = 0;
        int Ckosu = 0;
        if (Amax < kosu) {
          Akosu = Amax;
          if ((Amax + Bmax) < kosu) {
            Bkosu = Bmax;
            Ckosu = kosu - Bmax - Amax;
          } else {
            Bkosu = kosu - Amax;
          }
        } else {
          Akosu = kosu;
        }
        System.out.println(Akosu - Ckosu);
    }
} public class Main {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int Amax = scan.nextInt();
        int Bmax = scan.nextInt();
        int Cmax = scan.nextInt();
        int kosu = scan.nextInt();
		int Akosu = 0;
        int Bkosu = 0;
        int Ckosu = 0;
        if (Amax < kosu) {
          Akosu = Amax;
          if ((Amax + Bmax) < kosu) {
            Bkosu = Bmax;
            Ckosu = kosu - Bmax - Amax;
          } else {
            Bkosu = kosu - Amax;
          }
        } else {
          Akosu = kosu;
        }
        System.out.println(Akosu - Ckosu);
    }
} Main
import java.util.Scanner;
 
public class Main {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int Amax = scan.nextInt();
        int Bmax = scan.nextInt();
        int Cmax = scan.nextInt();
        int kosu = scan.nextInt();
		int Akosu = 0;
        int Bkosu = 0;
        int Ckosu = 0;
        if (Amax < kosu) {
          Akosu = Amax;
          if ((Amax + Bmax) < kosu) {
            Bkosu = Bmax;
            Ckosu = kosu - Bmax - Amax;
          } else {
            Bkosu = kosu - Amax;
          }
        } else {
          Akosu = kosu;
        }
        System.out.println(Akosu - Ckosu);
    }
}