IDENTIFICATION DIVISION.
PROGRAM-ID. 161C.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 INP        PIC X(41).
01 NX         PIC X(19).
01 KX         PIC X(19).
01 N          PIC 9(18) COMP.
01 K          PIC 9(18) COMP.
01 S          PIC 9(18) COMP.
01 A          PIC 9(18) COMP.
01 ANS        PIC Z(17)9.
*>
PROCEDURE DIVISION.
  ACCEPT INP.
  UNSTRING INP DELIMITED BY ' '
      INTO NX KX.
*>
    EVALUATE TRUE
      WHEN NX = '1000000000000000000' AND
           KX = '1000000000000000000'
        MOVE 1 TO N
        MOVE 1 TO N
      WHEN NX = '1000000000000000000'
        MOVE KX TO K
        COMPUTE N = 999999999999999999 - K + 1
      WHEN KX = '1000000000000000000'
        MOVE NX TO N
        COMPUTE K = 999999999999999999 - N + 1
      WHEN OTHER
        MOVE NX TO N
        MOVE KX TO K
    END-EVALUATE.
 
    DIVIDE N BY K GIVING S REMAINDER A
    EVALUATE TRUE
      WHEN A = 0
        MOVE 0 TO ANS
      WHEN A <= K / 2
        MOVE A TO ANS
      WHEN OTHER
        COMPUTE ANS = K - A
    END-EVALUATE.
*>
  DISPLAY FUNCTION TRIM(ANS).
  STOP RUN.
