This page is still under construction.

Parts of this page are still being built. What you see may change.

Punched Cards

Time limit1sMemory limit1024 MB

Summary
Order n punched cards so that, reading each column downward, the first letter encountered spells the target string s, or report that no order works.
Level

Medium7 of 10

Topics
Graph, Topological sort, Greedy, Sorting
Solved
No attempts yet

Problem

nn punched cards were found in the warehouse of the company hosting the programming olympiad. A punched card is a strip of mm cells, each of which either contains a lowercase English letter or is a hole.

The olympiad jury wants to order all the punched cards so that, when they are placed one under another from top to bottom in this order, the olympiad slogan appears: the given string ss of length mm.

In other words, fix the order in which the cards are laid and consider an arbitrary position ii (1≤i≤m1 \le i \le m). Then the ii-th character of the string ss must match the character at position ii of the topmost punched card that has a letter at position ii. If for some ii there is no punched card with a letter at position ii, then the required string ss is considered impossible to obtain.

Help the jury figure out the order in which the punched cards must be placed.

Fig. 1: The order of the cards from the second sample. The letters visible from above are highlighted

Input

The first line contains two integers nn and mm (1≤n,m≤100 0001 \le n, m \le 100\,000), the number of punched cards and the number of cells, respectively.

The second line contains the string ss consisting of mm lowercase English letters.

The ii-th of the following nn lines contains the description of the ii-th punched card.

The description begins with an integer k_ik\_i (0≤k_i≤m0 \le k\_i \le m), the number of positions with letters on this punched card. The sum of all k_ik\_i is guaranteed not to exceed 200 000200\,000.

Then follows the description of the letters on this punched card: k_ik\_i pairs a_i,ja\_{i,j}, c_i,jc\_{i,j} (1≤a_i,j≤m1 \le a\_{i,j} \le m, c_i,jc\_{i,j} is a lowercase English letter) for all integers 1≤j≤k_i1 \leq j \leq k\_i; each pair indicates that the character c_i,jc\_{i,j} is present at position a_i,ja\_{i,j}. The remaining positions contain holes. The numbers of the positions with letters on a single punched card are guaranteed to be given in increasing order, that is, for any 1≤j<k_i1 \leq j < k\_i we have a_i,j<a_i,j+1a\_{i,j} < a\_{i,j+1}.

Output

If there is a way to order the punched cards as required, output nn integers p_1,p_2,…,p_np\_1, p\_2, \ldots, p\_n (1≤p_i≤n1 \le p\_i \le n), where p_1p\_1 is the number of the topmost punched card, p_2p\_2 is the number of the second card from the top, and so on up to the card p_np\_n, which lies at the bottom. If there are several possible answers, you may output any of them.

If there is no way to order the punched cards as required, output the single number −1-1.

Notes

  • n≤100 000n \le 100\,000
  • m≤100 000m \le 100\,000

Examples3

  1. Example 1

    Input
    1 1
    a
    1 1 a
    
    Expected output
    1
    
  2. Example 2

    Input
    3 4
    glhf
    3 1 r 3 h 4 i
    3 1 r 2 l 3 o
    2 1 g 4 f
    
    Expected output
    3 1 2
    
  3. Example 3

    Input
    2 2
    aa
    2 1 a 2 b
    2 1 b 2 a
    
    Expected output
    -1