human
Give me a Python program to determine that this is impossible, i given the following:
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 ≤ 106) — 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 li (1 ≤ li ≤ 500 000), that denotes the length of the i-th word, followed by li integers wi, 1, wi, 2, ..., wi, li (1 ≤ wi, 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 106.

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.

Examples

Input

4 3
2 3 2
1 1
3 2 3 1
4 2 3 1...
assistant
#Here is Python program to determine that this is impossible, i.

#!/usr/bin/env python
# coding: utf-8

# Тип: пересечение диапазонов

if __name__ == '__main__':
    import sys
    f = sys.stdin

    if False:
        import StringIO
        f = StringIO.StringIO("""4 3
2 3 2
1 1
3 2 3 1
4 2 3 1 2""")

        f = StringIO.StringIO("""2 5
2 4 2
2 4 2""")

        f = StringIO.StringIO("""4 4
1 2
1 3
1 4
1 2""")

        f = StringIO.StringIO("""2 10
14 9 6 7 1 6 9 3 1 9 4 6 8 8 1
3 3 7 6""")

        f = StringIO.StringIO("""4 4
1 3
1 4
1 1
1 2""")

    if False:
        import os
        with open(os.path.join(os.path.dirname(__file__), "731D_gen.txt"), "w") as f:
            n, alp_cnt = 500000, 10**6
            f.write("%(n)s %(alp_cnt)s\n" % locals())

            import random

            ln = 10**6 / n
            for i in xrange(n):
                lst = [ln]
                for j in xrange(ln):
                    lst.append(random.randint(1, alp_cnt))

                f.write("%s\n" % " ".join(str(k) for k in lst))

            sys.exit(0)

    def getline():
        return next(f).rstrip('\n\r')

    def read_int_line():
        return map(int, next(f).split())

    n, alp_cnt = read_int_line()

    assert n > 1
    def read_word():
        return read_int_line()[1:]

    set_lst = [0] * (alp_cnt + 1)
    def add_range(x, y):
        set_lst[x] += 1
        set_lst[y] -= 1

    prev_word = read_word()
    bad_prefix = False
    for i in xrange(1, n):
        word = read_word()

        is_prefix = True

        ln  = len(prev_word)
        ln2 = len(word)
        for i in xrange(min(ln, ln2)):
            c, c2 = prev_word[i]-1, word[i]-1

            diff = c - c2
            if diff == 0:
                pass
            else:
                # дополнения
                if diff < 0:
                    # [alp_cnt - c2, alp_cnt - c)
                    add_range(alp_cnt - c2, alp_cnt - c)
                else:
                    # [alp_cnt - c2, alp_cnt), [0, alp_cnt - c)
                    add_range(alp_cnt - c2, alp_cnt)
                    add_range(0,...