#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 100;
vector<int> a[N];
int d[N];
void work(int l, int r) {
  d[l]++;
  d[r + 1]--;
}
int main() {
  cin.tie(0);
  cin.sync_with_stdio(false);
  memset(d, 0, sizeof(d));
  int n, c, m, tp, i, j, x, y;
  cin >> n >> c;
  int sum = 0, flag = 1;
  for (i = 1; i <= n; i++) {
    cin >> m;
    a[i].push_back(m);
    for (j = 1; j <= a[i][0]; j++) {
      cin >> m;
      a[i].push_back(m);
    }
  }
  for (i = 1; i < n; i++) {
    tp = min(a[i][0], a[i + 1][0]);
    for (j = 1; j <= tp; j++) {
      x = a[i][j];
      y = a[i + 1][j];
      if (x != y) break;
    }
    if (j > tp && a[i][0] > a[i + 1][0]) {
      cout << -1 << endl;
      return 0;
    }
    if (y > x) {
      work(0, c - y);
      work(c + 1 - x, c - 1);
    } else if (x > y) {
      work(c + 1 - x, c - y);
    } else
      work(0, c - 1);
  }
  for (int i = 0; i < c; i++) {
    sum += d[i];
    if (sum == n - 1) {
      cout << i << endl;
      return 0;
    }
  }
  cout << -1 << endl;
  return 0;
}
