#include <bits/stdc++.h>
using namespace std;
const long long MOD = (long long)1e9;
const int NMAX = (1e6) + 5;
const int INF = 1e7;
const int N = 101;
int parent[N];
int ranks[N] = {0};
int freq[N] = {0};
void make_set(int v) { parent[v] = v; }
int find_set(int v) {
  if (v == parent[v]) return v;
  return find_set(parent[v]);
}
void union_sets(int a, int b) {
  a = find_set(a);
  b = find_set(b);
  if (a != b) parent[b] = a;
}
int find_set_pc(int v) {
  if (v == parent[v]) return v;
  return parent[v] = find_set(parent[v]);
}
void make_set_ubr(int v) {
  parent[v] = v;
  ranks[v] = 0;
}
void union_sets_ubr(int a, int b) {
  a = find_set_pc(a);
  b = find_set_pc(b);
  if (a != b) {
    if (ranks[a] < ranks[b]) swap(a, b);
    parent[b] = a;
    if (ranks[a] == ranks[b]) ranks[a]++;
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int n, m, u, v, k;
  int no_lang = 0, count = 0;
  for (int i = 0; i < N; ++i) {
    make_set_ubr(i);
  }
  cin >> n >> m;
  for (int i = 0; i < n; ++i) {
    cin >> k;
    if (k == 0) {
      no_lang++;
      continue;
    }
    cin >> u;
    freq[u]++;
    for (int j = 1; j < k; j++) {
      cin >> v;
      freq[v]++;
      union_sets_ubr(u, v);
    }
  }
  int ans = 0;
  for (int i = 1; i <= m; i++) {
    if (freq[i] == 0)
      count++;
    else if (parent[i] == i)
      ans++;
  }
  ans -= 1;
  if (count == m)
    cout << n << endl;
  else {
    cout << ans + no_lang << endl;
  }
}
