#include <bits/stdc++.h>
using namespace std;
map<long long, vector<long long> > q;
map<long long, bool> used;
long long a = 0, x = 0, z = 0;
void f(long long u) {
  used[u] = true;
  a++;
  if (q[u].size() == 0) {
    z++;
  }
  for (long long i = 0; i < q[u].size(); i++) {
    for (long long j = 0; j < q[q[u][i]].size(); j++) {
      if (!used[q[q[u][i]][j]]) {
        f(q[q[u][i]][j]);
      }
    }
  }
}
int main() {
  ios::sync_with_stdio(false);
  long long n, m;
  cin >> n >> m;
  for (long long i = 0; i < n; i++) {
    long long k;
    cin >> k;
    vector<long long> z(k);
    for (long long j = 0; j < k; j++) {
      cin >> z[j];
      q[-(i + 1)].push_back(z[j]);
      q[z[j]].push_back(-(i + 1));
    }
  }
  for (long long i = 1; i <= n; i++) {
    if (!used[-i]) {
      a = 0;
      f(-i);
      x++;
    }
  }
  if (z == n) {
    cout << z;
  } else {
    cout << x - 1;
  }
  return 0;
}
