#include <bits/stdc++.h>
using namespace std;
long long n, m, vis[102];
vector<long long> lang[102], e[102];
void dfs(long long u) {
  vis[u] = 1;
  for (auto i : e[u]) {
    if (!vis[i]) dfs(i);
  }
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> m;
  for (long long i = 1; i <= n; i++) {
    long long t, x;
    cin >> t;
    for (long long j = 0; j < t; j++) cin >> x, lang[x].push_back(i);
  }
  bool ok = false;
  for (long long i = 1; i <= m; i++) {
    if (!lang[i].empty()) ok = true;
    for (long long j = 0; j < (long long)lang[i].size(); j++) {
      for (long long k = j + 1; k < (long long)lang[i].size(); k++) {
        e[lang[i][j]].push_back(lang[i][k]);
        e[lang[i][k]].push_back(lang[i][j]);
      }
    }
  }
  if (!ok) return cout << n << endl, 0;
  long long comps = 0;
  for (long long i = 1; i <= n; i++) {
    if (!vis[i]) dfs(i), comps++;
  }
  cout << comps - 1 << endl;
  return 0;
}
