#include <bits/stdc++.h>
using namespace std;
vector<int> v[500002];
int a[1000002], l, k, n, c;
int main() {
  scanf("%d%d", &n, &c);
  for (int i = 0; i < n; i++) {
    scanf("%d", &k);
    while (k--) {
      scanf("%d", &l);
      v[i].push_back(l);
    }
  }
  for (int i = 0; i < n - 1; i++) {
    int pos;
    for (pos = 0; pos < min(v[i].size(), v[i + 1].size()); pos++) {
      if (v[i][pos] != v[i + 1][pos]) break;
    }
    if (pos == min(v[i].size(), v[i + 1].size())) {
      if (v[i].size() > v[i + 1].size()) {
        cout << -1 << endl;
        return 0;
      }
      continue;
    }
    if (v[i][pos] < v[i + 1][pos]) {
      a[c - v[i + 1][pos] + 1]++;
      a[c - v[i][pos] + 1]--;
    } else {
      a[0]++;
      a[c - v[i][pos] + 1]--;
      a[c - v[i + 1][pos] + 1]++;
      a[c + 1]--;
    }
  }
  k = 0;
  for (int i = 0; i < c; i++) {
    k += a[i];
    if (k == 0) {
      cout << i << endl;
      return 0;
    }
  }
  cout << -1 << endl;
  return 0;
}
