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).


-----Input-----

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_{i} (0 ≤ k_{i} ≤ m) — the number of languages the i-th employee knows. Next, the i-th line contains k_{i} integers — a_{ij} (1 ≤ a_{ij} ≤ 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.


-----Output-----

Print a single integer — the minimum amount of money to pay so that in the end every employee could write a letter to every other one (other employees can help out translating).


-----Examples-----
Input
5 5
1 2
2 2 3
2 3 4
2 4 5
1 5

Output
0

Input
8 7
0
3 1 2 3
1 1
2 5 4
2 6 7
1 3
2 7 4
1 1

Output
2

Input
2 2
1 2
0

Output
1



-----Note-----

In the second sample the employee 1 can learn language 2, and employee 8 can learn language 4.

In the third sample employee 2 must learn language 2.

Difficulty: competition

Starter code:

Solutions:
rd = lambda: list(map(int, input().split()))

def root(x):
  if f[x]!=x: f[x] = root(f[x])
  return f[x]

n, m = rd()
N = list(range(n))
f = list(N)
lang = [0]*n
for i in N: lang[i] = set(rd()[1:])
for i in N:
  for j in N[:i]:
    rj = root(j)
    if lang[rj].intersection(lang[i]):
      f[rj] = i
      lang[i] = lang[i].union(lang[rj])
print(sum(1 for i in N if i==root(i)) - (sum(map(len, lang))>0))


rd = lambda: list(map(int, input().split()))

def root(x):
  if f[x]!=x: f[x] = root(f[x])
  return f[x]

n, m = rd()
N = list(range(n))
f = list(N)
lang = [0]*n
for i in N: lang[i] = set(rd()[1:])
for i in N:
  for j in N[:i]:
    if j==root(j) and lang[j].intersection(lang[i]):
      f[j] = i
      lang[i] = lang[i].union(lang[j])
print(sum(1 for i in N if i==root(i)) - (sum(map(len, lang))>0))


rd = lambda: list(map(int, input().split()))

def root(x):
  if f[x]!=x: f[x] = root(f[x])
  return f[x]

n, m = rd()
N = list(range(n))
f = list(N)
lang = [None]*n
for i in N: lang[i] = set(rd()[1:])
for i in N:
  for j in N[:i]:
    if j==root(j) and lang[j].intersection(lang[i]):
      f[j] = i
      lang[i] = lang[i].union(lang[j])
print(sum(1 for i in N if i==root(i)) - (sum(map(len, lang))>0))


n, m = map(int, input().split())
q = [[] for i in range(n + 1)]
r, s = [0] * (m + 1), 0
for i in range(1, n + 1):
    t = list(map(int, input().split()))
    if t[0]:
        t = t[1: ]
        d = set([r[j] for j in t])
        if 0 in d: d.remove(0)
        if len(d):
            for j in d:
                for k in q[j]:
                    r[k] = i
                q[i].extend(q[j])
                q[j] = []
            t = [j for j in t if r[j] == 0]
            for k in t:
                r[k] = i
            q[i].extend(t)
        else:
            for k in t:
                r[k] = i
            q[i] = t          
    else: s += 1
print(s + max(sum(len(i) > 0 for i in q) - 1, 0))

v0 = 0
l = []
n, m = (int(x) for x in input().split())
for i in range(n):
    l2 = []
    curs = set([int(x) for x in input().split()][1:])
    if not curs:
        v0 += 1
        continue
    for s in l:
        if curs & s:
            curs |= s
        else:
            l2.append(s)
    l2.append(curs)
    l = l2
print(v0 + max(0, len(l) - 1))


def recursive_dfs(graph, start, path=[]):
  '''recursive depth first search from start'''
  path=path+[start]
  for node in graph[start]:
    if not node in path:
      path=recursive_dfs(graph, node, path)
  return path

def connected(x, y):
    if set(x).intersection(set(y)) != set():
        return True
    return False

vertices = []
each = {}
adj = {}
num0 = 0
a, b = list(map(int, input().split(' ')))
for i in range(a):
    x = list(map(int, input().split(' ')))
    adj[i] = []
    if x[0] == 0:
        num0 += 1
    else:
        x = x[1:]
        each[i] = x
        vertices.append(i)

        for j in vertices:
            if connected(each[j], each[i]):
                adj[j].append(i)
                adj[i].append(j)
            
marked = [False] * a
num = 0
for i in vertices:
  if not marked[i]:
    for j in recursive_dfs(adj, i):
      marked[j] = True
    num += 1

if num == 0:
    print(num0)
else:
    print(num+num0-1)


n, m = [int(x) for x in input().split()]
l = [[]] * n
for i in range(n):
	l[i] = set(int(x) for x in input().split()[1:])
while True:
	for i in range(len(l)):
		for j in range(i):
			if l[i] & l[j]:
				l[j] |= l[i]
				l = l[:i] + l[i + 1:]
				break
		else:
			continue
		break
	else:
		break
ans = len(l)
for s in l:
	if s:
		ans -= 1
		break
print(ans)


def main():
    def root(a):
        b = f[a]
        while b != a:
            a, b = b, f[b]
        return a

    n, m = list(map(int, input().split()))
    f = list(range(n))
    lang = []
    for _ in range(n):
        lang.append(set(list(map(int, input().split()[1:]))))
    for i in range(n):
        for j in range(i):
            if j == root(j) and lang[j].intersection(lang[i]):
                f[j] = i
                lang[i].update(lang[j])
    print(sum(i == root(i) for i in range(n)) - any(lang))


def __starting_point():
    main()

__starting_point()

def main():
    def root(a):
        b = f[a]
        while b != a:
            a, b = b, f[b]
        return a

    n, m = list(map(int, input().split()))
    f = list(range(n))
    lang = []
    for _ in range(n):
        lang.append(set(map(int, input().split()[1:])))
    for i in range(n):
        for j in range(i):
            if j == root(j) and lang[j].intersection(lang[i]):
                f[j] = i
                lang[i].update(lang[j])
    print(sum(i == root(i) for i in range(n)) - any(lang))


def __starting_point():
    main()

__starting_point()

def main():
    def root(a):
        b = f[a]
        while b != a:
            a, b = b, f[b]
        return a

    n, m = list(map(int, input().split()))
    f = list(range(n))
    lang = []
    for _ in range(n):
        ll = [False] * m
        for i in map(int, input().split()[1:]):
            ll[i - 1] = True
        lang.append(ll)
    for i, langi in enumerate(lang):
        for j in range(i):
            if j == root(j) and any(a and b for a, b in zip(lang[i], lang[j])):
                f[j] = i
                for _, flag in enumerate(lang[j]):
                    langi[_] |= flag
    print(sum(i == root(i) for i in range(n)) - any(map(any, lang)))


def __starting_point():
    main()

__starting_point()

def main():
    def root(a):
        b = f[a]
        while b != a:
            a, b = b, f[b]
        return a

    n, m = list(map(int, input().split()))
    f = list(range(n))
    lang = []
    for _ in range(n):
        lang.append(set(map(int, input().split()[1:])))
    for i, langi in enumerate(lang):
        for j in range(i):
            if j == root(j) and lang[j].intersection(langi):
                f[j] = i
                langi.update(lang[j])
    print(sum(i == root(i) for i in range(n)) - any(lang))


def __starting_point():
    main()

__starting_point()

def main():
    def root(x):
        a, b = x, f[x]
        while b != a:
            a, b = b, f[b]
        return a == x

    n, m = list(map(int, input().split()))
    f = list(range(n))
    lang = []
    for _ in range(n):
        lang.append(set(map(int, input().split()[1:])))
    for i, langi in enumerate(lang):
        for j in range(i):
            if root(j) and lang[j].intersection(langi):
                f[j] = i
                langi.update(lang[j])
    print(sum(root(i) for i in range(n)) - any(lang))


def __starting_point():
    main()

__starting_point()

def main():
    def root(x):
        a, b = x, f[x]
        while b != a:
            a, b = b, f[b]
        return a == x

    n, m = list(map(int, input().split()))
    f = list(range(n))
    lang = [set(map(int, input().split()[1:])) for _ in range(n)]
    for i, langi in enumerate(lang):
        for j, langj in enumerate(lang[:i]):
            if root(j) and langj.intersection(langi):
                f[j] = i
                langi.update(langj)
    print(sum(root(i) for i in range(n)) - any(lang))


def __starting_point():
    main()

__starting_point()

def main():
    n, m = list(map(int, input().split()))
    f = list(range(n))
    lang = [set(map(int, input().split()[1:])) for _ in range(n)]
    for i, langi in enumerate(lang):
        for j, langj in enumerate(lang[:i]):
            if f[j] == j and langj.intersection(langi):
                f[j] = i
                langi.update(langj)
    print(sum(i == x for i, x in enumerate(f)) - any(lang))


def __starting_point():
    main()

__starting_point()

def main():
    n, m = list(map(int, input().split()))
    f = [1] * n
    lang = [set(map(int, input().split()[1:])) for _ in range(n)]
    for i, langi in enumerate(lang):
        for j, langj in enumerate(lang[:i]):
            if f[j] and langj.intersection(langi):
                f[j] = 0
                langi.update(langj)
    print(sum(f) - any(lang))


def __starting_point():
    main()

__starting_point()

def find(sets, a):
	if a == sets[a]:
		return a
	sets[a] = find(sets, sets[a])
	return sets[a]

def join(sets, a, b):
	a = find(sets, a)
	b = find(sets, b)
	if a != b:
		sets[a] = b

n, m = (int(x) for x in input().split())
sets = list(range(n))
langs = []
for i in range(n):
	langs.append(set([int(x) for x in input().split()][1:]))
if all(len(lang) == 0 for lang in langs):
	print(n)
	return
for i in range(n):
	for lang in langs[i]:
		for j in range(i):
			if lang in langs[j]:
				join(sets, i, j)
print(len(set(find(sets, s) for s in sets)) - 1)


# coding: utf-8
n, m = [int(i) for i in input().split()]
languages = []
ans = 0
for i in range(n):
    tmp = set([int(j) for j in input().split()[1:]])
    if tmp:
        languages.append(tmp)
    else:
        n -= 1
        ans += 1
i = 0
while i < n:
    j = i+1
    while j < n:
        if languages[i]&languages[j]:
            languages[i] = languages[i]|languages[j]
            del(languages[j])
            n -= 1
        else:
            j += 1
    i += 1
i = 0
while i < n:
    j = i+1
    while j < n:
        if languages[i]&languages[j]:
            languages[i] = languages[i]|languages[j]
            del(languages[j])
            n -= 1
        else:
            j += 1
    i += 1
if n:
    ans += n-1
print(ans)


# coding: utf-8
def union(s,n):
    i = 0
    while i < n:
        j = i+1
        while j < n:
            if s[i]&s[j]:
                s[i] = s[i]|s[j]
                del(s[j])
                n -= 1
            else:
                j += 1
        i += 1
    return s, n
n, m = [int(i) for i in input().split()]
languages = []
ans = 0
for i in range(n):
    tmp = set([int(j) for j in input().split()[1:]])
    if tmp:
        languages.append(tmp)
    else:
        n -= 1
        ans += 1
languages, n = union(languages,n)
languages, n = union(languages,n)
if languages:
    ans += n-1
print(ans)


n, m = map(int, input().split())
q = [[] for i in range(n + 1)]
r, s = [0] * (m + 1), 0
for i in range(1, n + 1):
    t = list(map(int, input().split()))
    if t[0]:
        t = t[1: ]
        d = set([r[j] for j in t])
        if 0 in d: d.remove(0)
        if len(d):
            for j in d:
                for k in q[j]:
                    r[k] = i
                q[i].extend(q[j])
                q[j] = []
            t = [j for j in t if r[j] == 0]
            for k in t:
                r[k] = i
            q[i].extend(t)
        else:
            for k in t:
                r[k] = i
            q[i] = t          
    else: s += 1
print(s + max(sum(len(i) > 0 for i in q) - 1, 0))

3.4

n, m = list(map(int, input().split()))

arr = [[] for i in range(n)]
used = [0 for i in range(n)]
ans = 0

def dfs(vertex):
    used[vertex] = 1
    langs = arr[vertex]
    for lang in langs:
            for ind, next_vertex in enumerate(arr):
                if lang in next_vertex and used[ind] == 0:
                    dfs(ind)
test = 0
for i in range(n):
    arr[i] = list(map(int, input().split()[1:]))
    if len(arr[i]) == 0:
        test += 1
for node in range(n):
    if used[node] == 0:
        dfs(node)
        ans += 1
print(ans - 1 + (1 if test == n else 0))




p = [i for i in range(110)]
rank = [0 for i in range(110)]

def findSet(i):
	nonlocal p
	if(p[i]==i):
		return i
	p[i] = findSet(p[i])
	return p[i]

def isSameSet(i,j):
	return findSet(i)==findSet(j)

def unionSet(i,j):
	nonlocal p,rank
	if(isSameSet(i,j)):
		return
	x = findSet(i)
	y = findSet(j)
	if(rank[x]>rank[y]):
		p[y] = x
	else:
		p[x] = y
		if(rank[x]==rank[y]):
			rank[y]+=1

n,m = tuple(int(i) for i in input().split())
langs = [set() for i in range(n)]

allzero = True
for i in range(n):
	A = list(int(i) for i in input().split())
	if(A[0]!=0):
		allzero = False
	for j in range(len(A)):
		if(j>0):
			langs[i].add(A[j])
if not allzero:
	for i in range(n):
		for j in range(i+1,n):
			if(len(langs[i] & langs[j])>=1):
				unionSet(i,j)

	roots = set()
	for i in range(n):
		roots.add(findSet(i))

	print(len(roots)-1)
else:
	print(n)

from collections import deque,namedtuple

def dfs(adj,s,visited):
    dq = deque([s])
    while len(dq) > 0:
        now = dq.pop()
        if visited[now]: continue
        visited[now] = 1
        for i in adj[now]:
            if not visited[i]:
                dq.append(i)

def main():
    n,m = map(int,input().strip().split())
    adj,check = [[] for j in range(n+m)],0
    for i in range(n):
        l = [int(j) for j in input().split()][1:]
        check = max(check,len(l))
        for j in l:
            adj[i].append(n+j-1)
            adj[n+j-1].append(i)
    visited,ans = [0 for i in range(n+m)],-1
    if check == 0:
        print(n)
        return
    for i in range(n):
        if not visited[i]:
            dfs(adj,i,visited)
            ans += 1
    print(ans)


def __starting_point():
    main()
__starting_point()

#DSU
import bisect,sys
from collections import deque, namedtuple
sys.setrecursionlimit(20000)
N = 300
par = [i for i in range(N)]

def find(i):
    if i == par[i]: return i
    par[i] = find(par[i])
    return par[i]

def merge(a,b):
    a = find(a)
    b = find(b)
    par[b] = a

def main():
    n,m = map(int,input().split())
    check,ans = 0,-1
    for _ in range(n):
        l = [int(i) for i in input().split()][1:]
        check = max(check,len(l))
        for i in l:
            merge(_,n+i)
    if not check:
        print(n)
        return
    for i in range(n):
        if i == par[i]:
            ans += 1
    print(ans)

def __starting_point():
    main()
__starting_point()

#DSU
import bisect,sys
from collections import deque, namedtuple
N = 300
par = [i for i in range(N)]

def find(i):
    root = i
    while root != par[root]: root = par[root]
    while i != par[i]:
        newp = par[i]
        par[i] = root
        i = newp
    return root

def merge(a,b):
    a = find(a)
    b = find(b)
    par[b] = a

def main():
    n,m = map(int,input().split())
    check,ans = 0,-1
    for _ in range(n):
        l = [int(i) for i in input().split()][1:]
        check = max(check,len(l))
        for i in l:
            merge(_,n+i)
    if not check:
        print(n)
        return
    for i in range(n):
        if i == par[i]:
            ans += 1
    print(ans)

def __starting_point():
    main()
__starting_point()

#DSUA
import bisect,sys
from collections import deque, namedtuple
N = 300
par = [i for i in range(N)]

def find(i):
    root = i
    while root != par[root]: root = par[root]
    while i != par[i]:
        newp = par[i]
        par[i] = root
        i = newp
    return root

def merge(a,b):
    a = find(a)
    b = find(b)
    par[b] = a

def main():
    n,m = map(int,input().split())
    check,ans = 0,-1
    for _ in range(n):
        l = [int(i) for i in input().split()][1:]
        check = max(check,len(l))
        for i in l:
            merge(_,n+i)
    if not check:
        print(n)
        return
    for i in range(n):
        if i == par[i]:
            ans += 1
    print(ans)

def __starting_point():
    main()
__starting_point()