#include <bits/stdc++.h>
using namespace std;
template <class S, class T>
ostream& operator<<(ostream& os, const pair<S, T>& p) {
  return os << "(" << p.first << ", " << p.second << ")";
}
template <class T>
void debug(T a, T b) {
  cerr << "[";
  for (T i = a; i != b; ++i) {
    if (i != a) cerr << ", ";
    cerr << *i;
  }
  cerr << "]\n";
}
const int N = 1000500;
struct node {
  int sum;
  int toPush;
  inline void combine(const node& left, const node& right) {
    sum = left.sum + right.sum;
  }
} T[N << 2];
struct segmentTree {
  int n;
  segmentTree(int m) : n(m) { build(0, 0, n - 1); }
  void build(int x, int l, int r) {
    T[x].toPush = -1;
    if (l == r) {
      T[x].sum = 1;
      return;
    }
    int m = (l + r) >> 1;
    build((x << 1) + 1, l, m);
    build((x << 1) + 2, m + 1, r);
    T[x].combine(T[(x << 1) + 1], T[(x << 1) + 2]);
  }
  inline void resolve(int x, int l, int r) {
    if (T[x].toPush != -1) {
      T[x].sum = T[x].toPush * (r - l + 1);
      if (l != r) {
        T[(x << 1) + 1].toPush = T[x].toPush;
        T[(x << 1) + 2].toPush = T[x].toPush;
      }
      T[x].toPush = -1;
    }
  }
  void updateRange(int x, int l, int r, int ql, int qr, int v) {
    resolve(x, l, r);
    if (l > qr || r < ql) return;
    if (l >= ql && r <= qr) {
      T[x].toPush = v;
      resolve(x, l, r);
      return;
    }
    int m = (l + r) >> 1;
    updateRange((x << 1) + 1, l, m, ql, qr, v);
    updateRange((x << 1) + 2, m + 1, r, ql, qr, v);
    T[x].combine(T[(x << 1) + 1], T[(x << 1) + 2]);
  }
  int queryRange(int x, int l, int r, int ql, int qr) {
    resolve(x, l, r);
    if (l >= ql && r <= qr) return T[x].sum;
    int ret = 0;
    int m = (l + r) >> 1;
    if (ql <= m) ret += queryRange((x << 1) + 1, l, m, ql, qr);
    if (qr > m) ret += queryRange((x << 1) + 2, m + 1, r, ql, qr);
    return ret;
  }
  void update(int ql, int qr, int v) { updateRange(0, 0, n - 1, ql, qr, v); }
  int query(int ql, int qr) { return queryRange(0, 0, n - 1, ql, qr); }
};
vector<int> a[N];
int main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  int n, c;
  cin >> n >> c;
  for (int i = 0; i < (n); ++i) {
    int m;
    cin >> m;
    a[i].resize(m);
    for (int j = 0; j < (m); ++j) {
      cin >> a[i][j];
      --a[i][j];
    }
  }
  segmentTree seg(c);
  for (int i = (1); i < (n); ++i) {
    int diffIdx = -1;
    for (int j = 0; j < ((int)a[i].size()) and j < ((int)a[i - 1].size());
         ++j) {
      if (a[i][j] != a[i - 1][j]) {
        diffIdx = j;
        break;
      }
    }
    if (diffIdx == -1) {
      if (((int)a[i].size()) < ((int)a[i - 1].size())) {
        cout << "-1\n";
        return 0;
      }
    } else {
      if (a[i - 1][diffIdx] < a[i][diffIdx]) {
        seg.update(c - a[i][diffIdx], c - 1 - a[i - 1][diffIdx], 0);
      } else if (a[i - 1][diffIdx] > a[i][diffIdx]) {
        pair<int, int> invalid(c - a[i - 1][diffIdx], c - 1 - a[i][diffIdx]);
        if (invalid.first > 0) {
          seg.update(0, invalid.first - 1, 0);
        }
        if (invalid.second < c - 1) {
          seg.update(invalid.second + 1, c - 1, 0);
        }
      }
    }
  }
  if (seg.query(0, c - 1) > 0) {
    for (int i = 0; i < (c); ++i) {
      if (seg.query(i, i) > 0) {
        cout << i << "\n";
        return 0;
      }
    }
  } else {
    cout << "-1\n";
  }
  return 0;
}
