#include <bits/stdc++.h>
using namespace std;
struct DSU {
  vector<int> pai, sz;
  DSU(int n) {
    pai.resize(n + 2);
    sz.resize(n + 2);
    for (int i = 0; i <= n; i++) pai[i] = i, sz[i] = 1;
  }
  int find(int x) { return pai[x] == x ? x : pai[x] = find(pai[x]); }
  void join(int x, int y) {
    x = find(x);
    y = find(y);
    if (x == y) return;
    if (sz[x] > sz[y]) swap(x, y);
    pai[x] = y;
    sz[y] += sz[x];
  }
  bool same(int x, int y) { return find(x) == find(y); }
};
int n, q;
int relevant[102];
int main() {
  cin >> n >> q;
  DSU dsu(q);
  int cnt = 0;
  for (int i = 0; i < n; i++) {
    int k, w;
    cin >> k;
    if (!k) cnt++;
    int last = -1;
    for (int j = 0; j < k; j++) {
      cin >> w;
      relevant[w] = 1;
      if (j) dsu.join(w, last);
      last = w;
    }
  }
  set<int> ans;
  for (int i = 1; i <= q; i++) {
    if (relevant[i]) {
      ans.insert(dsu.find(i));
    }
  }
  int one = ans.size() ? 1 : 0;
  cout << ans.size() + cnt - one << '\n';
  return 0;
}
