#include <bits/stdc++.h>
using namespace std;
vector<vector<int> > lang(105);
vector<vector<int> > emp(105);
vector<int> visited(105, false);
int n, m;
void dfs(int i) {
  if (!visited[i]) {
    visited[i] = true;
    int n_lang = emp[i].size();
    for (int k = 0; k < n_lang; k++) {
      int lg = emp[i][k];
      int n_emp = lang[lg].size();
      for (int x = 0; x < n_emp; x++) {
        dfs(lang[lg][x]);
      }
    }
  }
}
int main() {
  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    int n_l;
    cin >> n_l;
    for (int j = 0; j < n_l; j++) {
      int lg;
      cin >> lg;
      lg--;
      lang[lg].push_back(i);
      emp[i].push_back(lg);
    }
  }
  bool flag = false;
  for (int i = 0; i < m; i++) {
    if (lang[i].size() != 0) {
      flag = true;
      break;
    }
  }
  if (flag == false) {
    cout << n << endl;
    return 0;
  }
  int cc = 0;
  for (int i = 0; i < n; i++) {
    if (!visited[i]) {
      cc++;
      dfs(i);
    }
  }
  cout << cc - 1 << endl;
}
