#include <bits/stdc++.h>
using namespace std;
int comps[100];
void uni(int i, int j, int n) {
  int pi = comps[i];
  int pj = comps[j];
  for (int i = 0; i < n; i++) {
    if (comps[i] == pj) comps[i] = pi;
  }
}
int main() {
  int n, m;
  cin >> n >> m;
  bool flag = false;
  for (int i = 0; i < n; i++) {
    comps[i] = i;
  }
  map<int, set<int> > sprecher;
  for (int i = 0; i < n; i++) {
    int k;
    cin >> k;
    for (int j = 0; j < k; j++) {
      flag = true;
      int l;
      cin >> l;
      for (set<int>::iterator it = sprecher[l].begin(); it != sprecher[l].end();
           it++) {
        uni(i, *it, n);
      }
      sprecher[l].insert(i);
    }
  }
  set<int> counter;
  for (int i = 0; i < n; i++) counter.insert(comps[i]);
  cout << counter.size() - (flag ? 1 : 0) << endl;
}
