The "BerCorp" company has got *n* employees. These employees can use *m* approved official languages for the formal correspondence. The languages are numbered with integers from 1 to *m*. For each employee we have the list of languages, which he knows. This list could be empty, i. e. an employee may know no official languages. But the employees are willing to learn any number of official languages, as long as the company pays their lessons. A study course in one language for one employee costs 1 berdollar.

Find the minimum sum of money the company needs to spend so as any employee could correspond to any other one (their correspondence can be indirect, i. e. other employees can help out translating).
The first line contains two integers *n* and *m* (2 ≤ *n*, *m* ≤ 100) — the number of employees and the number of languages.

Then *n* lines follow — each employee's language list. At the beginning of the *i*-th line is integer *k*<sub class="lower-index">*i*</sub> (0 ≤ *k*<sub class="lower-index">*i*</sub> ≤ *m*) — the number of languages the *i*-th employee knows. Next, the *i*-th line contains *k*<sub class="lower-index">*i*</sub> integers — *a*<sub class="lower-index">*ij*</sub> (1 ≤ *a*<sub class="lower-index">*ij*</sub> ≤ *m*) — the identifiers of languages the *i*-th employee knows. It is guaranteed that all the identifiers in one list are distinct. Note that an employee may know zero languages.

The numbers in the lines are separated by single spaces.
# imports
import sys
from os import path
from collections import *
from itertools import *
import heapq
from math import *
from typing import List
import re
mod = 1e9+7
oo = float("inf")

# IO
if (path.exists('cf')):
    sys.stdin = open("in.txt", "r")
    sys.stdout = open("out.txt", "w")


class Solution:
    def minCostOfMoney(self) -> int:
        n, m = map(int, input().split())

        dsu = [-1]*(m+1)
        def find(u): return u if dsu[u] < 0 else find(dsu[u])

        def union(u, v):
            u, v = find(u), find(v)
            if u == v:
                return
            if dsu[u] > dsu[v]:
                u, v = v, u
            dsu[v] += dsu[u]
            dsu[u] = v

        zeros, componants = 0, n
        graph = {}
        # graph here is a map from language to person (bipartite graph)
        for i in range(n):
            lang = list(map(int, input().split()))
            if len(lang) == 1:  # lang[0] = 0 (no languages)
                zeros += 1
            for j in range(1, len(lang)):
                if lang[j] in graph:
                    u, v = i, graph[lang[j]]
                    if find(u) != find(v):
                        union(u, v)
                        componants -= 1
                else:
                    graph[lang[j]] = i
        # print(componants)
        # R: connecting betwwen them is the cost
        return max(componants-1, zeros)


"""
at the beginning, I thought that the answer is edges between compntants + zeros. but let's say that:
we have n components hence there are (compnants - 1) edges and cost of each edge is 1.
We need the minimum cost, so we can assign some languages to those people who know nothing;
and those languages can be part of existing components.
Remember we don't care to assign all languages, we care that all emps can talk to each other so we will pick smth spoken already.
and hence we either connect unconected components or we assign some commmon language to emps who know nothing.
if the case is that we have both, then we can connect those unconected components and keep assigning some common languages to emps (zeros).
only one language mentioned before with any emp is enough, and we assume that we assign that language to all zeros emps.
"""


def ezz() -> None:
    t = 1
    # t = int(input().strip())
    sol = Solution()
    while (t):
        t -= 1
        print(sol.minCostOfMoney())


# driver code
if '__main__':
    ezz()
