Under construction

Still being built. Things may change

Nightmare Brother

Interview

Time limit2sMemory limit1024 MB

Summary
Given N hints placing substrings in a length-M string, with at most one hint false, print the unique possible string, -1 if none exists, or -2 if several do.
Level

Medium5 of 10

Topics
Brute force, String, Implementation
Solved
No attempts yet

Problem

Your brother has a string SS of length MM with indices from 11 to MM. You want to know exactly what string SS is. To help you, he gives you NN hints. Hint ii is an integer XiX_i and a string TiT_i, meaning that TiT_i appears as a substring of SS starting at index XiX_i. All hints are unique: there are no hints ii and jj with i≠ji \ne j such that Xi=XjX_i = X_j and Ti=TjT_i = T_j.

Your brother is known to be mischievous. He tells you that at most one of the NN hints is false, but he does not say which one.

A string SS is a possible solution if and only if there is a set of at least N−1N - 1 hints (assumed true) such that SS is the only string consistent with all hints in that set.

Find a possible solution. If there is none, output -1. If there is more than one, output -2.

Input

The first line contains two integers NN and MM (1≤N≤1001 \le N \le 100, 1≤M≤1001 \le M \le 100), the number of hints and the length of the string. Each of the next NN lines contains an integer XiX_i and a string TiT_i (1≤Xi1 \le X_i, 1≤∣Ti∣1 \le |T_i|, Xi+∣Ti∣−1≤MX_i + |T_i| - 1 \le M). The string TiT_i consists only of uppercase letters. No two hints have the same XiX_i and the same TiT_i.

Output

If there is exactly one possible solution, print the string SS on a single line. If there is no possible solution, print -1 on a single line. If there is more than one possible solution, print -2 on a single line.

Examples4

  1. Example 1

    Input
    3 11
    5 JAKARTA
    1 ICPC
    3 BINUS
    
    Expected output
    ICPCJAKARTA
    
  2. Example 2

    Input
    3 9
    6 EX
    8 AM
    1 FINAL
    
    Expected output
    FINALEXAM
    
  3. Example 3

    Input
    3 8
    1 GRAD
    5 UAL
    6 ATE
    
    Expected output
    -1
    
  4. Example 4

    Input
    3 5
    1 BIN
    4 US
    4 OM
    
    Expected output
    -2