#include <bits/stdc++.h>
using namespace std;
vector<int> flag(1000, 0);
vector<int> mass[1000];
void dfs(int x, int col) {
  flag[x] = col;
  for (int i = 0; i < mass[x].size(); i++) {
    if (!flag[mass[x][i]]) {
      dfs(mass[x][i], col);
    }
  }
}
int main() {
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    int a;
    cin >> a;
    for (int j = 0; j < a; j++) {
      int t;
      scanf("%d", &t);
      t--;
      mass[i].push_back(n + t);
      mass[n + t].push_back(i);
    }
  }
  int col = 1;
  bool f = true;
  for (int i = 0; i < n; i++) {
    if (mass[i].size() == 0) {
      col++;
      continue;
    }
    if (flag[i] == 0) {
      dfs(i, col);
      col++;
      f = false;
    }
  }
  cout << col - 2 + int(f);
  return 0;
}
