#include <bits/stdc++.h>
using namespace std;
const int N = 110;
int n, m, k, ans, a;
vector<int> adj[N], lan[N];
bool mark[N];
void input() {
  cin >> n >> m;
  for (int i = 1; i <= n; i++) {
    cin >> k;
    if (k == 0) a++;
    while (k--) {
      int v;
      cin >> v;
      lan[v].push_back(i);
    }
  }
}
void aa_adj() {
  for (int i = 1; i <= m; i++) {
    if (lan[i].size() == 0) continue;
    for (int j = 0; j < lan[i].size() - 1; j++) {
      adj[lan[i][j]].push_back(lan[i][j + 1]),
          adj[lan[i][j + 1]].push_back(lan[i][j]);
    }
  }
}
void dfs(int v) {
  mark[v] = true;
  for (int u : adj[v])
    if (!mark[u]) dfs(u);
}
void find_ans() {
  for (int i = 1; i <= n; i++) {
    if (!mark[i]) {
      dfs(i), ans++;
    }
  }
  if (a == n)
    cout << ans << endl;
  else
    cout << ans - 1 << endl;
}
int main() {
  input();
  aa_adj();
  find_ans();
}
