Turning Red
Time limit3sMemory limit1024 MB
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.
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 and . Here is the number of lights, with , and is the number of buttons, with .
The second line is a string of characters, each one R, G, or B. The th character is the initial color of the th light.
Each of the next lines describes a button. A line starts with an integer (), the number of lights controlled by the button, followed by distinct integers, the indices of those lights. Lights are numbered from 1 to . 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.