n, m = map(int, input().split())
groups = [] # 모둠들을 저장할 리스트 (각 모둠은 set)
for _ in range(m):
a, b = map(int, input().split())
group_a = None
group_b = None
# a, b가 이미 어떤 모둠에 들어있는지 찾기
for g in groups:
if a in g:
group_a = g
if b in g:
group_b = g
# 둘 다 새로운 학생이면 → 새 모둠 생성
if group_a is None and group_b is None:
groups.append(set([a, b]))
# a만 모둠에 있으면 → 거기에 b 추가
elif group_a is not None and group_b is None:
group_a.add(b)
# b만 모둠에 있으면 → 거기에 a 추가
elif group_a is None and group_b is not None:
group_b.add(a)
# 서로 다른 모둠이면 → 합치기
elif group_a is not group_b:
group_a |= group_b # set 합치기
groups.remove(group_b)
# 혼자 있는 학생 세기
alone = n - len(set().union(*groups)) if groups else n
print(len(groups) + alone)
To embed this project on your website, copy the following code and paste it into your website's HTML: