TV Switches

Time limit1sMemory limit128 MB

Problem

Dave owns an old television whose mechanical switches no longer work as they did when the television was new.

Originally, pressing a released switch would press that switch and release all the other switches, so exactly one switch would remain pressed. Now, pressing a currently released switch still presses that switch, but it releases only some specified switches; every other switch keeps its current position.

For each switch, Dave knows which switches are released when it is pressed. Given the initial switch positions, determine the minimum number of switch presses needed so that, in the end, only switch 3 is pressed and every other switch is released.

Input

The first line contains an integer N (3 <= N <= 20), the number of TV switches.

The second line contains N binary digits separated by spaces. The i-th digit describes the initial position of switch i: 0 means released, and 1 means pressed.

Each of the next N lines describes one switch. The line for switch M starts with an integer K, followed by K switch numbers in increasing order. These are the switches that are released when switch M is pressed. Switches are numbered from 1 to N. A switch never releases itself, and it may release no switches.

The input is chosen so that a solution always exists.

Output

Print one integer: the length of the shortest sequence of switch presses that leaves only switch 3 pressed and all other switches released.