#include <bits/stdc++.h>
const int MOD = 1e9 + 7;
const int v1 = 1e5 + 5;
const int N = 3e5 + 5;
using namespace std;
vector<vector<long long>> adj(500);
vector<bool> visited(500);
long long m = 0;
void dfs(long long v) {
  visited[v] = true;
  for (auto i : adj[v])
    if (!visited[i]) dfs(i);
}
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  long long n = 0;
  long long x, y;
  bool flag = true;
  long long ans = 0;
  cin >> n >> m;
  for (long long i = 0; i < n; i++) {
    cin >> x;
    if (x == 0) ans++;
    for (long long j = 0; j < x; j++) {
      flag = false;
      cin >> y;
      adj[y].push_back(i + m + 1);
      adj[i + m + 1].push_back(y);
    }
  }
  if (flag) ans++;
  for (long long i = 1; i <= n + m; i++) {
    if (!visited[i] && adj[i].size() != 0) {
      dfs(i);
      ans++;
    }
  }
  cout << ans - 1 << "\n";
}
