import java.util.Scanner;
 
public class Main {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        String A = scan.next();
        String B = scan.next();
        String C = scan.next();
        String K = scan.next();
        int a = Integer.parseInt(A);
        int b = Integer.parseInt(B);
        int c = Integer.parseInt(C);
        int k = Integer.parseInt(K);
 
        if (k < a) {
          System.out.println(k);
        }
        else if (k <= a + b) {
          System.out.println(a);
        } else {
          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);
        String A = scan.next();
        String B = scan.next();
        String C = scan.next();
        String K = scan.next();
        int a = Integer.parseInt(A);
        int b = Integer.parseInt(B);
        int c = Integer.parseInt(C);
        int k = Integer.parseInt(K);
 
        if (k < a) {
          System.out.println(k);
        }
        else if (k <= a + b) {
          System.out.println(a);
        } else {
          System.out.println(a - (k - (a + b)));
        }
        
        scan.close();
    }
} Main public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        String A = scan.next();
        String B = scan.next();
        String C = scan.next();
        String K = scan.next();
        int a = Integer.parseInt(A);
        int b = Integer.parseInt(B);
        int c = Integer.parseInt(C);
        int k = Integer.parseInt(K);
 
        if (k < a) {
          System.out.println(k);
        }
        else if (k <= a + b) {
          System.out.println(a);
        } else {
          System.out.println(a - (k - (a + b)));
        }
        
        scan.close();
    } main {
        Scanner scan = new Scanner(System.in);
        String A = scan.next();
        String B = scan.next();
        String C = scan.next();
        String K = scan.next();
        int a = Integer.parseInt(A);
        int b = Integer.parseInt(B);
        int c = Integer.parseInt(C);
        int k = Integer.parseInt(K);
 
        if (k < a) {
          System.out.println(k);
        }
        else if (k <= a + b) {
          System.out.println(a);
        } else {
          System.out.println(a - (k - (a + b)));
        }
        
        scan.close();
    } Scanner scan = new Scanner(System.in); scan new Scanner(System.in) String A = scan.next(); A scan.next() scan.next scan String B = scan.next(); B scan.next() scan.next scan String C = scan.next(); C scan.next() scan.next scan String K = scan.next(); K scan.next() scan.next scan int a = Integer.parseInt(A); a Integer.parseInt(A) Integer.parseInt Integer A int b = Integer.parseInt(B); b Integer.parseInt(B) Integer.parseInt Integer B int c = Integer.parseInt(C); c Integer.parseInt(C) Integer.parseInt Integer C int k = Integer.parseInt(K); k Integer.parseInt(K) Integer.parseInt Integer K if (k < a) {
          System.out.println(k);
        }
        else if (k <= a + b) {
          System.out.println(a);
        } else {
          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 + b) {
          System.out.println(a);
        } else {
          System.out.println(a - (k - (a + b)));
        } 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 {
          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 scan.close() scan.close scan String[] args args public class Main {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        String A = scan.next();
        String B = scan.next();
        String C = scan.next();
        String K = scan.next();
        int a = Integer.parseInt(A);
        int b = Integer.parseInt(B);
        int c = Integer.parseInt(C);
        int k = Integer.parseInt(K);
 
        if (k < a) {
          System.out.println(k);
        }
        else if (k <= a + b) {
          System.out.println(a);
        } else {
          System.out.println(a - (k - (a + b)));
        }
        
        scan.close();
    }
} public class Main {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        String A = scan.next();
        String B = scan.next();
        String C = scan.next();
        String K = scan.next();
        int a = Integer.parseInt(A);
        int b = Integer.parseInt(B);
        int c = Integer.parseInt(C);
        int k = Integer.parseInt(K);
 
        if (k < a) {
          System.out.println(k);
        }
        else if (k <= a + b) {
          System.out.println(a);
        } else {
          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);
        String A = scan.next();
        String B = scan.next();
        String C = scan.next();
        String K = scan.next();
        int a = Integer.parseInt(A);
        int b = Integer.parseInt(B);
        int c = Integer.parseInt(C);
        int k = Integer.parseInt(K);
 
        if (k < a) {
          System.out.println(k);
        }
        else if (k <= a + b) {
          System.out.println(a);
        } else {
          System.out.println(a - (k - (a + b)));
        }
        
        scan.close();
    }
}