#include <bits/stdc++.h>
using namespace std;
int n, c;
const int MAXN = 500005;
vector<int> a[MAXN];
int seg[1000005];
int go(int idx) {
  for (int i = 0; i < a[idx - 1].size() && i < a[idx].size(); i++) {
    if (a[idx - 1][i] < a[idx][i]) {
      seg[0]++;
      seg[c - a[idx][i] + 1]--;
      seg[c - a[idx - 1][i] + 1]++;
      seg[c]--;
      return 1;
    } else if (a[idx - 1][i] > a[idx][i]) {
      seg[c - a[idx - 1][i] + 1]++;
      seg[c - a[idx][i] + 1]--;
      return 1;
    }
  }
  if (a[idx - 1].size() > a[idx].size()) return 0;
  return 2;
}
int main() {
  ios::sync_with_stdio(0);
  cin >> n >> c;
  for (int i = 0; i < n; i++) {
    int l;
    cin >> l;
    for (int j = 0; j < l; j++) {
      int w;
      cin >> w;
      a[i].push_back(w);
    }
  }
  int counter = 0;
  for (int i = 1; i < n; i++) {
    int r = go(i);
    if (r == 0) {
      cout << -1 << "\n";
      return 0;
    } else if (r == 1)
      counter++;
  }
  int net = 0;
  for (int i = 0; i < c; i++) {
    net += seg[i];
    if (net >= counter) {
      cout << i << "\n";
      return 0;
    }
  }
  cout << -1 << "\n";
  return 0;
}
