#include <bits/stdc++.h>
using namespace std;
int n, m;
int know[128][128];
int g[128][128];
int vst[128] = {0};
void dfs(int u) {
  vst[u] = 1;
  for (int v = 1; v <= n; ++v) {
    if (g[u][v] && vst[v] == 0) {
      dfs(v);
    }
  }
}
int num[128];
int main() {
  cin >> n >> m;
  for (int i = 1; i <= n; ++i) {
    int k;
    cin >> num[i];
    for (int j = 1; j <= num[i]; ++j) {
      int l;
      cin >> l;
      know[i][l] = 1;
    }
  }
  for (int k = 1; k <= m; ++k) {
    for (int v = 1; v <= n; ++v) {
      if (know[v][k]) {
        for (int u = v + 1; u <= n; ++u) {
          if (know[u][k]) {
            g[u][v] = g[v][u] = 1;
          }
        }
      }
    }
  }
  int cnt0 = 0, cnt1 = 0;
  for (int i = 1; i <= n; ++i) {
    if (vst[i] == 0) {
      num[i] ? ++cnt1 : ++cnt0;
      dfs(i);
    }
  }
  cout << cnt0 + (cnt1 ? cnt1 - 1 : 0) << endl;
  return 0;
}
