#include <bits/stdc++.h>
using namespace std;
template <class T>
inline T sqr(T x) {
  return x * x;
}
double dist(double x1, double y1, double x2, double y2) {
  return sqrt(sqr(x1 - x2) + sqr(y1 - y2));
}
template <class T>
inline T lcm(T a, T b) {
  if (a < 0) return lcm(-a, b);
  if (b < 0) return lcm(a, -b);
  return a * (b / __gcd(a, b));
}
template <class T>
T power(T N, T P) {
  return (P == 0) ? 1 : N * power(N, P - 1);
}
template <class T>
string itoa(T a) {
  if (!a) return "0";
  string ret;
  for (T i = a; i > 0; i = i / 10) ret.push_back((i % 10) + 48);
  reverse(ret.begin(), ret.end());
  return ret;
}
double log(double N, double B) { return (log10l(N)) / (log10l(B)); }
vector<string> token(char *a, char *b) {
  vector<string> v;
  char *p = strtok(a, " ");
  while (p) {
    v.push_back(p);
    p = strtok(NULL, " ");
  }
  return v;
}
vector<string> token(string a, char *b) {
  char temp[100002];
  strcpy(temp, a.c_str());
  return token(temp, b);
}
string toBin(int n) {
  string s;
  for (int i = 10 - 1; i >= 0; i--) s += ((bool)(n & (1 << i)) + '0');
  return s;
}
int fx[] = {0, +0, +0, +1, -1, -1, +1, -1, +1};
int fy[] = {0, -1, +1, +0, +0, +1, +1, -1, -1};
set<int> ms[2000];
int pr[2000];
vector<int> sav[2000];
int find(int r) {
  if (pr[r] == r) return r;
  return pr[r] = find(pr[r]);
}
int main() {
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < (int)1000; i++) pr[i] = i;
  int zero = 1;
  for (int i = 0; i < (int)n; i++) {
    int k;
    scanf("%d", &k);
    if (k) zero = 0;
    for (int j = 0; j < (int)k; j++) {
      int x;
      scanf("%d", &x);
      sav[i].push_back(x);
      ms[i].insert(x);
    }
  }
  if (zero) {
    printf("%d\n", n);
    return 0;
  }
  for (int i = 0; i < (int)n; i++) {
    for (int j = 0; j < (int)n; j++) {
      if (i == j) continue;
      int found = 0;
      for (int k = 0; k < (int)(int)sav[i].size(); k++) {
        int x = sav[i][k];
        if (ms[j].find(x) != ms[j].end()) {
          found = 1;
          break;
        }
      }
      if (found) {
        if (find(i) != find(j)) pr[find(i)] = find(j);
      }
    }
  }
  set<int> ss;
  for (int i = 0; i < (int)n; i++) ss.insert(find(i));
  cout << (int)ss.size() - 1 << endl;
  return 0;
}
