#include <bits/stdc++.h>
using namespace std;
const int MOD = 1000000007;
class sumsegtree {
  void build(int node, int ss, int se) {
    if (se - ss == 1) {
      tree[node] = arr[ss];
      return;
    }
    int m = (ss + se + 1) / 2;
    build(2 * node + 1, ss, m);
    build(2 * node + 2, m, se);
    tree[node] = tree[2 * node + 1] + tree[2 * node + 2];
  }
  void update(int node, int ss, int se, int i, int j, int val) {
    if (lazy[node] != 0) {
      tree[node] += lazy[node] * (se - ss);
      if (se - ss > 1) {
        lazy[2 * node + 1] += lazy[node];
        lazy[2 * node + 2] += lazy[node];
      }
      lazy[node] = 0;
    }
    if (j <= ss || se <= i) return;
    if (i <= ss && se <= j) {
      tree[node] += val * (se - ss);
      if (se - ss > 1) {
        lazy[2 * node + 1] += val;
        lazy[2 * node + 2] += val;
      }
      return;
    }
    int m = (ss + se + 1) / 2;
    update(2 * node + 1, ss, m, i, j, val);
    update(2 * node + 2, m, se, i, j, val);
    tree[node] = tree[2 * node + 1] + tree[2 * node + 2];
  }
  int query(int node, int ss, int se, int i, int j) {
    if (j <= ss || se <= i) return 0;
    if (lazy[node] != 0) {
      tree[node] += lazy[node] * (se - ss);
      if (se - ss > 1) {
        lazy[2 * node + 1] += lazy[node];
        lazy[2 * node + 2] += lazy[node];
      }
      lazy[node] = 0;
    }
    if (i <= ss && se <= j) return tree[node];
    int m = (ss + se + 1) / 2;
    return query(2 * node + 1, ss, m, i, j) + query(2 * node + 2, m, se, i, j);
  }

 public:
  int n;
  int* tree;
  int* arr;
  int* lazy;
  sumsegtree(int* ar, int nn) {
    n = nn;
    arr = ar;
    tree = new int[4 * n];
    build(0, 0, n);
    lazy = new int[4 * n];
    for (int i = 0; i < 4 * n; i++) lazy[i] = 0;
  }
  void update(int i, int j, int val) { update(0, 0, n, i, j, val); }
  void update(int i, int val) { update(0, 0, n, i, i + 1, val); }
  int query(int i, int j) { return query(0, 0, n, i, j); }
};
const int MAXN = 500005;
const int MAXC = 1000006;
int* word[MAXN];
int len[MAXN];
int main() {
  int n, c;
  scanf("%d %d", &n, &c);
  for (int i = 0; i < n; i++) {
    int l;
    scanf("%d", &l);
    len[i] = l;
    word[i] = new int[l];
    for (int j = 0; j < l; j++) scanf("%d", &(word[i][j]));
    for (int j = 0; j < l; j++) word[i][j]--;
  }
  int* fat = new int[c];
  for (int i = 0; i < c; i++) fat[i] = 0;
  sumsegtree tree(fat, c);
  for (int i = 1; i < n; i++) {
    int ml = min(len[i - 1], len[i]);
    for (int j = 0; j < ml + 1; j++) {
      if (j == ml) {
        if (len[i - 1] <= len[i]) tree.update(0, c, 1);
        break;
      }
      int a = word[i - 1][j];
      int b = word[i][j];
      if (a == b)
        continue;
      else if (a < b) {
        tree.update(0, c - b, 1);
        tree.update(c - a, c, 1);
      } else {
        tree.update(c - a, c - b, 1);
      }
      break;
    }
  }
  for (int i = 0; i < c; i++) {
    if (tree.query(i, i + 1) == n - 1) {
      cout << i << endl;
      return 0;
    }
  }
  cout << -1 << endl;
}
