#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1000000007;
vector<long long> parent(101);
vector<long long> size(101);
long long find_set(long long v) {
  if (v == parent[v]) return v;
  parent[v] = find_set(parent[v]);
  return parent[v];
}
void union_sets(long long a, long long b) {
  a = find_set(a);
  b = find_set(b);
  if (a != b) parent[b] = a;
}
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  long long n, m;
  cin >> n >> m;
  for (long long i = 0; i <= m; i++) parent[i] = i;
  long long z = 0;
  map<long long, long long> lang;
  for (long long i = 0; i < n; i++) {
    long long l;
    cin >> l;
    if (l == 0) {
      z++;
      continue;
    }
    long long a;
    cin >> a;
    long long b;
    lang[a]++;
    for (long long i = 1; i < l; i++) {
      cin >> b;
      lang[b]++;
      union_sets(a, b);
    }
  }
  long long ans = 0;
  map<long long, long long> make_pair;
  for (long long i = 1; i <= m; i++) {
    if (lang[i] == 0) continue;
    long long a = find_set(i);
    if (make_pair[a] == 0) {
      ans++;
    }
    make_pair[a]++;
  }
  if (ans == 0) z++;
  cout << ans - 1 + z;
  return 0;
}
