#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 1;
vector<int> per[N];
vector<int> lang[N];
bool vis[N];
vector<int> ans;
void init() {
  for (int i = 0; i < 100001; i++) vis[i] = false;
}
void dfs(int r) {
  vis[r] = true;
  for (auto x : per[r]) {
    for (auto y : lang[x]) {
      if (!vis[y]) dfs(y);
    }
  }
}
void solve() {
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    int k;
    cin >> k;
    for (int j = 0; j < k; j++) {
      int l;
      cin >> l;
      lang[l].push_back(i);
      per[i].push_back(l);
    }
  }
  int f = 0;
  for (int i = 0; i < n; i++) {
    if (per[i].size() != 0) f = 1;
  }
  if (!f)
    cout << n;
  else {
    int c = 0;
    for (int i = 0; i < n; i++) {
      if (!vis[i]) {
        dfs(i);
        c++;
      }
    }
    cout << c - 1;
  }
}
int main() {
  init();
  int t = 1;
  while (t--) {
    solve();
  }
}
