#include <bits/stdc++.h>
using namespace std;
const long long int MOD = 1e9 + 7;
vector<long long int> king;
long long int find(long long int a) {
  if (king[a] != a) {
    king[a] = find(king[a]);
    return king[a];
  } else {
    return a;
  }
}
long long int merge(long long int a, long long int b) { king[b] = king[a]; }
void solve() {
  long long int n, a, m, k, b;
  cin >> n >> m;
  king.resize(m + 2);
  vector<long long int> vis(m + 2, 0);
  for (long long int i = 1; i <= m; i++) {
    king[i] = i;
  }
  long long int zero = 0;
  for (long long int i = 0; i < n; i++) {
    cin >> k;
    if (k == 0) {
      zero++;
      continue;
    }
    cin >> a;
    vis[a] = 1;
    long long int p = find(a);
    for (long long int j = 0; j < k - 1; j++) {
      cin >> b;
      vis[b] = 1;
      long long int q = find(b);
      if (p != q) {
        merge(p, q);
      }
    }
  }
  long long int ans = 0;
  for (long long int i = 1; i <= m; i++) {
    if (vis[i] == 0) {
      continue;
    }
    a = find(i);
    if (a == i) {
      ans++;
    }
  }
  if (ans != 0) {
    ans--;
  }
  cout << ans + zero << endl;
}
int main(void) {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  solve();
  return 0;
}
