#include <bits/stdc++.h>
using namespace std;
int a[1000005];
vector<int> b[500002];
int main() {
  ios_base::sync_with_stdio(0);
  int n, c, x;
  cin >> n >> c;
  for (int i = 0; i < n; i++) {
    cin >> x;
    for (int j = 0, k; j < x; j++) {
      cin >> k;
      b[i].push_back(k);
    }
  }
  for (int i = 0; i < (n - 1); i++) {
    int l1 = b[i].size();
    int l2 = b[i + 1].size();
    int j;
    for (j = 0; j < l1 && j < l2; j++)
      if (b[i][j] != b[i + 1][j]) break;
    if (j == l1)
      continue;
    else if (j == l2) {
      cout << "-1\n";
      return 0;
    } else if (b[i][j] > b[i + 1][j]) {
      a[0]++;
      a[c - b[i][j] + 1]--;
      a[c - b[i + 1][j] + 1]++;
    } else if (b[i][j] < b[i + 1][j]) {
      a[c - b[i][j] + 1]--;
      a[c - b[i + 1][j] + 1]++;
    }
  }
  for (int i = 1; i <= c; i++) {
    a[i] += a[i - 1];
  }
  for (int i = 0; i <= c; i++) {
    if (a[i] == 0) {
      cout << i << endl;
      return 0;
    }
  }
  cout << "-1\n";
  return 0;
}
