#include <bits/stdc++.h>
using namespace std;
const long long N = 1e3 + 7;
long long sum, n, t, a[N], l, ans, m, k, h;
bool u[N];
int p[N];
int get(int v) {
  if (p[v] == v)
    return v;
  else {
    p[v] = get(p[v]);
    return p[v];
  }
}
int add(int v1, int v2) {
  int g1 = get(v1);
  int g2 = get(v2);
  if (g1 != g2) p[g1] = g2;
}
int main() {
  cin >> n >> m;
  for (long long i = 0; i < m + 1; i++) p[i] = i;
  for (long long i = 0; i < n; i++) {
    cin >> k;
    if (k == 0)
      ans++;
    else
      h = 1;
    for (long long j = 0; j < k; j++) {
      cin >> a[j];
      u[a[j]] = true;
    }
    for (long long j = 0; j < k - 1; j++)
      for (long long ii = j + 1; ii <= k - 1; ii++) add(a[j], a[ii]);
  }
  for (long long i = 0; i < m + 1; i++)
    if (p[i] == i && u[i]) ans++;
  cout << ans - h;
  return 0;
}
