#include <bits/stdc++.h>
using namespace std;
int n, c;
vector<int> locck[567567];
const int S = 1 << 20;
int tree[2 * S];
void loduj() {
  cin >> n >> c;
  int x, l;
  for (int i = 0; i < n; ++i) {
    cin >> l;
    for (int j = 0; j < l; ++j) {
      cin >> x;
      locck[i].push_back(x);
    }
  }
  for (int i = 1; i < 2 * S; ++i) {
    tree[i] = 1;
  }
}
void forbRec(int p, int q, int pp, int qq, int no) {
  if (q < pp || qq < p) {
    return;
  }
  if (p <= pp && qq <= q) {
    tree[no] = 0;
    return;
  }
  int ss = (pp + qq) / 2;
  forbRec(p, q, pp, ss, 2 * no);
  forbRec(p, q, ss + 1, qq, 2 * no + 1);
}
void forb(int p, int q) { forbRec(p, q, 0, S - 1, 1); }
bool isVal(int p) {
  p += S;
  while (p) {
    if (!tree[p]) {
      return 0;
    }
    p /= 2;
  }
  return 1;
}
void cons(int x, int y) {
  if (x < y) {
    forb(c - y + 1, c - x);
  } else {
    forb(0, c - x);
    forb(c - y + 1, c - 1);
  }
}
void cons(vector<int> &u, vector<int> &v) {
  int d = 0;
  while ((d < u.size()) && (d < v.size()) && u[d] == v[d]) {
    ++d;
  }
  if (d == u.size()) {
    return;
  }
  if (d == v.size()) {
    forb(0, c - 1);
    return;
  }
  cons(u[d], v[d]);
}
void makeInts() {
  for (int i = 0; i < n - 1; ++i) cons(locck[i], locck[i + 1]);
}
int solve() {
  for (int i = 0; i < c; ++i)
    if (isVal(i)) return i;
  return -1;
}
int main() {
  ios_base::sync_with_stdio(0);
  loduj();
  makeInts();
  cout << solve() << endl;
}
