#include <bits/stdc++.h>
using namespace std;
int a[105][105];
int parent[105];
int find(int x) {
  if (x == parent[x]) return x;
  return (parent[x] = find(parent[x]));
}
void unio(int x, int y) {
  int xp = find(x);
  int yp = find(y);
  if (xp != yp) {
    parent[xp] = yp;
  }
  return;
}
int main() {
  int len, num, i, j, k, l, m, n, t, q, r, cnt;
  t = 1;
  while (t--) {
    cin >> n >> m;
    int flag = 0;
    for (i = 0; i < n; i++) {
      cin >> len;
      for (j = 0; j < len; j++) {
        flag = 1;
        cin >> num;
        a[i][num] = 1;
      }
    }
    if (flag == 0) {
      cout << n << endl;
      continue;
    }
    for (i = 0; i < n; i++) {
      parent[i] = i;
    }
    for (i = 0; i < n; i++) {
      for (j = i + 1; j < n; j++) {
        for (k = 1; k <= m; k++) {
          if (a[i][k] == 1 && a[j][k] == 1) {
            unio(i, j);
            break;
          }
        }
      }
    }
    cnt = 0;
    for (i = 0; i < n; i++) {
      if (parent[i] == i) {
        cnt++;
      }
    }
    cout << cnt - 1 << endl;
  }
}
