IDENTIFICATION DIVISION.
PROGRAM-ID. 124C.
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
  SELECT STDIO ASSIGN TO KEYBOARD. 
*>
DATA DIVISION.
FILE SECTION.
FD STDIO.
01 INP   PIC X(200000).
*>
WORKING-STORAGE SECTION.
01 S     PIC X(100001).
01 CNT.
   03 C  PIC 9(5) OCCURS 2.
01 F     PIC 9(1).
01 A     PIC 9(1).
01 H     PIC 9(5).
01 K1    PIC 9(5).
01 K2    PIC 9(5).
01 I     PIC 9(6).
01 OUT   PIC ZZZZ9.
*>
PROCEDURE DIVISION.
*>ACCEPT S FROM SYSIN.
  OPEN INPUT STDIO.
  READ STDIO INTO S.
  CLOSE STDIO.
*>
  MOVE 0 TO C(1) C(2).
  MOVE 1 TO F.
  PERFORM VARYING I FROM 1 BY 1 UNTIL S(I:1) = ' '
    IF (S(I:1) = '1') 
      COMPUTE C(F) = C(F) + 1
    END-IF     
    COMPUTE F = 3 - F
  END-PERFORM.
*>
  SUBTRACT 1 FROM I
  DIVIDE I BY 2 GIVING H REMAINDER A.
  COMPUTE K1 = H + A - C(1) + C(2)
  COMPUTE K2 = H     - C(2) + C(1)    
*>
  IF (K1 < K2)
    MOVE K1 TO OUT
  ELSE
    MOVE K2 TO OUT
  END-IF.
*>
  DISPLAY FUNCTION TRIM(OUT).
  STOP RUN.
