#include <bits/stdc++.h>
using namespace std;
struct RTC {};
vector<vector<int> > g;
vector<int> us;
void dfs(int node) {
  for (int i = 0; i < (((int)(g[node]).size())); i++) {
    if (us[g[node][i]] == -1) {
      us[g[node][i]] = 1;
      dfs(g[node][i]);
    }
  }
}
int main() {
  int n, m;
  cin >> n >> m;
  g.resize(n + m + 10);
  us.resize(n + m + 10, -1);
  int nc = 0;
  int ln;
  for (int i = 0; i < (n); i++) {
    int k, a;
    cin >> k;
    if (k == 0) nc++;
    if (k != 0) ln = i;
    for (int j = 0; j < (k); j++) {
      cin >> a;
      g[i].push_back(a + n - 1);
      g[a + n - 1].push_back(i);
    }
  }
  if (nc == n) {
    cout << nc << "\n";
    return 0;
  }
  int sol = 0;
  for (int i = 0; i < (n); i++) {
    if (us[i] == -1) {
      sol++;
      us[i] = 1;
      dfs(i);
    }
  }
  cout << sol - 1 << "\n";
  return 0;
}
