#include <bits/stdc++.h>
using namespace std;
int l[500010];
vector<int> w[500010];
int Turn[1000010];
int main() {
  int n, c;
  cin >> n >> c;
  for (int i = 0; i < n; i++) {
    scanf("%d", l + i);
    for (int j = 0; j < l[i]; j++) {
      int x;
      scanf("%d", &x);
      w[i].push_back(x);
    }
  }
  int Criteria = 0;
  for (int i = 1; i < n; i++) {
    bool NoSolution = true;
    for (int j = 0; j < min(l[i - 1], l[i]); j++) {
      if (w[i - 1][j] != w[i][j]) {
        if (w[i - 1][j] > w[i][j]) {
          Criteria++;
        }
        Turn[c - w[i - 1][j] + 1]++;
        Turn[c - w[i][j] + 1]--;
        NoSolution = false;
        break;
      }
    }
    if (NoSolution && l[i - 1] > l[i]) {
      cout << "-1" << endl;
      return 0;
    }
  }
  int Exam = 0;
  for (int i = 0; i < c; i++) {
    Exam += Turn[i];
    if (Exam == Criteria) {
      cout << i << endl;
      return 0;
    }
  }
  cout << "-1" << endl;
  return 0;
}
