CF731D.80-th Level Archeology

普及/提高-

通过率:0%

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

Archeologists have found a secret pass in the dungeon of one of the pyramids of Cycleland. To enter the treasury they have to open an unusual lock on the door. The lock consists of nn words, each consisting of some hieroglyphs. The wall near the lock has a round switch. Each rotation of this switch changes the hieroglyphs according to some rules. The instruction nearby says that the door will open only if words written on the lock would be sorted in lexicographical order (the definition of lexicographical comparison in given in notes section).

The rule that changes hieroglyphs is the following. One clockwise rotation of the round switch replaces each hieroglyph with the next hieroglyph in alphabet, i.e. hieroglyph xx ( 1<=x<=c11<=x<=c-1 ) is replaced with hieroglyph (x+1)(x+1) , and hieroglyph cc is replaced with hieroglyph 11 .

Help archeologist determine, how many clockwise rotations they should perform in order to open the door, or determine that this is impossible, i.e. no cyclic shift of the alphabet will make the sequence of words sorted lexicographically.

输入格式

The first line of the input contains two integers nn and cc ( 2<=n<=5000002<=n<=500000 , 1<=c<=1061<=c<=10^{6} ) — the number of words, written on the lock, and the number of different hieroglyphs.

Each of the following nn lines contains the description of one word. The ii -th of these lines starts with integer lil_{i} ( 1<=li<=5000001<=l_{i}<=500000 ), that denotes the length of the ii -th word, followed by lil_{i} integers wi,1w_{i,1} , wi,2w_{i,2} , ..., wi,liw_{i,li} ( 1<=wi,j<=c1<=w_{i,j}<=c ) — the indices of hieroglyphs that make up the ii -th word. Hieroglyph with index 11 is the smallest in the alphabet and with index cc — the biggest.

It's guaranteed, that the total length of all words doesn't exceed 10610^{6} .

输出格式

If it is possible to open the door by rotating the round switch, print integer xx ( 0<=x<=c10<=x<=c-1 ) that defines the required number of clockwise rotations. If there are several valid xx , print any of them.

If it is impossible to open the door by this method, print 1-1 .

输入输出样例

  • 输入#1

    4 3
    2 3 2
    1 1
    3 2 3 1
    4 2 3 1 2
    

    输出#1

    1
    
  • 输入#2

    2 5
    2 4 2
    2 4 2
    

    输出#2

    0
    
  • 输入#3

    4 4
    1 2
    1 3
    1 4
    1 2
    

    输出#3

    -1
    

说明/提示

Word a1,a2,...,ama_{1},a_{2},...,a_{m} of length mm is lexicographically not greater than word b1,b2,...,bkb_{1},b_{2},...,b_{k} of length kk , if one of two conditions hold:

  • at first position ii , such that aibia_{i}≠b_{i} , the character aia_{i} goes earlier in the alphabet than character bib_{i} , i.e. aa has smaller character in the first position where they differ;
  • if there is no such position ii and m<=km<=k , i.e. the first word is a prefix of the second or two words are equal.

The sequence of words is said to be sorted in lexicographical order if each word (except the last one) is lexicographically not greater than the next word.

In the first sample, after the round switch is rotated 11 position clockwise the words look as follows:

<br></br>1 3<br></br>2<br></br>3 1 2<br></br>3 1 2 3<br></br>In the second sample, words are already sorted in lexicographical order.

In the last sample, one can check that no shift of the alphabet will work.

首页