#include <bits/stdc++.h>
using namespace std;
void checkpoint1() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
}
vector<long long> adj[105];
void dfs(long long v, bool vis[]) {
  vis[v] = 1;
  for (auto u : adj[v]) {
    if (!vis[u]) dfs(u, vis);
  }
}
int main() {
  checkpoint1();
  long long n, m;
  cin >> n >> m;
  bool vis[m + 1];
  memset(vis, 1, sizeof(vis));
  long long ans = 0;
  for (long long i = 0; i < n; i++) {
    long long k;
    cin >> k;
    if (!k)
      ans++;
    else {
      long long a[k];
      for (long long j = 0; j < k; j++) {
        cin >> a[j];
        vis[a[j]] = 0;
      }
      for (long long j = 0; j < k; j++) {
        for (long long o = j + 1; o < k; o++) {
          adj[a[j]].push_back(a[o]);
          adj[a[o]].push_back(a[j]);
        }
      }
    }
  }
  if (ans == n) {
    cout << n;
    return 0;
  }
  ans--;
  for (long long i = 1; i <= m; i++) {
    if (!vis[i]) {
      ans++;
      dfs(i, vis);
    }
  }
  cout << ans;
  return 0;
}
