#include <bits/stdc++.h>
using namespace std;
long long m, n;
vector<long long> w[500005];
vector<pair<long long, long long> > bad;
bool good = true;
void process(long long start, long long end, long long nth) {
  while (start < end && w[start].size() <= nth) start++;
  while (start < end) {
    long long cur = start;
    while (cur + 1 < end) {
      if (w[cur + 1].size() <= nth) {
        good = false;
        cur = end;
        break;
      } else if (w[cur + 1][nth] == w[cur][nth])
        cur++;
      else
        break;
    }
    if (!good) break;
    if (cur > start) process(start, cur + 1, nth + 1);
    if (cur >= end - 1) break;
    long long t1 = w[cur][nth];
    long long t2 = w[cur + 1][nth];
    if (t1 < t2) {
      bad.push_back({n - t2 + 1, n - t1});
    } else {
      bad.push_back({0, n - t1});
      bad.push_back({n - t2 + 1, n});
    }
    start = cur;
    start++;
  }
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cin >> m >> n;
  for (long long i = 0; i < m; i++) {
    long long l;
    cin >> l;
    for (long long j = 0; j < l; j++) {
      long long a;
      cin >> a;
      w[i].push_back(a);
    }
  }
  process(0, m, 0);
  if (!good) {
    cout << -1 << endl;
    return 0;
  }
  vector<pair<long long, long long> > ev;
  for (long long i = 0; i < bad.size(); i++) {
    ev.push_back({bad[i].first, 0});
    ev.push_back({bad[i].second + 1, 1});
  }
  sort(ev.begin(), ev.end());
  if (ev.size() == 0 || ev[0].first != 0) {
    cout << 0 << endl;
    return 0;
  }
  long long cnt = 0;
  for (long long i = 0; i < ev.size(); i++) {
    if (ev[i].second == 0)
      cnt++;
    else
      cnt--;
    if (cnt == 0 && ev[i].first < n) {
      cout << ev[i].first << endl;
      return 0;
    }
  }
  if (ev[ev.size() - 1].first < n - 1) {
    cout << n - 1 << endl;
    return 0;
  }
  cout << -1 << endl;
}
