#include <bits/stdc++.h>
using namespace std;
const string yno[2] = {"NO\n", "YES\n"};
int n, c, b[1000005];
vector<vector<int>> w;
void process() {
  cin >> n >> c;
  w.assign(n, vector<int>());
  for (int i = 0; i <= n - 1; i++) {
    int l;
    cin >> l;
    w[i].resize(l);
    for (int &j : w[i]) cin >> j;
  }
  for (int i = 0; i <= n - 2; i++) {
    int l = 0, r = c - 1;
    bool out = false;
    for (int j = 0; j < min(w[i].size(), w[i + 1].size()); j++) {
      if (w[i][j] != w[i + 1][j]) {
        r = c - w[i + 1][j];
        l = c - w[i][j] + 1;
        out = true;
        break;
      }
    }
    if (!out && w[i].size() > w[i + 1].size()) {
      cout << -1;
      return;
    }
    if (l <= r) {
      b[l]++;
      b[r + 1]--;
    } else {
      b[0]++;
      b[r + 1]--;
      b[l]++;
      b[c]--;
    }
  }
  for (int i = 0; i <= c - 1; i++) {
    if (i) b[i] += b[i - 1];
    if (b[i] == n - 1) {
      cout << i;
      return;
    }
  }
  cout << -1;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  process();
  return 0;
}
