#include <bits/stdc++.h>
using namespace std;
const int N = 100 + 5;
map<int, int> v[N];
int par[N];
bool flag = true;
void init() {
  for (int i = 0; i < N; ++i) par[i] = i;
}
void input(const int n) {
  int num;
  int lan;
  for (int i = 1; i <= n; ++i) {
    cin >> num;
    if (num > 0) flag = false;
    for (int j = 1; j <= num; ++j) {
      cin >> lan;
      v[i][lan] = 1;
    }
  }
}
int finds(int x) {
  if (x == par[x])
    return x;
  else
    return par[x] = finds(par[x]);
}
void unite(int x, int y) {
  int x1 = finds(x);
  int y1 = finds(y);
  if (x1 != y1) {
    par[y1] = x1;
  }
}
int judge(const int n, const int m) {
  if (flag == true) return n;
  for (int k = 1; k <= m; ++k) {
    for (int i = 1; i <= n; ++i) {
      for (int j = 1; j <= n; ++j) {
        if (v[i][k] == 1 && v[j][k] == 1) {
          unite(i, j);
        }
      }
    }
  }
  int ans = 0;
  for (int i = 1; i <= n; ++i) {
    if (par[i] == i) {
      ++ans;
    }
  }
  return ans - 1;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  int n, m;
  cin >> n >> m;
  init();
  input(n);
  int ans = judge(n, m);
  cout << ans << endl;
  return 0;
}
