题库练习 80-th Level Archeology
← 上一题 下一题 →

A10585 | 80-th Level Archeology

时间限制1s
内存限制256MB
通过 / 提交0/0

题目描述

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 $n$ 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 $x$ ( $1<=x<=c-1$ ) is replaced with hieroglyph $(x+1)$ , and hieroglyph $c$ is replaced with hieroglyph $1$ .

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 $n$ and $c$ ( $2<=n<=500000$ , $1<=c<=10^{6}$ ) — the number of words, written on the lock, and the number of different hieroglyphs.

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

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

输出格式

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

If it is impossible to open the door by this method, print $-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
C++ 编辑器
输入
输出