#include <bits/stdc++.h>
using namespace std;
inline long long gcd(long long a, long long b) {
  return (b ? gcd(b, a % b) : a);
}
inline long long lcm(long long a, long long b) { return (a / gcd(a, b)) * b; }
const int MAXN = 500005;
bool deuRuim = false;
vector<int> v[MAXN];
int n, c;
bool iguais(int a, int b) {
  if (((int)v[a].size()) != ((int)v[b].size())) return false;
  for (int i = 0; i < ((int)v[a].size()); i++)
    if (v[a][i] != v[b][i]) return false;
  return true;
}
vector<pair<int, int> > comp(int a, int b) {
  vector<pair<int, int> > ans;
  if (iguais(a, b)) {
    ans.push_back(make_pair(0, c));
  } else {
    bool igual = true;
    for (int i = 0; i < min(((int)v[a].size()), ((int)v[b].size())); i++) {
      if (v[a][i] != v[b][i]) igual = false;
    }
    if (igual) {
      if (((int)v[a].size()) > ((int)v[b].size()))
        deuRuim = true;
      else
        ans.push_back(make_pair(0, c));
    } else {
      for (int i = 0; i < min(((int)v[a].size()), ((int)v[b].size())); i++) {
        if (v[a][i] > v[b][i]) {
          ans.push_back(make_pair(c - v[a][i] + 1, c - v[b][i]));
          break;
        }
        if (v[a][i] < v[b][i]) {
          ans.push_back(make_pair(0, c - v[b][i]));
          ans.push_back(make_pair(c - v[a][i] + 1, c));
          break;
        }
      }
    }
  }
  return ans;
}
pair<int, int> inter(pair<int, int> a, pair<int, int> b) {
  int L = max(a.first, b.first);
  int R = min(a.second, b.second);
  if (L > R) return make_pair(-1, -1);
  return make_pair(L, R);
}
void junta(vector<pair<int, int> > &ans, vector<pair<int, int> > first) {
  vector<pair<int, int> > novo;
  pair<int, int> aux;
  for (int i = 0; i < ((int)ans.size()); i++) {
    aux = ans[i];
    for (int j = 0; j < ((int)first.size()); j++) {
      pair<int, int> oxi = inter(aux, first[j]);
      if (oxi != make_pair(-1, -1)) {
        novo.push_back(oxi);
      }
    }
  }
  ans = novo;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> n >> c;
  for (int i = 0, first, k; i < n; i++) {
    cin >> k;
    for (int j = 0; j < k; j++) {
      cin >> first;
      v[i].push_back(first);
    }
  }
  vector<pair<int, int> > ans;
  ans.push_back(make_pair(0, c));
  for (int i = 1; i < n; i++) {
    vector<pair<int, int> > aux = comp(i - 1, i);
    junta(ans, aux);
  }
  if (deuRuim || ans.empty())
    cout << -1 << endl;
  else {
    sort(ans.begin(), ans.end());
    cout << ans[0].first << endl;
  }
}
