#include <bits/stdc++.h>
using namespace std;
struct UF {
  vector<int> uf;
  UF(int n) : uf(n) {
    for (int i = 0; i < int(n); ++i) uf[i] = i;
  }
  int find(int a) {
    if (a == uf[a]) return a;
    return uf[a] = find(uf[a]);
  }
  void unio(int a, int b) { uf[find(a)] = find(b); }
};
const int MAXLEN = 500009;
int n, c;
vector<vector<int> > in;
vector<pair<int, int> > res;
pair<int, int> inter(pair<int, int> a, pair<int, int> b) {
  return {max(a.first, b.first), min(a.second, b.second)};
}
void process_cand(pair<int, int> cand) {
  vector<pair<int, int> > nres;
  for (int k = 0; k < int(2); ++k) {
    for (auto& op : res) {
      auto np = inter(op, cand);
      if (np.first <= np.second) nres.push_back(np);
    }
    cand.first += c;
    cand.second += c;
  }
  res = nres;
}
void fail() {
  cout << -1 << endl;
  exit(0);
}
inline int znak(int y, int x) {
  if (x >= (int)in[y].size()) return 0;
  return in[y][x];
}
void refine(int k, int a, int b) {
  int aa = a;
  int bb = b;
  int st = 0;
  for (int y = int(a); y < int(b); ++y) {
    int cu = znak(y, k);
    if (st == 0 && cu) {
      st = 1;
      aa = y;
      continue;
    }
    if (st == 1 && !cu) {
      st = 2;
      bb = y;
      continue;
    }
    if (st == 2 && cu) fail();
  }
  int mi = INT_MAX;
  int ma = -INT_MAX;
  for (int y = int(aa); y < int(bb); ++y) {
    int cu = znak(y, k);
    mi = min(mi, cu);
    ma = max(ma, cu);
  }
  if (mi == INT_MAX) return;
  int first = znak(a, k);
  int la = first;
  int brk0 = -1;
  for (int y = int(a); y < int(b); ++y) {
    int cu = znak(y, k);
    if (cu == 0) continue;
    if (cu < la && cu > first) fail();
    if (cu < la && brk0 != -1) fail();
    if (cu < la) brk0 = la;
    if (brk0 != -1 && cu > first) fail();
    la = cu;
  }
  pair<int, int> cand;
  if (mi != ma) {
    if (brk0 == -1) {
      cand.first = -(mi - 1);
      cand.second = c - ma;
    } else {
      cand.first = c - znak(aa, k) + 1;
      cand.second = c - znak(bb - 1, k);
    }
    process_cand(cand);
  }
  int cval = -1;
  int y0 = a;
  for (int y = int(a); y < int(b); ++y) {
    int nval = znak(y, k);
    if (nval == 0) continue;
    if (nval != cval) {
      if (cval != -1) refine(k + 1, y0, y);
      y0 = y;
    }
    cval = nval;
  }
  if (cval > 0) refine(k + 1, y0, bb);
}
int main() {
  ios_base::sync_with_stdio(0);
  cin >> n >> c;
  in.resize(n);
  for (int i = 0; i < int(n); ++i) {
    int len;
    cin >> len;
    in[i].resize(len);
    for (int k = 0; k < int(len); ++k) cin >> in[i][k];
  }
  res.push_back({0, c - 1});
  for (int i = 0; i < int(n - 1); ++i) {
    int kk = max(in[i].size(), in[i + 1].size()) + 1;
    int rozny = INT_MAX;
    bool order = false;
    int a;
    int b;
    for (int k = 0; k < int(kk); ++k) {
      a = znak(i, k);
      b = znak(i + 1, k);
      if (a != b) {
        rozny = k;
        order = a < b;
        break;
      }
    }
    if (rozny == INT_MAX) continue;
    if (a == 0 || b == 0) {
      if (!order) fail();
      continue;
    }
    if (a < b) {
      process_cand({-(a - 1), c - b});
    } else {
      process_cand({c - a + 1, c - b});
    }
  }
  if (res.empty()) fail();
  cout << res[0].first << endl;
  return 0;
}
