#include <bits/stdc++.h>
using namespace std;
vector<vector<int> > e;
vector<int> ind;
void dfs(int v, int k) {
  if (ind[v] == -1) {
    ind[v] = k;
    for (int i = 0; i < e[v].size(); ++i) dfs(e[v][i], k);
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  int n, m;
  cin >> n >> m;
  e.resize(n);
  ind.resize(n, -1);
  vector<vector<int> > speakers(m);
  bool all_zeros = true;
  for (int i = 0; i < n; ++i) {
    int k;
    cin >> k;
    if (k) all_zeros = false;
    for (int j = 0; j < k; ++j) {
      int lang;
      cin >> lang;
      --lang;
      speakers[lang].push_back(i);
    }
  }
  for (int i = 0; i < m; ++i) {
    if (speakers[i].size() >= 2) {
      int first = speakers[i][0];
      for (int j = 1; j < speakers[i].size(); ++j) {
        e[first].push_back(speakers[i][j]);
        e[speakers[i][j]].push_back(first);
      }
    }
  }
  int k = 0;
  for (int i = 0; i < n; ++i) {
    if (ind[i] == -1) dfs(i, k++);
  }
  cout << (all_zeros ? k : k - 1);
  return 0;
}
