#include <bits/stdc++.h>
using namespace std;
const int INF = 1000000000;
const long long MOD = 12345678910;
const long double eps = 1e-13;
const int imax = INT_MAX;
int n, m;
vector<vector<int> > g;
vector<bool> used;
void dfs(int v) {
  used[v] = 1;
  for (int i = 0; i < g[v].size(); i++)
    if (!used[g[v][i]]) dfs(g[v][i]);
}
int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(0);
  cin >> n >> m;
  g.resize(n + m);
  used.resize(n + m, 0);
  int kk = 0;
  for (int i = 0; i < n; i++) {
    int k;
    cin >> k;
    if (k == 0) kk++;
    for (int j = 0; j < k; j++) {
      int a;
      cin >> a;
      a--;
      g[i].push_back(a + n);
      g[a + n].push_back(i);
    }
  }
  int ans = 0;
  for (int i = 0; i < n; i++)
    if (!used[i]) {
      ans++;
      dfs(i);
    }
  if (kk == n)
    cout << ans;
  else
    cout << ans - 1;
  return 0;
}
