#include <bits/stdc++.h>
using namespace std;
int N, M;
int vis[256], a, b, cnt;
vector<int> v[256];
void dfs(int n) {
  if (vis[n]) return;
  vis[n] = 1;
  for (int i = 0; i < v[n].size(); i += 1) {
    dfs(v[n][i]);
  }
}
int main() {
  cin >> N >> M;
  int flag = 1;
  for (int i = 0; i < N; i += 1) {
    cin >> a;
    for (int j = 0; j < a; j += 1) {
      flag = 0;
      cin >> b;
      v[i].push_back(N + b - 1);
      v[N + b - 1].push_back(i);
    }
  }
  if (flag) {
    cout << N << '\n';
    return 0;
  }
  for (int i = 0; i < N; i += 1) {
    if (!vis[i]) {
      dfs(i);
      cnt++;
    }
  }
  cout << cnt - 1 << '\n';
  return 0;
}
