import java.util.Scanner;


public class Main{
    public static Scanner scan  = new Scanner(System.in);
    public static void main(String[]args){
        int a = nextInt();
        int  b= nextInt();
        int  c= nextInt();
        int k = nextInt();
        int sum = 0;

        if(k<=a){
            print(k);
            System.exit(0);
        }
        if(k<=a+b){
            print(a);
            System.exit(0);
        }
        else{
            print(a-(k-a-b));
            System.exit(0);
        }
    }

    public static int nextInt(){
        return Integer.parseInt(scan.next());
    }
    public static long nextLong(){
        return Long.parseLong(scan.next());
    }
    public static String next(){
        return scan.next();
    }
    public static double nextDouble(){
        return Double.parseDouble(scan.next());
    }
    public static float nextFloat(){
        return Float.parseFloat(scan.next());
    }

    //Yes or No
    public static void yesNo(boolean flag){
        if(flag) System.out.println("Yes");
        else System.out.println("No");
    }
    public static void print(Object a){
        System.out.println(a);
    }
} import java.util.Scanner; Scanner java.util public class Main{
    public static Scanner scan  = new Scanner(System.in);
    public static void main(String[]args){
        int a = nextInt();
        int  b= nextInt();
        int  c= nextInt();
        int k = nextInt();
        int sum = 0;

        if(k<=a){
            print(k);
            System.exit(0);
        }
        if(k<=a+b){
            print(a);
            System.exit(0);
        }
        else{
            print(a-(k-a-b));
            System.exit(0);
        }
    }

    public static int nextInt(){
        return Integer.parseInt(scan.next());
    }
    public static long nextLong(){
        return Long.parseLong(scan.next());
    }
    public static String next(){
        return scan.next();
    }
    public static double nextDouble(){
        return Double.parseDouble(scan.next());
    }
    public static float nextFloat(){
        return Float.parseFloat(scan.next());
    }

    //Yes or No
    public static void yesNo(boolean flag){
        if(flag) System.out.println("Yes");
        else System.out.println("No");
    }
    public static void print(Object a){
        System.out.println(a);
    }
} Main public static Scanner scan  = new Scanner(System.in); scan new Scanner(System.in) public static void main(String[]args){
        int a = nextInt();
        int  b= nextInt();
        int  c= nextInt();
        int k = nextInt();
        int sum = 0;

        if(k<=a){
            print(k);
            System.exit(0);
        }
        if(k<=a+b){
            print(a);
            System.exit(0);
        }
        else{
            print(a-(k-a-b));
            System.exit(0);
        }
    } main {
        int a = nextInt();
        int  b= nextInt();
        int  c= nextInt();
        int k = nextInt();
        int sum = 0;

        if(k<=a){
            print(k);
            System.exit(0);
        }
        if(k<=a+b){
            print(a);
            System.exit(0);
        }
        else{
            print(a-(k-a-b));
            System.exit(0);
        }
    } int a = nextInt(); a nextInt() nextInt int  b= nextInt(); b nextInt() nextInt int  c= nextInt(); c nextInt() nextInt int k = nextInt(); k nextInt() nextInt int sum = 0; sum 0 if(k<=a){
            print(k);
            System.exit(0);
        } k<=a k a {
            print(k);
            System.exit(0);
        } print(k) print k System.exit(0) System.exit System 0 if(k<=a+b){
            print(a);
            System.exit(0);
        }
        else{
            print(a-(k-a-b));
            System.exit(0);
        } k<=a+b k a+b a b {
            print(a);
            System.exit(0);
        } print(a) print a System.exit(0) System.exit System 0 {
            print(a-(k-a-b));
            System.exit(0);
        } print(a-(k-a-b)) print a-(k-a-b) a (k-a-b) k-a-b k a b System.exit(0) System.exit System 0 String[]args args public static int nextInt(){
        return Integer.parseInt(scan.next());
    } nextInt {
        return Integer.parseInt(scan.next());
    } return Integer.parseInt(scan.next()); Integer.parseInt(scan.next()) Integer.parseInt Integer scan.next() scan.next scan public static long nextLong(){
        return Long.parseLong(scan.next());
    } nextLong {
        return Long.parseLong(scan.next());
    } return Long.parseLong(scan.next()); Long.parseLong(scan.next()) Long.parseLong Long scan.next() scan.next scan public static String next(){
        return scan.next();
    } next {
        return scan.next();
    } return scan.next(); scan.next() scan.next scan public static double nextDouble(){
        return Double.parseDouble(scan.next());
    } nextDouble {
        return Double.parseDouble(scan.next());
    } return Double.parseDouble(scan.next()); Double.parseDouble(scan.next()) Double.parseDouble Double scan.next() scan.next scan public static float nextFloat(){
        return Float.parseFloat(scan.next());
    } nextFloat {
        return Float.parseFloat(scan.next());
    } return Float.parseFloat(scan.next()); Float.parseFloat(scan.next()) Float.parseFloat Float scan.next() scan.next scan //Yes or No
    public static void yesNo(boolean flag){
        if(flag) System.out.println("Yes");
        else System.out.println("No");
    } yesNo {
        if(flag) System.out.println("Yes");
        else System.out.println("No");
    } if(flag) System.out.println("Yes");
        else System.out.println("No"); flag System.out.println("Yes") System.out.println System.out System System.out "Yes" System.out.println("No") System.out.println System.out System System.out "No" boolean flag flag public static void print(Object a){
        System.out.println(a);
    } print {
        System.out.println(a);
    } System.out.println(a) System.out.println System.out System System.out a Object a a public class Main{
    public static Scanner scan  = new Scanner(System.in);
    public static void main(String[]args){
        int a = nextInt();
        int  b= nextInt();
        int  c= nextInt();
        int k = nextInt();
        int sum = 0;

        if(k<=a){
            print(k);
            System.exit(0);
        }
        if(k<=a+b){
            print(a);
            System.exit(0);
        }
        else{
            print(a-(k-a-b));
            System.exit(0);
        }
    }

    public static int nextInt(){
        return Integer.parseInt(scan.next());
    }
    public static long nextLong(){
        return Long.parseLong(scan.next());
    }
    public static String next(){
        return scan.next();
    }
    public static double nextDouble(){
        return Double.parseDouble(scan.next());
    }
    public static float nextFloat(){
        return Float.parseFloat(scan.next());
    }

    //Yes or No
    public static void yesNo(boolean flag){
        if(flag) System.out.println("Yes");
        else System.out.println("No");
    }
    public static void print(Object a){
        System.out.println(a);
    }
} public class Main{
    public static Scanner scan  = new Scanner(System.in);
    public static void main(String[]args){
        int a = nextInt();
        int  b= nextInt();
        int  c= nextInt();
        int k = nextInt();
        int sum = 0;

        if(k<=a){
            print(k);
            System.exit(0);
        }
        if(k<=a+b){
            print(a);
            System.exit(0);
        }
        else{
            print(a-(k-a-b));
            System.exit(0);
        }
    }

    public static int nextInt(){
        return Integer.parseInt(scan.next());
    }
    public static long nextLong(){
        return Long.parseLong(scan.next());
    }
    public static String next(){
        return scan.next();
    }
    public static double nextDouble(){
        return Double.parseDouble(scan.next());
    }
    public static float nextFloat(){
        return Float.parseFloat(scan.next());
    }

    //Yes or No
    public static void yesNo(boolean flag){
        if(flag) System.out.println("Yes");
        else System.out.println("No");
    }
    public static void print(Object a){
        System.out.println(a);
    }
} Main
import java.util.Scanner;


public class Main{
    public static Scanner scan  = new Scanner(System.in);
    public static void main(String[]args){
        int a = nextInt();
        int  b= nextInt();
        int  c= nextInt();
        int k = nextInt();
        int sum = 0;

        if(k<=a){
            print(k);
            System.exit(0);
        }
        if(k<=a+b){
            print(a);
            System.exit(0);
        }
        else{
            print(a-(k-a-b));
            System.exit(0);
        }
    }

    public static int nextInt(){
        return Integer.parseInt(scan.next());
    }
    public static long nextLong(){
        return Long.parseLong(scan.next());
    }
    public static String next(){
        return scan.next();
    }
    public static double nextDouble(){
        return Double.parseDouble(scan.next());
    }
    public static float nextFloat(){
        return Float.parseFloat(scan.next());
    }

    //Yes or No
    public static void yesNo(boolean flag){
        if(flag) System.out.println("Yes");
        else System.out.println("No");
    }
    public static void print(Object a){
        System.out.println(a);
    }
}