#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:256000000")
using namespace std;
int solution();
int main() {
  solution();
  return 0;
}
vector<vector<int> > lang(111), worker(111);
int used[111];
int dfs(int cur, int color) {
  used[cur] = color;
  for (int i = 0; i < (int)worker[cur].size(); i++)
    if (!used[worker[cur][i]]) {
      dfs(worker[cur][i], color);
    }
  return 0;
}
int solution() {
  int n, m;
  cin >> n >> m;
  for (int i = 1; i <= n; i++) {
    int ki;
    scanf("%i", &ki);
    for (int k = 0; k < ki; k++) {
      int tmp;
      scanf("%i", &tmp);
      lang[tmp].push_back(i);
    }
  }
  for (int i = 1; i <= m; i++) {
    for (int j = 0; j < (int)lang[i].size(); j++)
      for (int k = 0; k < (int)lang[i].size(); k++)
        if (j != k) {
          worker[lang[i][k]].push_back(lang[i][j]);
        }
  }
  memset(used, 0, sizeof(used));
  int sum = 0;
  for (int i = 1; i <= m; i++) {
    sum += lang[i].size() == 0;
  }
  if (sum == m) {
    cout << n;
    return 0;
  }
  int color = 1;
  for (int i = 1; i <= n; i++)
    if (used[i] == 0) {
      dfs(i, color++);
    }
  set<int> ans;
  for (int i = 1; i <= n; i++) {
    ans.insert(used[i]);
  }
  cout << (int)ans.size() - 1;
  return 0;
}
