#include <bits/stdc++.h>
using namespace std;
void init_ios() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
}
const int N = 1 << 20;
int n, c, drz[2 * N];
vector<int> word[N];
vector<pair<int, int> > p, zle;
pair<int, int> lacz(pair<int, int> x, pair<int, int> y) {
  return {max(x.first, y.first), min(x.second, y.second)};
}
void add(int x, int y) {
  x += N;
  y += N;
  drz[x]++;
  if (x != y) drz[y]++;
  while (x / 2 != y / 2) {
    if (x % 2 == 0) drz[x + 1]++;
    if (y % 2 == 1) drz[y - 1]++;
    x /= 2;
    y /= 2;
  }
}
int get(int x) {
  x += N;
  int res = drz[x];
  while (x > 1) {
    x /= 2;
    res += drz[x];
  }
  return res;
}
int main() {
  init_ios();
  cin >> n >> c;
  for (int i = 1; i <= n; ++i) {
    int l, x;
    cin >> l;
    while (l--) {
      cin >> x;
      --x;
      word[i].push_back(x);
    }
    if (i > 1) {
      bool git = false;
      int now = 0;
      pair<int, int> co = {-1, -1};
      for (int j = 0; j < min((int)word[i - 1].size(), (int)word[i].size());
           ++j) {
        if (word[i][j] > word[i - 1][j]) {
          git = true;
          add(c - word[i][j], c - 1 - word[i - 1][j]);
          break;
        }
        if (word[i][j] < word[i - 1][j]) {
          git = true;
          co = {c - word[i - 1][j], c - 1 - word[i][j]};
          break;
        }
      }
      if (!git) {
        if (word[i - 1].size() > word[i].size()) {
          cout << "-1\n";
          return 0;
        }
      } else if (co.first != -1)
        p.push_back(co);
    }
  }
  if (p.size() == 0) {
    cout << "0\n";
    return 0;
  }
  while (p.size() > 1) {
    pair<int, int> p1 = p.back();
    p.pop_back();
    pair<int, int> p2 = p.back();
    p.pop_back();
    p.push_back(lacz(p1, p2));
    if (p.back().first > p.back().second) {
      cout << "-1\n";
      return 0;
    }
  }
  int p1 = p.back().first, p2 = p.back().second;
  for (int i = p1; i <= p2; ++i) {
    if (get(i) == 0) {
      cout << i << "\n";
      return 0;
    }
  }
  cout << "-1\n";
}
