#include <bits/stdc++.h>
using namespace std;
const int N = 1e2 + 1;
int parent[N];
int ranking[N];
vector<vector<int> > a(N);
void make_set(int v) {
  if (parent[v]) return;
  parent[v] = v;
}
int find_set(int v) {
  if (parent[v] == v) return v;
  return parent[v] = find_set(parent[v]);
}
bool union_sets(int v, int u, int i) {
  if (v == 0) {
    v = u;
    a[i].pop_back();
    a[i].push_back(u);
  }
  if (u == 0) {
    u = v;
    a[i + 1].pop_back();
    a[i + 1].push_back(v);
  }
  v = find_set(v);
  u = find_set(u);
  if (v != u) {
    if (ranking[v] == ranking[u]) ranking[v]++;
    if (ranking[v] < ranking[u]) swap(v, u);
    parent[u] = v;
    return true;
  }
  return false;
}
void solve() {
  int n, m, k, language;
  cin >> n >> m;
  for (int i = 1; i <= n; i++) {
    cin >> k;
    for (int j = 1; j <= k; j++) {
      cin >> language;
      make_set(language);
      a[i].push_back(language);
    }
    for (int j = 1; j < k; j++) union_sets(a[i][j - 1], a[i][j], i);
  }
  int ans = 0, x = 0;
  for (int i = 1; i < n; i++) {
    if (!a[i].size()) ans++, a[i].push_back(0);
    if (!a[i + 1].size()) ans++, a[i + 1].push_back(0);
    if (union_sets(a[i][0], a[i + 1][0], i)) ans++;
  }
  cout << ans;
}
int main() {
  ios_base::sync_with_stdio(0);
  solve();
}
