Codeforces Round 376 (Div. 2)
D
80-th Level Archeology
Codeforces
2200
原题
← CE →
翻译中…

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.

翻译中…

Input

翻译中…

The first line of the input contains two integers $*n*$ and $*c*$ ($2 ≤ *n* ≤ 500 000$, $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*} ≤ 500 000$), that denotes the length of the $*i*$-th word, followed by $*l*_{*i*}$ integers $*w*_{*i*, 1}$, $*w*_{*i*, 2}$, ..., $*w*_{*i*, *l*_{*i*}}$ ($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}$.

翻译中…

Output

翻译中…

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$.

翻译中…

Note

翻译中…

Word $*a*_{1}, *a*_{2}, ..., *a*_{*m*}$ of length $*m*$ is lexicographically not greater than word $*b*_{1}, *b*_{2}, ..., *b*_{*k*}$ of length $*k*$, if one of two conditions hold:

翻译中…
  • at first position $*i*$, such that $*a*_{*i*} ≠ *b*_{*i*}$, the character $*a*_{*i*}$ goes earlier in the alphabet than character $*b*_{*i*}$, i.e. $*a*$ has smaller character in the first position where they differ;
  • if there is no such position $*i*$ and $*m* ≤ *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 $1$ position clockwise the words look as follows:

翻译中…

1 3
2
3 1 2
3 1 2 3

翻译中…

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.

翻译中…

样例

Input #1
4 3
2 3 2
1 1
3 2 3 1
4 2 3 1 2
Output #1
1
InputCopy OutputCopy
Input #2
2 5
2 4 2
2 4 2
Output #2
0
InputCopy OutputCopy
Input #3
4 4
1 2
1 3
1 4
1 2
Output #3
-1
InputCopy OutputCopy
题目信息
题号
731D
时间限制
2 s
内存限制
256 MB
标签
暴力
数据结构
贪心
排序

题面与样例版权归 Codeforces 及出题人所有;中文翻译由 AI 生成(CI 管线),如有出入请以原文为准。