#include <bits/stdc++.h>
using namespace std;
const int MOD = (int)1e9 + 7, MAX = (int)1e5 + 10;
vector<int> v[MAX];
int n, m, s, x, vis[MAX];
void dfs(int nod) {
  if (!vis[nod]) {
    vis[nod] = 1;
    for (auto it : v[nod]) {
      if (vis[it]) continue;
      dfs(it);
    }
  }
}
int main() {
  bool o = 0;
  cin >> n >> m;
  for (int i = 1; i <= n; i++) {
    cin >> s;
    if (s) o = 1;
    for (int j = 0; j < s; j++) {
      cin >> x;
      v[i].push_back(x * 100 + 1);
      v[x * 100 + 1].push_back(i);
    }
  }
  int sum = 0;
  for (int i = 1; i <= n; i++) {
    if (!vis[i]) {
      dfs(i);
      sum++;
    }
  }
  cout << sum - (o);
}
