import java.util.*;
public class Main{
    public static void main(String args[]){
        Scanner sn = new Scanner(System.in);
        int A = sn.nextInt();
        int B = sn.nextInt();
        int C = sn.nextInt();
        int K = sn.nextInt();
        int flag = 1;
        int ans = 0;

        for(int i=0; i<A; i++){
            if(K == 0) {
                break;
            }
            ans = ans + 1;
            K = K - 1;
        }

        for(int i=0; i<B; i++){
            if(K == 0) {
                break;
            }
            K = K - 1;
        }

        for(int i=0; i<C; i++){
            if(K == 0) {
                break;
            }
            ans = ans - 1;
            K = K - 1;
        }

        System.out.println(ans);

    }
} import java.util.*; util.* java public class Main{
    public static void main(String args[]){
        Scanner sn = new Scanner(System.in);
        int A = sn.nextInt();
        int B = sn.nextInt();
        int C = sn.nextInt();
        int K = sn.nextInt();
        int flag = 1;
        int ans = 0;

        for(int i=0; i<A; i++){
            if(K == 0) {
                break;
            }
            ans = ans + 1;
            K = K - 1;
        }

        for(int i=0; i<B; i++){
            if(K == 0) {
                break;
            }
            K = K - 1;
        }

        for(int i=0; i<C; i++){
            if(K == 0) {
                break;
            }
            ans = ans - 1;
            K = K - 1;
        }

        System.out.println(ans);

    }
} Main public static void main(String args[]){
        Scanner sn = new Scanner(System.in);
        int A = sn.nextInt();
        int B = sn.nextInt();
        int C = sn.nextInt();
        int K = sn.nextInt();
        int flag = 1;
        int ans = 0;

        for(int i=0; i<A; i++){
            if(K == 0) {
                break;
            }
            ans = ans + 1;
            K = K - 1;
        }

        for(int i=0; i<B; i++){
            if(K == 0) {
                break;
            }
            K = K - 1;
        }

        for(int i=0; i<C; i++){
            if(K == 0) {
                break;
            }
            ans = ans - 1;
            K = K - 1;
        }

        System.out.println(ans);

    } main {
        Scanner sn = new Scanner(System.in);
        int A = sn.nextInt();
        int B = sn.nextInt();
        int C = sn.nextInt();
        int K = sn.nextInt();
        int flag = 1;
        int ans = 0;

        for(int i=0; i<A; i++){
            if(K == 0) {
                break;
            }
            ans = ans + 1;
            K = K - 1;
        }

        for(int i=0; i<B; i++){
            if(K == 0) {
                break;
            }
            K = K - 1;
        }

        for(int i=0; i<C; i++){
            if(K == 0) {
                break;
            }
            ans = ans - 1;
            K = K - 1;
        }

        System.out.println(ans);

    } Scanner sn = new Scanner(System.in); sn new Scanner(System.in) int A = sn.nextInt(); A sn.nextInt() sn.nextInt sn int B = sn.nextInt(); B sn.nextInt() sn.nextInt sn int C = sn.nextInt(); C sn.nextInt() sn.nextInt sn int K = sn.nextInt(); K sn.nextInt() sn.nextInt sn int flag = 1; flag 1 int ans = 0; ans 0 for(int i=0; i<A; i++){
            if(K == 0) {
                break;
            }
            ans = ans + 1;
            K = K - 1;
        } int i=0; int i=0; i 0 i<A i A i++ i++ i {
            if(K == 0) {
                break;
            }
            ans = ans + 1;
            K = K - 1;
        } {
            if(K == 0) {
                break;
            }
            ans = ans + 1;
            K = K - 1;
        } if(K == 0) {
                break;
            } K == 0 K 0 {
                break;
            } break; ans = ans + 1 ans ans + 1 ans 1 K = K - 1 K K - 1 K 1 for(int i=0; i<B; i++){
            if(K == 0) {
                break;
            }
            K = K - 1;
        } int i=0; int i=0; i 0 i<B i B i++ i++ i {
            if(K == 0) {
                break;
            }
            K = K - 1;
        } {
            if(K == 0) {
                break;
            }
            K = K - 1;
        } if(K == 0) {
                break;
            } K == 0 K 0 {
                break;
            } break; K = K - 1 K K - 1 K 1 for(int i=0; i<C; i++){
            if(K == 0) {
                break;
            }
            ans = ans - 1;
            K = K - 1;
        } int i=0; int i=0; i 0 i<C i C i++ i++ i {
            if(K == 0) {
                break;
            }
            ans = ans - 1;
            K = K - 1;
        } {
            if(K == 0) {
                break;
            }
            ans = ans - 1;
            K = K - 1;
        } if(K == 0) {
                break;
            } K == 0 K 0 {
                break;
            } break; ans = ans - 1 ans ans - 1 ans 1 K = K - 1 K K - 1 K 1 System.out.println(ans) System.out.println System.out System System.out ans String args[] args public class Main{
    public static void main(String args[]){
        Scanner sn = new Scanner(System.in);
        int A = sn.nextInt();
        int B = sn.nextInt();
        int C = sn.nextInt();
        int K = sn.nextInt();
        int flag = 1;
        int ans = 0;

        for(int i=0; i<A; i++){
            if(K == 0) {
                break;
            }
            ans = ans + 1;
            K = K - 1;
        }

        for(int i=0; i<B; i++){
            if(K == 0) {
                break;
            }
            K = K - 1;
        }

        for(int i=0; i<C; i++){
            if(K == 0) {
                break;
            }
            ans = ans - 1;
            K = K - 1;
        }

        System.out.println(ans);

    }
} public class Main{
    public static void main(String args[]){
        Scanner sn = new Scanner(System.in);
        int A = sn.nextInt();
        int B = sn.nextInt();
        int C = sn.nextInt();
        int K = sn.nextInt();
        int flag = 1;
        int ans = 0;

        for(int i=0; i<A; i++){
            if(K == 0) {
                break;
            }
            ans = ans + 1;
            K = K - 1;
        }

        for(int i=0; i<B; i++){
            if(K == 0) {
                break;
            }
            K = K - 1;
        }

        for(int i=0; i<C; i++){
            if(K == 0) {
                break;
            }
            ans = ans - 1;
            K = K - 1;
        }

        System.out.println(ans);

    }
} Main
import java.util.*;
public class Main{
    public static void main(String args[]){
        Scanner sn = new Scanner(System.in);
        int A = sn.nextInt();
        int B = sn.nextInt();
        int C = sn.nextInt();
        int K = sn.nextInt();
        int flag = 1;
        int ans = 0;

        for(int i=0; i<A; i++){
            if(K == 0) {
                break;
            }
            ans = ans + 1;
            K = K - 1;
        }

        for(int i=0; i<B; i++){
            if(K == 0) {
                break;
            }
            K = K - 1;
        }

        for(int i=0; i<C; i++){
            if(K == 0) {
                break;
            }
            ans = ans - 1;
            K = K - 1;
        }

        System.out.println(ans);

    }
}