#include <bits/stdc++.h>
using namespace std;
using ulong = unsigned long;
using ll = long long;
const int M = 1e9 + 7;
struct SegNode {
  SegNode *left = nullptr, *right = nullptr;
  int start, last;
  bool value = true;
  static vector<SegNode> data;
  static int idx;
  static SegNode make(int l, int r) {
    static bool made = false;
    if (made) {
      exit(1);
    }
    made = true;
    idx = 0;
    data.resize(2 * (r - l + 1));
    return (data[idx++] = SegNode(l, r));
  }
  SegNode() {}
  SegNode(int l, int r) : start(l), last(r) {
    if (l != r) {
      left = &(data[idx++] = SegNode(l, (l + r) / 2));
      right = &(data[idx++] = SegNode((l + r) / 2 + 1, r));
    }
  }
  void setFalse(int l, int r) {
    if (last < l || r < start) {
      return;
    }
    if (l <= start && last <= r) {
      value = false;
      return;
    }
    if (start != last) {
      left->setFalse(l, r);
      right->setFalse(l, r);
      if (left->value == false && right->value == false) {
        value = false;
      }
    }
  }
  int getTruePos(int l, int r) {
    if (value == false) {
      return M;
    }
    if (start == last) {
      return start;
    }
    int pos = left->getTruePos(l, r);
    if (pos < M) {
      return pos;
    }
    return right->getTruePos(l, r);
  }
};
int SegNode::idx;
vector<SegNode> SegNode::data;
int main() {
  int n, c;
  cin >> n >> c;
  vector<vector<int>> wss(n);
  for (int i = 0; i < n; i++) {
    int l;
    cin >> l;
    wss[i].resize(l);
    for (int j = 0; j < l; j++) {
      int w;
      scanf("%d", &w);
      wss[i][j] = w;
    }
  }
  SegNode sn = SegNode::make(0, c - 1);
  for (int i = 0; i < n - 1; i++) {
    int idx = 0;
    bool same = false;
    while (wss[i][idx] == wss[i + 1][idx]) {
      idx++;
      if (idx == wss[i].size() || idx == wss[i + 1].size()) {
        if (wss[i].size() > wss[i + 1].size()) {
          cout << -1 << endl;
          return 0;
        }
        same = true;
        break;
      }
    }
    int l = wss[i][idx], r = wss[i + 1][idx];
    if (same) {
      continue;
    }
    if (l < r) {
      sn.setFalse(c - r + 1, c - l);
    } else {
      sn.setFalse(0, c - l);
      sn.setFalse(c - r + 1, c - 1);
    }
  }
  int pos = sn.getTruePos(0, c - 1);
  cout << (pos == M ? -1 : pos) << endl;
  return 0;
}
