#include <bits/stdc++.h>
using namespace std;
using ll = long long;
bool visited[100010];
vector<ll> vec[100010];
ll ar[100010], cnt = 0;
void dfs(ll indx) {
  visited[indx] = true;
  for (auto i : vec[indx]) {
    if (!visited[i]) dfs(i);
  }
}
int main() {
  ios::sync_with_stdio(false), cin.tie(0);
  ll n, m;
  while (cin >> n >> m) {
    for (ll i = 0; i < 100010; i++) {
      visited[i] = false;
      vec[i].clear();
    }
    bool flag = false;
    for (ll i = 0; i < n; i++) {
      ll k;
      cin >> k;
      if (k) flag = true;
      while (k--) {
        ll child;
        cin >> child;
        vec[child - 1].push_back(m + i);
        vec[m + i].push_back(child - 1);
      }
    }
    for (ll i = m; i < n + m; i++) {
      if (visited[i]) continue;
      cnt++;
      dfs(i);
    }
    if (flag) cnt--;
    cout << cnt << endl;
  }
}
