#include <bits/stdc++.h>
using namespace std;
int n, m;
vector<int> lang[105];
vector<int> adj[105];
int group[105];
int cnt = 0;
void dfs(int u) {
  group[u] = cnt;
  int sz = adj[u].size();
  for (int i = 0; i < sz; i++) {
    int v = adj[u][i];
    if (!group[v]) {
      dfs(v);
    }
  }
}
int main() {
  int edgecase = 0;
  int s, u;
  scanf("%d %d", &n, &m);
  for (int i = 0; i < n; i++) {
    scanf("%d", &s);
    for (int j = 0; j < s; j++) {
      edgecase = 1;
      scanf("%d", &u);
      lang[u].push_back(i + 1);
    }
  }
  for (int i = 1; i <= m; i++) {
    int sz = lang[i].size();
    for (int j = 0; j < sz; j++) {
      for (int k = 0; k < sz; k++) {
        if (j != k) {
          adj[lang[i][j]].push_back(lang[i][k]);
          adj[lang[i][k]].push_back(lang[i][j]);
        }
      }
    }
  }
  for (int i = 1; i <= n; i++) {
    if (!group[i]) {
      cnt++;
      dfs(i);
    }
  }
  if (edgecase == 0)
    printf("%d\n", n);
  else
    printf("%d\n", cnt - 1);
  return 0;
}
