IDENTIFICATION DIVISION.
PROGRAM-ID. 170C.
DATA DIVISION.
*>
WORKING-STORAGE SECTION.
01 INP        PIC X(400).
01 X          PIC 9(03) COMP.
01 N          PIC 9(03) COMP.
01 P1         PIC 9(04) COMP.
01 P2         PIC 9(04) COMP.
01 TBL1.
   03 P       PIC 9(03) OCCURS 102.
01 I          PIC 9(03) COMP.
01 IDX        PIC 9(03) COMP.
01 CNT        PIC 9(03) COMP.
01 OUT        PIC ZZ9.
*>
PROCEDURE DIVISION.
  ACCEPT INP.
  UNSTRING INP DELIMITED BY ' '
      INTO X N.
*>
  ACCEPT INP.
  MOVE ZERO TO TBL1.
  MOVE 1    TO P1.
  PERFORM VARYING I FROM 1 BY 1 UNTIL I > N
    PERFORM VARYING P2 FROM P1 BY 1 UNTIL INP(P2:1) = ' '
    END-PERFORM
    MOVE INP(P1:P2 - P1) TO IDX
    MOVE 1               TO P(IDX + 1)
    COMPUTE P1 = P2 + 1
  END-PERFORM.
*>
  IF (N = 0) OR P(X + 1) = 0
    MOVE X TO CNT    
  ELSE      
    MOVE 1 TO IDX 
    PERFORM VARYING I FROM 1 BY 1
              UNTIL P(X - I + 1) = 0 OR P(X + I + 1) = 0
    END-PERFORM  
    IF (P(X- I + 1) = 0)
      COMPUTE CNT = X - I 
    ELSE
      COMPUTE CNT = X + I
    END-IF
  END-IF.
*>
  MOVE CNT TO OUT.
  DISPLAY FUNCTION TRIM(OUT)
  STOP RUN.
