IDENTIFICATION DIVISION.
PROGRAM-ID. 167B.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 INP        PIC X(144).
01 A          PIC 9(10).
01 B          PIC 9(10).
01 C          PIC 9(10).
01 K          PIC 9(10).
01 CNT        PIC S9(10).
01 OUT        PIC -(10)9.
*>
PROCEDURE DIVISION.
  ACCEPT INP.
  UNSTRING INP DELIMITED BY ' '
      INTO A B C K.
*>
  IF (A >= K)
    MOVE K TO A
    MOVE 0 TO C
  ELSE
    IF (A + B >= K)
      MOVE 0 TO C
    ELSE
      COMPUTE C = K - A - B
    END-IF
  END-IF.
*>
  COMPUTE CNT = A - C.
*>
  MOVE CNT TO OUT.
  DISPLAY FUNCTION TRIM(OUT).
  STOP RUN.
