#include <bits/stdc++.h>
using namespace std;
const double pi = acos(-1.0);
int find(vector<int>& par, int x) {
  if (x != par[x]) return find(par, par[x]);
  return x;
}
bool same(vector<int>& par, int x, int y) {
  return find(par, x) == find(par, y);
}
void merge(vector<int>& par, vector<int>& sz, int x, int y) {
  int X = find(par, x);
  int Y = find(par, y);
  if (sz[X] < sz[Y]) swap(X, Y);
  par[Y] = X;
  sz[X] += sz[Y];
  return;
}
int main(int argc, char const* argv[]) {
  cin.sync_with_stdio(0);
  cin.tie(0);
  int n, m;
  cin >> n >> m;
  vector<int> sz(n);
  vector<int> par(n);
  for (int i = 0; i < n; i++) {
    sz[i] = 1;
    par[i] = i;
  }
  vector<vector<int> > l(m);
  int maxk = -1;
  int ind = -1;
  for (int i = 0; i < n; i++) {
    int k;
    cin >> k;
    if (maxk < k) {
      maxk = k;
      ind = i;
    }
    for (int j = 0; j < k; j++) {
      int lng;
      cin >> lng;
      lng--;
      l[lng].push_back(i);
    }
  }
  for (int i = 0; i < m; i++) {
    for (int j = 0; j < l[i].size(); j++) {
      if (!same(par, l[i][0], l[i][j])) {
        merge(par, sz, l[i][0], l[i][j]);
      }
    }
  }
  int ret = 0;
  if (maxk == 0) {
    cout << n;
    return 0;
  }
  for (int i = 0; i < n; i++) {
    if (!same(par, ind, i)) {
      merge(par, sz, ind, i);
      ret++;
    }
  }
  cout << ret;
  return 0;
}
