IDENTIFICATION DIVISION.
PROGRAM-ID. 162D.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 N          PIC  9(4).
01 S          PIC  X(4000).
01 I1         PIC  9(4) COMP.
01 I2         PIC  9(4) COMP.
01 I3         PIC  9(4) COMP.
01 IR         PIC  9(4) COMP.
01 IG         PIC  9(4) COMP.
01 IB         PIC  9(4) COMP.
01 KS         PIC  9(9) COMP.
01 S1         PIC  9(4) COMP.
01 S2         PIC  9(4) COMP.
01 S3         PIC  9(4) COMP.
01 OUT        PIC  Z(8)9.
01 TBL.
   03 RN      PIC  9(4) COMP OCCURS 4000.
   03 GN      PIC  9(4) COMP OCCURS 4000.
   03 BN      PIC  9(4) COMP OCCURS 4000.
*>
PROCEDURE DIVISION.
  ACCEPT N.
  ACCEPT S.
*>
  PERFORM VARYING I1 FROM 1 BY 1 UNTIL I1 > 4000
    MOVE 0 TO RN(I1) GN(I1) BN(I1) 
  END-PERFORM.
*>
  MOVE 0 TO IR IG IB.
  PERFORM VARYING I1 FROM 1 BY 1 UNTIL I1 > 4000
    EVALUATE S(I1:1)
      WHEN 'R'
        ADD   1 TO IR
        MOVE I1 TO RN(IR)
      WHEN 'G'
        ADD   1 TO IG
        MOVE I1 TO GN(IG)
      WHEN 'B'
        ADD   1 TO IB
        MOVE I1 TO BN(IB)
  END-PERFORM.
*>
  MOVE 0 TO KS.
  PERFORM VARYING I1 FROM 1 BY 1 UNTIL I1 > IR
    PERFORM VARYING I2 FROM 1 BY 1 UNTIL I2 > IG
      PERFORM VARYING I3 FROM 1 BY 1 UNTIL I3 > IB
        COMPUTE S1 = RN(I1) - GN(I2)
        COMPUTE S2 = GN(I2) - BN(I3)
        COMPUTE S3 = RN(I1) - BN(I3)
        IF (S1 = S2) OR (S1 = S3) OR (S2 = S3)
          CONTINUE
        ELSE
          ADD 1 TO KS
        END-IF
      END-PERFORM
    END-PERFORM
  END-PERFORM.

*>
  MOVE KS TO OUT.
  DISPLAY FUNCTION TRIM(OUT).
  STOP RUN.
