#include <bits/stdc++.h>
using namespace std;
int mat[101][101];
bool vis[101];
int main() {
  int i, j, n, m, k, x;
  cin >> n >> m;
  for (i = 1; i <= n; i++) {
    for (j = 1; j <= m; j++) {
      mat[i][j] = 0;
    }
    vis[i] = false;
  }
  bool flag = false;
  for (i = 1; i <= n; i++) {
    cin >> k;
    if (k > 0) flag = true;
    while (k--) {
      cin >> x;
      mat[i][x] = 1;
    }
  }
  queue<int> q;
  int count = 0;
  for (i = 1; i <= n; i++) {
    if (!vis[i]) {
      count++;
      q.push(i);
      vis[i] = true;
      while (!q.empty()) {
        int cur = q.front();
        q.pop();
        for (j = 1; j <= m; j++) {
          if (mat[cur][j] == 1) {
            for (k = 1; k <= n; k++) {
              if (mat[k][j] == 1 && !vis[k]) {
                vis[k] = true;
                q.push(k);
              }
            }
          }
        }
      }
    }
  }
  if (flag)
    cout << count - 1 << endl;
  else
    cout << count << endl;
  return 0;
}
