#include <bits/stdc++.h>
using namespace std;
using ii = pair<int, int>;
vector<int> ls[110];
class UFDS {
  vector<int> ps, size;
  int n;

 public:
  UFDS(int N) : ps(N + 1), size(N + 1), n(N) {
    for (int i = 1; i <= n; ++i) {
      ps[i] = i;
      size[i] = 1;
    }
  }
  bool same_set(int i, int j) { return parent(i) == parent(j); }
  int parent(int i) { return ps[i] == i ? i : (ps[i] = parent(ps[i])); }
  void union_set(int i, int j) {
    if (same_set(i, j)) return;
    auto p = parent(i);
    auto q = parent(j);
    if (size[p] > size[q])
      ps[q] = p;
    else if (size[q] > size[p])
      ps[p] = q;
    else {
      ps[q] = p;
      ++size[p];
    }
    --n;
  }
  int sets() { return n; }
};
int main() {
  int n, m;
  scanf("%d %d", &n, &m);
  int sum = 0;
  for (int i = 1; i <= n; ++i) {
    int k;
    scanf("%d", &k);
    sum += k;
    while (k--) {
      int L;
      scanf("%d", &L);
      ls[L].push_back(i);
    }
  }
  UFDS ufds(n);
  for (int k = 1; k <= m; ++k)
    for (size_t i = 0; i < ls[k].size(); ++i)
      for (size_t j = i + 1; j < ls[k].size(); ++j)
        ufds.union_set(ls[k][i], ls[k][j]);
  if (not sum)
    printf("%d\n", n);
  else
    printf("%d\n", ufds.sets() - 1);
  return 0;
}
