Under construction

Still being built. Things may change

Turning Red

Time limit3sMemory limit1024 MB

Summary
Given lights cycling through R, G, B and buttons that each advance a set of lights, find the fewest total button presses that turn every light red, or report impossible.
Level

Medium6 of 10

Topics
Graph, BFS, Math
Solved
No attempts yet

Problem

Mei's parents have spent the last year remodeling their house, but their lighting system is quite complex! Each room in the house has an LED light, which can be set to red, green, or blue, as shown in Figure P.1.

Figure P.1: The initial state of the lights in Sample Input 1. Buttons and wires not shown.

Throughout the house are various buttons, and each button is connected to one or more lights. When a button is pressed, every red light connected to that button becomes green, every green light connected to it becomes blue, and every blue light connected to it becomes red. Each button can be pressed multiple times. The house was built before crossbar wiring was invented, so each light is controlled by at most two buttons.

Mei wants to turn all of the lights red. Her parents worry that the buttons will wear out, so they asked her to minimize the total number of button presses.

Input

The first line contains two positive integers ll and bb. Here ll is the number of lights, with 1≤l≤2⋅1051 \le l \le 2 \cdot 10^5, and bb is the number of buttons, with 0≤b≤2⋅l0 \le b \le 2 \cdot l.

The second line is a string of ll characters, each one R, G, or B. The iith character is the initial color of the iith light.

Each of the next bb lines describes a button. A line starts with an integer kk (1≤k≤l1 \le k \le l), the number of lights controlled by the button, followed by kk distinct integers, the indices of those lights. Lights are numbered from 1 to ll. Each light appears at most twice across all buttons.

Output

Output the minimum number of button presses Mei needs to turn all the lights red. If it is impossible, output impossible.

Examples4

  1. Example 1

    Input
    8 6
    GBRBRRRG
    2 1 4
    1 2
    4 4 5 6 7
    3 5 6 7
    1 8
    1 8
    
    Expected output
    8
    
  2. Example 2

    Input
    4 3
    RGBR
    2 1 2
    2 2 3
    2 3 4
    
    Expected output
    impossible
    
  3. Example 3

    Input
    4 4
    GBRG
    2 1 2
    2 2 3
    2 3 4
    1 4
    
    Expected output
    6
    
  4. Example 4

    Input
    3 3
    RGB
    1 1
    1 2
    1 3
    
    Expected output
    3