#include <bits/stdc++.h>
using namespace std;
int n, c, k1, k2, a[2][1111111], num[1111111];
void update(int l, int r) { num[l]++, num[r + 1]--; }
int main() {
  scanf("%d%d", &n, &c);
  {
    memset(num, 0, sizeof(num));
    int gg = 0, now = 0, res = 0, ans = -1;
    scanf("%d", &k1);
    for (int i = 1; i <= k1; i++) scanf("%d", &a[0][i]);
    for (int i = 2; i <= n; i++) {
      scanf("%d", &k2);
      for (int j = 1; j <= k2; j++) scanf("%d", &a[now ^ 1][j]);
      int x, y, pos = -1;
      for (int j = 1; j <= min(k1, k2); j++)
        if (a[now][j] != a[now ^ 1][j]) {
          pos = j;
          x = a[now][j] - 1;
          y = a[now ^ 1][j] - 1;
          break;
        }
      if (pos == -1) {
        if (k1 > k2) gg = 1;
      } else {
        res++;
        if (x > y)
          update(c - x, c - 1 - y);
        else
          update(0, c - y - 1), update(c - x, c - 1);
      }
      k1 = k2, now ^= 1;
    }
    for (int i = 1; i < c; i++) num[i] += num[i - 1];
    for (int i = 0; i < c; i++)
      if (num[i] == res) {
        ans = i;
        break;
      }
    if (gg) ans = -1;
    printf("%d\n", ans);
  }
  return 0;
}
