#include <bits/stdc++.h>
using namespace std;
long long power(long x, long n) {
  if (n == 0) return 1;
  if (n == 1) return x;
  long n2 = n / 2;
  long long po = power(x, n2);
  if (n % 2) return po * po * x;
  return po * po;
}
long long gcd(long long a, long long b) {
  if (b == 0) return a;
  a %= b;
  return gcd(b, a);
}
class CLearningLanguages {
  int A[105];
  void start() {
    int i;
    for (i = 0; i < 105; i++) A[i] = i;
  }
  int parent(int x) {
    if (x == A[x]) return x;
    return A[x] = parent(A[x]);
  }
  void unite(int x, int y) {
    int par_x, par_y;
    par_x = parent(x);
    par_y = parent(y);
    A[par_x] = par_y;
  }

 public:
  void solve(std::istream &cin, std::ostream &cout) {
    int n, m, i, k, j, pre, x, c = 0, z = 0;
    start();
    cin >> n >> m;
    vector<int> V[105];
    for (i = 0; i < n; i++) {
      cin >> k;
      z += (k == 0);
      for (j = 0; j < k; j++) cin >> x, V[x].push_back(i + 1);
    }
    for (i = 0; i < m; i++) {
      if (V[i + 1].empty()) continue;
      pre = V[i + 1][0];
      for (auto it : V[i + 1]) {
        unite(pre, it);
      }
    }
    for (i = 0; i < n; i++) c += A[i + 1] == (i + 1);
    if (z != n) c--;
    cout << c << endl;
  }
};
int main() {
  CLearningLanguages solver;
  std::istream &in(std::cin);
  std::ostream &out(std::cout);
  solver.solve(in, out);
  return 0;
}
