PROGRAM-ID.                      ABC_105_D.
ENVIRONMENT                      DIVISION.
INPUT-OUTPUT                     SECTION.
FILE-CONTROL.
    SELECT SYSIN ASSIGN TO KEYBOARD ORGANIZATION LINE SEQUENTIAL.

DATA                             DIVISION.
FILE                             SECTION.
    FD  SYSIN.
        01 INDATA PIC X(1100000).

WORKING-STORAGE                  SECTION.
    01 NM      PIC X(17).
    01 N       PIC 9(6).
    01 M       PIC 9(10).
    01 INP     PIC X(1100000).

    01 X1.
        03 XNUM PIC 9(6) VALUE ZERO.
        03 X11 OCCURS 100000 DEPENDING XNUM ASCENDING X INDEXED BY K.
            05 X PIC 9(10).
            05 XCNT PIC 9(6) VALUE ZERO.

    01 flg     PIC 9(1).
    01 ky      PIC 9(10).

    01 tmp     PIC 9(10).
    01 r       PIC 9(10).
    01 sm      PIC 9(18).

    01 maxlen  PIC 9(7).
    01 cur     PIC 9(7) VALUE 1.
    01 i       PIC 9(7) VALUE 1.
    01 j       PIC 9(7).
    01 len     PIC 9(7).

    01 ans     PIC X(18).

    01 ZS      PIC Z(18)9.
    01 DUMMY   PIC X(1).

PROCEDURE                        DIVISION.
    ACCEPT NM.

    UNSTRING NM
    DELIMITED BY SPACE
    INTO N M.

    OPEN INPUT SYSIN.

    READ SYSIN INTO INP.

    CLOSE SYSIN.

*>    DISPLAY FUNCTION STORED-CHAR-LENGTH(INP)

    MOVE N TO maxlen.

    MOVE ZERO TO sm.

    PERFORM maxlen TIMES

        PERFORM VARYING j FROM cur BY 1
            UNTIL INP(j:1) = SPACE
        END-PERFORM

        COMPUTE len = j - cur

        MOVE INP(cur:len) TO tmp

        ADD tmp TO sm

        COMPUTE ky = FUNCTION MOD(sm, M)

        SORT X11 ASCENDING X

        MOVE ZERO TO flg

        SEARCH ALL X11
            AT END
                CONTINUE
            WHEN X(K) = ky
                MOVE 1 TO flg
                CONTINUE
        END-SEARCH

        IF flg = 1 THEN
            ADD 1 TO XCNT(K)
        ELSE
            ADD 1 TO XNUM
            MOVE ky TO X(XNUM)
            ADD 1 TO XCNT(XNUM)
        END-IF

        COMPUTE cur = j + 1

        ADD 1 TO i

    END-PERFORM.

    SORT X11 ASCENDING X.

    MOVE ZERO TO sm.

    PERFORM VARYING i FROM 1 BY 1 UNTIL XNUM < i
*> pair
        COMPUTE sm = sm + XCNT(i) * (XCNT(i) - 1) / 2
        IF ZERO = FUNCTION MOD(X(i), M)
*> alone
            COMPUTE sm = sm + XCNT(i)
        END-IF
    END-PERFORM.

    MOVE sm TO ZS.

    PERFORM UNANS.

    DISPLAY ans(1:FUNCTION STORED-CHAR-LENGTH(ans)).
    STOP RUN.

UNANS                            SECTION.
    UNSTRING
        ZS DELIMITED BY ALL SPACE
        INTO DUMMY ans
    END-UNSTRING.
