#include <bits/stdc++.h>
using namespace std;
vector<vector<int> > v, in;
bool vis[500];
void dfs(int s) {
  vis[s] = 1;
  for (int l = 0; l < v[s].size(); l++)
    if (!vis[v[s][l]]) dfs(v[s][l]);
}
int main() {
  int t = 0, z = 0, len;
  int n, k = 0, m = 0, bam = 0, dan = 0, ans = 0;
  scanf("%d %d", &n, &m);
  v.resize(n + 10);
  in.resize(m + 10);
  for (int l = 1; l < n + 1; l++) {
    scanf("%d", &k);
    if (!k) t++;
    while (k--) {
      scanf("%d", &z);
      in[z].push_back(l);
    }
  }
  for (int l = 1; l < m + 1; l++) {
    if (in[l].size()) {
      for (int i = 0; i < in[l].size() - 1; i++) {
        v[in[l][i]].push_back(in[l][i + 1]);
        v[in[l][i + 1]].push_back(in[l][i]);
      }
    }
  }
  ans = 0;
  for (int l = 1; l < n + 1; l++) {
    if (!vis[l]) {
      ans++;
      dfs(l);
    }
  }
  if (t == n)
    printf("%d", n);
  else
    printf("%d", ans - 1);
  return 0;
}
