import java.util.Scanner;

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

        int ans = 0;
        int numa = 0;
        int numb = 0;
        for(int i=0; i<k; i++) {
            if(i < a) {
                ans += 1;
            } else {
                break;
            }
            numa++;
        }
        if(numa == k) printAns(ans);

        for(int j=0; j<k-numa; j++) {
            if(j < b) {
                ;
            } else {
                break;
            }
            numb++;
        }
        if(numa+numb == k) printAns(ans);

        for(int l=0; l<k-(numa+numb); l++) {
            if(l < c) {
                ans += -1;
            }
        }
        printAns(ans);
    }

    public static void printAns(int ans) {
        System.out.println(ans);
        System.exit(0);
    }
} import java.util.Scanner; Scanner java.util public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int a = sc.nextInt();
        int b = sc.nextInt();
        int c = sc.nextInt();
        int k = sc.nextInt();

        int ans = 0;
        int numa = 0;
        int numb = 0;
        for(int i=0; i<k; i++) {
            if(i < a) {
                ans += 1;
            } else {
                break;
            }
            numa++;
        }
        if(numa == k) printAns(ans);

        for(int j=0; j<k-numa; j++) {
            if(j < b) {
                ;
            } else {
                break;
            }
            numb++;
        }
        if(numa+numb == k) printAns(ans);

        for(int l=0; l<k-(numa+numb); l++) {
            if(l < c) {
                ans += -1;
            }
        }
        printAns(ans);
    }

    public static void printAns(int ans) {
        System.out.println(ans);
        System.exit(0);
    }
} Main public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int a = sc.nextInt();
        int b = sc.nextInt();
        int c = sc.nextInt();
        int k = sc.nextInt();

        int ans = 0;
        int numa = 0;
        int numb = 0;
        for(int i=0; i<k; i++) {
            if(i < a) {
                ans += 1;
            } else {
                break;
            }
            numa++;
        }
        if(numa == k) printAns(ans);

        for(int j=0; j<k-numa; j++) {
            if(j < b) {
                ;
            } else {
                break;
            }
            numb++;
        }
        if(numa+numb == k) printAns(ans);

        for(int l=0; l<k-(numa+numb); l++) {
            if(l < c) {
                ans += -1;
            }
        }
        printAns(ans);
    } main {
        Scanner sc = new Scanner(System.in);
        int a = sc.nextInt();
        int b = sc.nextInt();
        int c = sc.nextInt();
        int k = sc.nextInt();

        int ans = 0;
        int numa = 0;
        int numb = 0;
        for(int i=0; i<k; i++) {
            if(i < a) {
                ans += 1;
            } else {
                break;
            }
            numa++;
        }
        if(numa == k) printAns(ans);

        for(int j=0; j<k-numa; j++) {
            if(j < b) {
                ;
            } else {
                break;
            }
            numb++;
        }
        if(numa+numb == k) printAns(ans);

        for(int l=0; l<k-(numa+numb); l++) {
            if(l < c) {
                ans += -1;
            }
        }
        printAns(ans);
    } Scanner sc = new Scanner(System.in); sc new Scanner(System.in) int a = sc.nextInt(); a sc.nextInt() sc.nextInt sc int b = sc.nextInt(); b sc.nextInt() sc.nextInt sc int c = sc.nextInt(); c sc.nextInt() sc.nextInt sc int k = sc.nextInt(); k sc.nextInt() sc.nextInt sc int ans = 0; ans 0 int numa = 0; numa 0 int numb = 0; numb 0 for(int i=0; i<k; i++) {
            if(i < a) {
                ans += 1;
            } else {
                break;
            }
            numa++;
        } int i=0; int i=0; i 0 i<k i k i++ i++ i {
            if(i < a) {
                ans += 1;
            } else {
                break;
            }
            numa++;
        } {
            if(i < a) {
                ans += 1;
            } else {
                break;
            }
            numa++;
        } if(i < a) {
                ans += 1;
            } else {
                break;
            } i < a i a {
                ans += 1;
            } ans += 1 ans 1 {
                break;
            } break; numa++ numa if(numa == k) printAns(ans); numa == k numa k printAns(ans) printAns ans for(int j=0; j<k-numa; j++) {
            if(j < b) {
                ;
            } else {
                break;
            }
            numb++;
        } int j=0; int j=0; j 0 j<k-numa j k-numa k numa j++ j++ j {
            if(j < b) {
                ;
            } else {
                break;
            }
            numb++;
        } {
            if(j < b) {
                ;
            } else {
                break;
            }
            numb++;
        } if(j < b) {
                ;
            } else {
                break;
            } j < b j b {
                ;
            } {
                break;
            } break; numb++ numb if(numa+numb == k) printAns(ans); numa+numb == k numa+numb numa numb k printAns(ans) printAns ans for(int l=0; l<k-(numa+numb); l++) {
            if(l < c) {
                ans += -1;
            }
        } int l=0; int l=0; l 0 l<k-(numa+numb) l k-(numa+numb) k (numa+numb) numa numb l++ l++ l {
            if(l < c) {
                ans += -1;
            }
        } {
            if(l < c) {
                ans += -1;
            }
        } if(l < c) {
                ans += -1;
            } l < c l c {
                ans += -1;
            } ans += -1 ans -1 1 printAns(ans) printAns ans String[] args args public static void printAns(int ans) {
        System.out.println(ans);
        System.exit(0);
    } printAns {
        System.out.println(ans);
        System.exit(0);
    } System.out.println(ans) System.out.println System.out System System.out ans System.exit(0) System.exit System 0 int ans ans public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int a = sc.nextInt();
        int b = sc.nextInt();
        int c = sc.nextInt();
        int k = sc.nextInt();

        int ans = 0;
        int numa = 0;
        int numb = 0;
        for(int i=0; i<k; i++) {
            if(i < a) {
                ans += 1;
            } else {
                break;
            }
            numa++;
        }
        if(numa == k) printAns(ans);

        for(int j=0; j<k-numa; j++) {
            if(j < b) {
                ;
            } else {
                break;
            }
            numb++;
        }
        if(numa+numb == k) printAns(ans);

        for(int l=0; l<k-(numa+numb); l++) {
            if(l < c) {
                ans += -1;
            }
        }
        printAns(ans);
    }

    public static void printAns(int ans) {
        System.out.println(ans);
        System.exit(0);
    }
} public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int a = sc.nextInt();
        int b = sc.nextInt();
        int c = sc.nextInt();
        int k = sc.nextInt();

        int ans = 0;
        int numa = 0;
        int numb = 0;
        for(int i=0; i<k; i++) {
            if(i < a) {
                ans += 1;
            } else {
                break;
            }
            numa++;
        }
        if(numa == k) printAns(ans);

        for(int j=0; j<k-numa; j++) {
            if(j < b) {
                ;
            } else {
                break;
            }
            numb++;
        }
        if(numa+numb == k) printAns(ans);

        for(int l=0; l<k-(numa+numb); l++) {
            if(l < c) {
                ans += -1;
            }
        }
        printAns(ans);
    }

    public static void printAns(int ans) {
        System.out.println(ans);
        System.exit(0);
    }
} Main
import java.util.Scanner;

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

        int ans = 0;
        int numa = 0;
        int numb = 0;
        for(int i=0; i<k; i++) {
            if(i < a) {
                ans += 1;
            } else {
                break;
            }
            numa++;
        }
        if(numa == k) printAns(ans);

        for(int j=0; j<k-numa; j++) {
            if(j < b) {
                ;
            } else {
                break;
            }
            numb++;
        }
        if(numa+numb == k) printAns(ans);

        for(int l=0; l<k-(numa+numb); l++) {
            if(l < c) {
                ans += -1;
            }
        }
        printAns(ans);
    }

    public static void printAns(int ans) {
        System.out.println(ans);
        System.exit(0);
    }
}
