import java.util.*;

public class Main {
    public static void main(String[] args) {
      Scanner s = new Scanner(System.in);
        final long A = s.nextLong();
        final long B = s.nextLong();
        final long C = s.nextLong();
        long K = s.nextLong();

        long calcVal = 0 ;
        if (K - A > 0) {
            calcVal = A;
            K -= A;

        } else {
            calcVal = K;
            System.out.println(calcVal);
            return;
        }

        if ( K - B > 0) {
            K -= B;
        } else {
            System.out.println(calcVal);
            return;
        }

        if (K - C > 1) {
            calcVal += C * -1;
        } else {
            calcVal += K * -1;
        }
        System.out.println(calcVal);
    }
} import java.util.*; util.* java public class Main {
    public static void main(String[] args) {
      Scanner s = new Scanner(System.in);
        final long A = s.nextLong();
        final long B = s.nextLong();
        final long C = s.nextLong();
        long K = s.nextLong();

        long calcVal = 0 ;
        if (K - A > 0) {
            calcVal = A;
            K -= A;

        } else {
            calcVal = K;
            System.out.println(calcVal);
            return;
        }

        if ( K - B > 0) {
            K -= B;
        } else {
            System.out.println(calcVal);
            return;
        }

        if (K - C > 1) {
            calcVal += C * -1;
        } else {
            calcVal += K * -1;
        }
        System.out.println(calcVal);
    }
} Main public static void main(String[] args) {
      Scanner s = new Scanner(System.in);
        final long A = s.nextLong();
        final long B = s.nextLong();
        final long C = s.nextLong();
        long K = s.nextLong();

        long calcVal = 0 ;
        if (K - A > 0) {
            calcVal = A;
            K -= A;

        } else {
            calcVal = K;
            System.out.println(calcVal);
            return;
        }

        if ( K - B > 0) {
            K -= B;
        } else {
            System.out.println(calcVal);
            return;
        }

        if (K - C > 1) {
            calcVal += C * -1;
        } else {
            calcVal += K * -1;
        }
        System.out.println(calcVal);
    } main {
      Scanner s = new Scanner(System.in);
        final long A = s.nextLong();
        final long B = s.nextLong();
        final long C = s.nextLong();
        long K = s.nextLong();

        long calcVal = 0 ;
        if (K - A > 0) {
            calcVal = A;
            K -= A;

        } else {
            calcVal = K;
            System.out.println(calcVal);
            return;
        }

        if ( K - B > 0) {
            K -= B;
        } else {
            System.out.println(calcVal);
            return;
        }

        if (K - C > 1) {
            calcVal += C * -1;
        } else {
            calcVal += K * -1;
        }
        System.out.println(calcVal);
    } Scanner s = new Scanner(System.in); s new Scanner(System.in) final long A = s.nextLong(); A s.nextLong() s.nextLong s final long B = s.nextLong(); B s.nextLong() s.nextLong s final long C = s.nextLong(); C s.nextLong() s.nextLong s long K = s.nextLong(); K s.nextLong() s.nextLong s long calcVal = 0 ; calcVal 0 if (K - A > 0) {
            calcVal = A;
            K -= A;

        } else {
            calcVal = K;
            System.out.println(calcVal);
            return;
        } K - A > 0 K - A K A 0 {
            calcVal = A;
            K -= A;

        } calcVal = A calcVal A K -= A K A {
            calcVal = K;
            System.out.println(calcVal);
            return;
        } calcVal = K calcVal K System.out.println(calcVal) System.out.println System.out System System.out calcVal return; if ( K - B > 0) {
            K -= B;
        } else {
            System.out.println(calcVal);
            return;
        } K - B > 0 K - B K B 0 {
            K -= B;
        } K -= B K B {
            System.out.println(calcVal);
            return;
        } System.out.println(calcVal) System.out.println System.out System System.out calcVal return; if (K - C > 1) {
            calcVal += C * -1;
        } else {
            calcVal += K * -1;
        } K - C > 1 K - C K C 1 {
            calcVal += C * -1;
        } calcVal += C * -1 calcVal C * -1 C -1 1 {
            calcVal += K * -1;
        } calcVal += K * -1 calcVal K * -1 K -1 1 System.out.println(calcVal) System.out.println System.out System System.out calcVal String[] args args public class Main {
    public static void main(String[] args) {
      Scanner s = new Scanner(System.in);
        final long A = s.nextLong();
        final long B = s.nextLong();
        final long C = s.nextLong();
        long K = s.nextLong();

        long calcVal = 0 ;
        if (K - A > 0) {
            calcVal = A;
            K -= A;

        } else {
            calcVal = K;
            System.out.println(calcVal);
            return;
        }

        if ( K - B > 0) {
            K -= B;
        } else {
            System.out.println(calcVal);
            return;
        }

        if (K - C > 1) {
            calcVal += C * -1;
        } else {
            calcVal += K * -1;
        }
        System.out.println(calcVal);
    }
} public class Main {
    public static void main(String[] args) {
      Scanner s = new Scanner(System.in);
        final long A = s.nextLong();
        final long B = s.nextLong();
        final long C = s.nextLong();
        long K = s.nextLong();

        long calcVal = 0 ;
        if (K - A > 0) {
            calcVal = A;
            K -= A;

        } else {
            calcVal = K;
            System.out.println(calcVal);
            return;
        }

        if ( K - B > 0) {
            K -= B;
        } else {
            System.out.println(calcVal);
            return;
        }

        if (K - C > 1) {
            calcVal += C * -1;
        } else {
            calcVal += K * -1;
        }
        System.out.println(calcVal);
    }
} Main
import java.util.*;

public class Main {
    public static void main(String[] args) {
      Scanner s = new Scanner(System.in);
        final long A = s.nextLong();
        final long B = s.nextLong();
        final long C = s.nextLong();
        long K = s.nextLong();

        long calcVal = 0 ;
        if (K - A > 0) {
            calcVal = A;
            K -= A;

        } else {
            calcVal = K;
            System.out.println(calcVal);
            return;
        }

        if ( K - B > 0) {
            K -= B;
        } else {
            System.out.println(calcVal);
            return;
        }

        if (K - C > 1) {
            calcVal += C * -1;
        } else {
            calcVal += K * -1;
        }
        System.out.println(calcVal);
    }
}