#include <bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7;
vector<int> graph[300];
bool visited[300] = {false};
void dfs(int u) {
  visited[u] = true;
  for (auto v : graph[u]) {
    if (visited[v] == false) dfs(v);
  }
}
void solve() {
  int n, m;
  cin >> n >> m;
  bool flag = true;
  for (int i = 0; i < n; ++i) {
    int x, y;
    cin >> x;
    for (int j = 0; j < x; ++j) {
      cin >> y;
      graph[i].push_back(100 + y);
      graph[100 + y].push_back(i);
      flag = false;
    }
  }
  int ans = 0;
  for (int i = 0; i < n; ++i) {
    if (visited[i] == false) {
      ++ans;
      dfs(i);
    }
  }
  if (flag)
    cout << ans << '\n';
  else
    cout << ans - 1 << '\n';
}
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int t = 1;
  while (t--) {
    solve();
  }
}
