#include <bits/stdc++.h>
using namespace std;
int n, c;
vector<vector<int> > W;
int main() {
  ios::sync_with_stdio(false);
  cin >> n >> c;
  W = vector<vector<int> >(n);
  for (int(i) = 0; (i) < (int)(n); ++(i)) {
    int l;
    cin >> l;
    W[i].resize(l);
    for (auto& x : W[i]) {
      cin >> x;
      --x;
    }
  }
  vector<int> oks(c + 1, 0);
  for (int(i) = 0; (i) < (int)(n - 1); ++(i)) {
    vector<int>& a = W[i];
    vector<int>& b = W[i + 1];
    int j = 0;
    for (; j < a.size() && j < b.size(); ++j) {
      if (a[j] < b[j]) {
        ++oks[0];
        --oks[c - b[j]];
        ++oks[c - a[j]];
        --oks[c];
        break;
      } else if (a[j] > b[j]) {
        ++oks[c - a[j]];
        --oks[c - b[j]];
        break;
      }
    }
    if (j >= a.size()) {
      ++oks[0];
      --oks[c];
    }
  }
  for (int(i) = 0; (i) < (int)(c); ++(i)) oks[i + 1] += oks[i];
  int sol = -1;
  for (int(i) = 0; (i) < (int)(c); ++(i)) {
    if (oks[i] == n - 1) {
      sol = i;
      break;
    }
  }
  cout << sol;
}
