#include <bits/stdc++.h>
using namespace std;
int p[105];
inline int parent(int i) { return (p[i] == i) ? i : p[i] = parent(p[i]); }
inline void unite(const int i, const int j) { p[parent(i)] = parent(j); }
int main() {
  int n, m;
  scanf("%d%d", &n, &m);
  for (int i = 0; i < n; ++i) p[i] = i;
  vector<int> v[105];
  int x, y;
  bool found = false;
  for (int i = 0; i < n; ++i) {
    scanf("%d", &x);
    while (x--) {
      scanf("%d", &y);
      v[y].push_back(i);
      found = true;
    }
  }
  for (int i = 1; i <= m; ++i) {
    if (v[i].empty()) continue;
    int prev = *v[i].begin();
    for (auto iter = v[i].begin(); iter != v[i].end(); ++iter)
      unite(prev, *iter);
  }
  set<int> s;
  for (int i = 0; i < n; ++i) s.insert(parent(i));
  printf("%d", s.size() - found);
  return 0;
}
