#include <bits/stdc++.h>
using namespace std;
int N, M, k, p, h, i, j, a[101];
bool b;
vector<int> V[101];
int find1(int d) {
  if (a[d] == a[a[d]]) {
    return a[d];
  }
  return a[d] = find1(a[d]);
}
void union1(int x, int y) {
  int aa = find1(x), bb = find1(y);
  if (aa != bb) {
    a[aa] = bb;
    h--;
  }
}
int main() {
  cin >> N >> M;
  h = N;
  for (i = 1; i <= N; i++) {
    a[i] = i;
    cin >> k;
    if (k > 0) {
      b = 1;
    }
    for (j = 0; j < k; j++) {
      cin >> p;
      V[p].push_back(i);
    }
  }
  for (i = 1; i <= M; i++) {
    for (j = 1; j < V[i].size(); j++) {
      union1(V[i][j - 1], V[i][j]);
    }
  }
  cout << h - b;
  return 0;
}
