#include <bits/stdc++.h>
using namespace std;
void fast() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
}
vector<int> adj[10005];
int visited[10005] = {0};
void dfs(int node) {
  visited[node] = 1;
  for (int next : adj[node]) {
    if (!visited[next]) {
      dfs(next);
    }
  }
}
int main() {
  fast();
  int n, m;
  cin >> n >> m;
  vector<int> cnt[m];
  int g = 0;
  for (int i = 0; i < n; i++) {
    int k;
    cin >> k;
    for (int j = 0; j < k; j++) {
      int l;
      cin >> l;
      --l;
      cnt[l].push_back(i);
      g = 1;
    }
  }
  map<pair<int, int>, int> mt;
  for (int i = 0; i < m; i++) {
    vector<int> time = cnt[i];
    for (int j = 0; j < time.size(); j++) {
      for (int k = j + 1; k < time.size(); k++) {
        if (!mt[make_pair(time[j], time[k])]) {
          adj[time[j]].push_back(time[k]);
          adj[time[k]].push_back(time[j]);
          mt[make_pair(time[j], time[k])]++;
        }
      }
    }
  }
  int ans = 0;
  for (int i = 0; i < n; i++) {
    if (!visited[i]) {
      ans++;
      dfs(i);
    }
  }
  if (!g) {
    cout << n << "\n";
  } else {
    cout << ans - 1 << "\n";
  }
}
