#include <bits/stdc++.h>
using namespace std;
long long vis[1009];
vector<long long> v[1000];
long long th, n;
void dfs(long long s) {
  if (vis[s]) return;
  vis[s] = 1;
  for (long long i = 0; i < v[s].size(); i++) {
    if (s <= n) th++;
    dfs(v[s][i]);
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  long long i, j, k, m, l, a, prothom = 1, kake, ans = 0;
  cin >> n >> m;
  for (i = 1; i <= n; i++) {
    cin >> k;
    while (k--) {
      cin >> a;
      v[a + n].push_back(i);
      v[i].push_back(a + n);
    }
  }
  for (i = 1; i <= n; i++) {
    if (vis[i] == 0) {
      ans++;
      dfs(i);
    }
  }
  if (ans == n && th == 0)
    cout << n << endl;
  else
    cout << ans - 1 << endl;
}
