#include <bits/stdc++.h>
using namespace std;
const int maxn = 105;
int m, n;
int k;
vector<int> parent(maxn);
vector<int> freq(maxn);
vector<int> rnk(maxn);
int Find(int x) {
  if (x != parent[x]) parent[x] = Find(parent[x]);
  return parent[x];
}
void Union(int x, int y) {
  int a = Find(x);
  int b = Find(y);
  if (a == b) return;
  if (rnk[a] < rnk[b]) {
    parent[a] = b;
  } else {
    parent[b] = a;
    if (rnk[a] == rnk[b]) rnk[b]++;
  }
}
int main() {
  cin >> n >> m;
  for (int i = 0; i < maxn; i++) {
    parent[i] = i;
  }
  int res = 0;
  for (int i = 0; i < n; i++) {
    cin >> k;
    if (k == 0) {
      res++;
      continue;
    }
    int x;
    cin >> x;
    freq[x]++;
    for (int j = 1; j < k; j++) {
      int y;
      cin >> y;
      freq[y]++;
      Union(x, y);
    }
  }
  int ans = 0;
  for (int i = 1; i <= m; i++) {
    if (parent[i] == i && freq[i] != 0) ans++;
  }
  if (res == n) {
    cout << n << endl;
    return 0;
  }
  if (ans > 0) cout << res + ans - 1 << endl;
  return 0;
}
