import java.util.Scanner;

public class Main{
    public static void main(String args[]){

    Scanner scan = new Scanner(System.in);

    int a = scan.nextInt();
    int b = scan.nextInt();
    int c = scan.nextInt();
    int k = scan.nextInt();
    
    
        if (k <= a) {
            System.out.println(k);
        } else if (k > a && k <= a + b) {
            System.out.println(a);
        }  else if (k > a + b && k <= a + b + c) {
            System.out.println(a- (k-a-b));
        } 
    
      


      scan.close();
    }
  } import java.util.Scanner; Scanner java.util public class Main{
    public static void main(String args[]){

    Scanner scan = new Scanner(System.in);

    int a = scan.nextInt();
    int b = scan.nextInt();
    int c = scan.nextInt();
    int k = scan.nextInt();
    
    
        if (k <= a) {
            System.out.println(k);
        } else if (k > a && k <= a + b) {
            System.out.println(a);
        }  else if (k > a + b && k <= a + b + c) {
            System.out.println(a- (k-a-b));
        } 
    
      


      scan.close();
    }
  } Main public static void main(String args[]){

    Scanner scan = new Scanner(System.in);

    int a = scan.nextInt();
    int b = scan.nextInt();
    int c = scan.nextInt();
    int k = scan.nextInt();
    
    
        if (k <= a) {
            System.out.println(k);
        } else if (k > a && k <= a + b) {
            System.out.println(a);
        }  else if (k > a + b && k <= a + b + c) {
            System.out.println(a- (k-a-b));
        } 
    
      


      scan.close();
    } main {

    Scanner scan = new Scanner(System.in);

    int a = scan.nextInt();
    int b = scan.nextInt();
    int c = scan.nextInt();
    int k = scan.nextInt();
    
    
        if (k <= a) {
            System.out.println(k);
        } else if (k > a && k <= a + b) {
            System.out.println(a);
        }  else if (k > a + b && k <= a + b + c) {
            System.out.println(a- (k-a-b));
        } 
    
      


      scan.close();
    } Scanner scan = new Scanner(System.in); scan new Scanner(System.in) int a = scan.nextInt(); a scan.nextInt() scan.nextInt scan int b = scan.nextInt(); b scan.nextInt() scan.nextInt scan int c = scan.nextInt(); c scan.nextInt() scan.nextInt scan int k = scan.nextInt(); k scan.nextInt() scan.nextInt scan if (k <= a) {
            System.out.println(k);
        } else if (k > a && k <= a + b) {
            System.out.println(a);
        }  else if (k > a + b && k <= a + b + c) {
            System.out.println(a- (k-a-b));
        } k <= a k a {
            System.out.println(k);
        } System.out.println(k) System.out.println System.out System System.out k if (k > a && k <= a + b) {
            System.out.println(a);
        }  else if (k > a + b && k <= a + b + c) {
            System.out.println(a- (k-a-b));
        } k > a && k <= a + b k > a k a k <= a + b k a + b a b {
            System.out.println(a);
        } System.out.println(a) System.out.println System.out System System.out a if (k > a + b && k <= a + b + c) {
            System.out.println(a- (k-a-b));
        } k > a + b && k <= a + b + c k > a + b k a + b a b k <= a + b + c k a + b + c a + b + c a b c {
            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 k a b scan.close() scan.close scan String args[] args public class Main{
    public static void main(String args[]){

    Scanner scan = new Scanner(System.in);

    int a = scan.nextInt();
    int b = scan.nextInt();
    int c = scan.nextInt();
    int k = scan.nextInt();
    
    
        if (k <= a) {
            System.out.println(k);
        } else if (k > a && k <= a + b) {
            System.out.println(a);
        }  else if (k > a + b && k <= a + b + c) {
            System.out.println(a- (k-a-b));
        } 
    
      


      scan.close();
    }
  } public class Main{
    public static void main(String args[]){

    Scanner scan = new Scanner(System.in);

    int a = scan.nextInt();
    int b = scan.nextInt();
    int c = scan.nextInt();
    int k = scan.nextInt();
    
    
        if (k <= a) {
            System.out.println(k);
        } else if (k > a && k <= a + b) {
            System.out.println(a);
        }  else if (k > a + b && k <= a + b + c) {
            System.out.println(a- (k-a-b));
        } 
    
      


      scan.close();
    }
  } Main
import java.util.Scanner;

public class Main{
    public static void main(String args[]){

    Scanner scan = new Scanner(System.in);

    int a = scan.nextInt();
    int b = scan.nextInt();
    int c = scan.nextInt();
    int k = scan.nextInt();
    
    
        if (k <= a) {
            System.out.println(k);
        } else if (k > a && k <= a + b) {
            System.out.println(a);
        }  else if (k > a + b && k <= a + b + c) {
            System.out.println(a- (k-a-b));
        } 
    
      


      scan.close();
    }
  }