#include <bits/stdc++.h>
using namespace std;
struct vertex {
  bool ul;
  int parent;
};
vector<vertex> graph;
int n, m;
int find_set(int v) {
  if (v == graph[v].parent) return v;
  return graph[v].parent = find_set(graph[v].parent);
}
void union_sets(int a, int b) {
  a = find_set(a);
  b = find_set(b);
  if (a != b) {
    graph[b].parent = a;
  }
}
int main() {
  int k, emps = 0, lang1, lang2;
  cin >> n >> m;
  graph.resize(m + 1);
  for (int i = 1; i <= m; i++) {
    graph[i].parent = i;
    graph[i].ul = 0;
  }
  for (int i = 1; i <= n; ++i) {
    cin >> k;
    if (k == 0) {
      emps++;
      continue;
    }
    cin >> lang1;
    graph[lang1].ul = 1;
    for (int j = 2; j <= k; ++j) {
      cin >> lang2;
      graph[lang2].ul = 1;
      union_sets(lang1, lang2);
    }
  }
  int learn = 0;
  for (int i = 1; i <= m; i++) {
    if ((graph[i].ul) && (graph[i].parent == i)) learn++;
  }
  if (learn != 0) {
    --learn;
  }
  cout << emps + learn;
  return 0;
}
