#include <bits/stdc++.h>
using namespace ::std;
typedef unsigned int uint;
typedef signed long long int64;
typedef unsigned long long uint64;
set<int> lang[105], eg;
int lg[105], e[105][105], ne[105];
int n, m;
int c;
void _set(int li, int gi) {
  lg[li] = gi;
  set<int>::iterator it;
  for (it = lang[li].begin(); it != lang[li].end(); ++it)
    if (lg[*it] == 0) _set(*it, gi);
}
int main() {
  while (cin >> n >> m) {
    for (int(i) = 0; (i) < (n); ++(i)) {
      cin >> ne[i];
      for (int(j) = 0; (j) < (ne[i]); ++(j)) {
        cin >> e[i][j];
        --e[i][j];
      }
      for (int(j) = 0; (j) < (ne[i]); ++(j)) {
        for (int(k) = 0; (k) < (ne[i]); ++(k)) {
          if (j == k) continue;
          lang[e[i][j]].insert(e[i][k]);
        }
      }
    }
    c = 1;
    for (int(i) = 0; (i) < (m); ++(i))
      if (lg[i] == 0) _set(i, c++);
    c = 0;
    for (int(i) = 0; (i) < (n); ++(i)) {
      if (ne[i] == 0)
        ++c;
      else
        eg.insert(lg[e[i][0]]);
    }
    cout << c + (eg.size() == 0 ? 0 : eg.size() - 1);
  }
  return 0;
  ;
}
