#include <bits/stdc++.h>
using namespace std;
int D[1000000 + 20];
int E[1000000 + 20];
int main() {
  int n, c;
  scanf("%d%d", &n, &c);
  vector<int> fronts;
  vector<int> K;
  int num;
  int l = 0;
  int r = c;
  int l1 = 0;
  int r2 = 0;
  memset(E, 0, sizeof(E));
  vector<int> behinds;
  memset(D, 0, sizeof(D));
  for (int i = 0; i < n; i++) {
    scanf("%d", &num);
    int p;
    for (int k = 0; k < num; k++) {
      scanf("%d", &p);
      behinds.push_back(p);
    }
    if (i == 0) {
      fronts = behinds;
      behinds.clear();
      continue;
    } else {
      int dif = 0;
      for (int k = 0; k < min(behinds.size(), fronts.size()); k++) {
        if (behinds[k] != fronts[k]) {
          if (fronts[k] > behinds[k]) {
            l = max(l, c + 1 - fronts[k]);
            r = min(r, c - behinds[k]);
          } else {
            D[c - behinds[k] + 1]++;
            D[c + 1 - fronts[k]]--;
          }
          dif = 1;
          break;
        }
      }
      if (!dif) {
        if (fronts.size() > behinds.size()) {
          cout << -1 << endl;
          return 0;
        }
      }
    }
    fronts = behinds;
    behinds.clear();
  }
  if (l > r) {
    cout << -1 << endl;
  } else {
    for (int i = 1; i <= c; i++) {
      E[i] = E[i - 1] + D[i];
    }
    for (int h = l; h <= r; h++) {
      if (E[h] == 0) {
        cout << h << endl;
        return 0;
      }
    }
    cout << -1 << endl;
  }
  return 0;
}
