IDENTIFICATION DIVISION.
PROGRAM-ID. 163B.
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
  SELECT STDIO ASSIGN TO KEYBOARD. 
*>
DATA DIVISION.
FILE SECTION.
FD STDIO.
01 INP        PIC X(60000).
*>
WORKING-STORAGE SECTION.
01 INW        PIC X(13).
01 N          PIC 9(7) COMP.
01 M          PIC 9(5) COMP.
01 V          PIC 9(9) COMP.
01 W          PIC 9(5) COMP.
01 I          PIC 9(5) COMP.
01 P1         PIC 9(5) COMP.
01 P2         PIC 9(5) COMP.
01 OUT        PIC -(7)9.
*>
PROCEDURE DIVISION.
  ACCEPT INW.
  UNSTRING INW DELIMITED BY ' '
      INTO N M.
*>
  OPEN INPUT STDIO.
  READ STDIO.
  CLOSE STDIO.
*>
  MOVE 0 TO V.
  MOVE 1 TO P1.
  PERFORM VARYING I FROM 1 BY 1 UNTIL I > M
    PERFORM VARYING P2 FROM P1 BY 1 UNTIL INP(P2:1) = ' '
    END-PERFORM
    MOVE INP(P1:P2 - P1) TO W
    ADD W TO V
    COMPUTE P1 = P2 + 1
  END-PERFORM.
*>
  IF (N >= V)
    COMPUTE OUT = N - V
  ELSE
    MOVE -1 TO OUT
  END-IF.
*>
  DISPLAY FUNCTION TRIM(OUT).
  STOP RUN.
