#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 11111;
vector<int> g[MAX_N];
bool was[MAX_N];
void dfs(int u) {
  was[u] = true;
  for (int i = 0; i < g[u].size(); i++) {
    int to = g[u][i];
    if (!was[to]) dfs(to);
  }
}
int main() {
  int n, m, k;
  int lang;
  int comp;
  int sum = 0;
  scanf("%d %d", &n, &m);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &k);
    sum += k;
    for (int j = 0; j < k; j++) {
      scanf("%d", &lang);
      g[lang + n].push_back(i);
      g[i].push_back(lang + n);
    }
  }
  if (sum == 0) {
    printf("%d", n);
    return 0;
  }
  comp = 0;
  for (int i = 1; i <= n; i++)
    if (!was[i]) {
      comp++;
      dfs(i);
    }
  printf("%d", comp - 1);
  return 0;
}
