#include <bits/stdc++.h>
using namespace std;
int n, m, k, a, u[101];
void init() {
  for (int i = 0; i <= n; i++) {
    u[i] = -1;
  }
}
int root(int v) {
  int r = v;
  while (u[r] >= 0) {
    r = u[r];
  }
  return r;
}
void merge(int s, int f) {
  int x = root(s), y = root(f);
  if (x != y) {
    if (u[x] < u[y]) {
      u[x] += u[y];
      u[y] = x;
    } else {
      u[y] += u[x];
      u[x] = y;
    }
  }
}
map<int, int> mpp;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(NULL);
  cin >> n >> m;
  init();
  int cntZ = 0;
  for (int i = 1; i <= n; i++) {
    cin >> k;
    if (k == 0) {
      cntZ++;
    }
    for (int j = 0; j < k; j++) {
      cin >> a;
      if (mpp[a] != 0) {
        if (root(mpp[a]) != root(i)) {
          merge(mpp[a], i);
        }
      }
      mpp[a] = i;
    }
  }
  int cnt = 0;
  for (int i = 1; i <= n; i++) {
    if (u[i] < 0) {
      cnt++;
    }
  }
  if (cntZ == n) {
    cout << cntZ;
  } else {
    cout << cnt - 1;
  }
  return 0;
}
