#include <bits/stdc++.h>
using namespace std;
vector<bool> V(1005);
vector<vector<int>> adj(2005);
int F[1005];
int get(int x) { return x == F[x] ? x : F[x] = get(F[x]); }
void initialize(int n) {
  for (int i = 1; i <= n; ++i) F[i] = i;
}
int main() {
  int n, m, c = 0;
  cin >> n >> m;
  initialize(m);
  for (int i = 0; i < n; ++i) {
    int A[1005], k;
    cin >> k;
    if (!k) ++c;
    for (int j = 1; j <= k; ++j) cin >> A[j], V[A[j]] = 1;
    for (int j = 1; j < k; ++j) F[get(A[j])] = get(A[j + 1]);
  }
  if (c == n) ++c;
  for (int i = 1; i <= m; ++i)
    if (V[i] && i == F[i]) ++c;
  cout << c - 1;
}
