#include <bits/stdc++.h>
using namespace std;
int n, m;
void replace(int num1, int num2, int* arr) {
  for (int i = 0; i <= m - 1; i++) {
    if (arr[i] == num1) arr[i] = num2;
  }
  return;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin >> n >> m;
  int c[m];
  memset(c, -1, sizeof(c));
  int counter = 0;
  int temp;
  int ans = 0;
  for (int l = 0; l <= n - 1; l++) {
    int x;
    cin >> x;
    if (!x) {
      ans++;
      continue;
    }
    bool flag = false;
    vector<int> v;
    for (int i = 0; i <= x - 1; i++) {
      int y;
      cin >> y;
      v.push_back(y);
      if (flag) continue;
      if (c[y - 1] != -1) {
        temp = c[y - 1];
        flag = true;
      }
    }
    if (!flag) {
      temp = counter;
      counter++;
    }
    for (int j = 0; j <= v.size() - 1; j++) {
      if (c[v[j] - 1] == -1)
        c[v[j] - 1] = temp;
      else if (c[v[j] - 1] != temp)
        replace(c[v[j] - 1], temp, c);
    }
  }
  set<int> s;
  for (int i = 0; i <= m - 1; i++) {
    if (c[i] != -1) s.insert(c[i]);
  }
  if (s.size() != 0) ans += s.size() - 1;
  cout << ans << endl;
  return 0;
}
