#include <bits/stdc++.h>
using namespace std;
const int N = 500010, C = 1000010;
int n, c;
int l[N], a[C];
vector<int> vec[N];
inline int read(register int x = 0, unsigned char ch = getchar()) {
  for (; !isdigit(ch); ch = getchar())
    ;
  for (; isdigit(ch); ch = getchar()) x = (x * 10) + (ch ^ 48);
  return x;
}
int main() {
  n = read(), c = read();
  for (register int i = 1; i <= n; i++) {
    l[i] = read();
    for (register int j = 1; j <= l[i]; j++) vec[i].push_back(read());
  }
  for (register int i = 2, j; i <= n; i++) {
    for (j = 0; (j < l[i - 1] && j < l[i]) && (vec[i - 1][j] == vec[i][j]); ++j)
      ;
    if (j == l[i - 1]) {
      ++a[0], --a[c];
      continue;
    }
    if (j == l[i]) return printf("-1\n"), 0;
    if (vec[i - 1][j] < vec[i][j]) {
      ++a[0], --a[c - vec[i][j] + 1];
      ++a[c - vec[i - 1][j] + 1], --a[c];
    } else {
      ++a[c - vec[i - 1][j] + 1], --a[c - vec[i][j] + 1];
    }
  }
  for (register int i = 0; i < c; i++)
    if ((a[i] += a[i - 1]) == n - 1) return printf("%d\n", i), 0;
  printf("-1\n");
}
