#include <bits/stdc++.h>
using namespace std;
const long long int maxn = 200;
vector<long long int> adj[maxn];
long long int visited[maxn];
void dfs(long long int s) {
  visited[s] = 1;
  for (auto x : adj[s]) {
    if (!visited[x]) {
      dfs(x);
    }
  }
}
void runcases(long long int T) {
  long long int n, m;
  cin >> n >> m;
  map<long long int, vector<long long int> > mp;
  long long int d = 0;
  for (long long int i = 0; i < n; i++) {
    long long int k;
    cin >> k;
    if (k == 0) {
      d++;
    }
    while (k--) {
      long long int x;
      cin >> x;
      mp[x].push_back(i + 1);
    }
  }
  for (auto x : mp) {
    for (long long int i = 0; i < x.second.size(); i++) {
      for (long long int j = i + 1; j < x.second.size(); j++) {
        adj[x.second[i]].push_back(x.second[j]);
        adj[x.second[j]].push_back(x.second[i]);
      }
    }
  }
  long long int cnt = 0;
  for (long long int i = 1; i <= n; i++) {
    if (!visited[i]) {
      dfs(i);
      cnt++;
    }
  }
  if (cnt == d) {
    cout << d << "\n";
  } else {
    cout << (cnt - 1) << "\n";
  }
}
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  long long int T = 1;
  for (long long int t = 1; t <= T; t++) {
    runcases(t);
  }
  return 0;
}
