from collections import deque



N, M = list(map(int, input().split()))



AB = [[] for _ in range(N)]



for _ in range(M):

    a,b = [int(x)-1 for x in input().split()]

    AB[a].append(b)

    AB[b].append(a)



visited = [0] * N

ans = []

for i in range(N):

    if visited[i]:

        continue

    group = [i]

    d = deque(AB[i])

    while d:

        s = d.popleft()

        if visited[s]:

            continue

        visited[s] = 1

        group.append(s)

        for k in AB[s]:

            d.append(k)

    ans.append(len(group))



print((len(ans)-1))

import sys

sys.setrecursionlimit(2 * (10 ** 6))



def main():

    N, M = list(map(int, input().split()))

    AB = [[] for _ in range(N)]



    for _ in range(M):

        a,b = [int(x)-1 for x in input().split()]

        AB[a].append(b)

        AB[b].append(a)



    def dfs(v):

        visited[v] = 1

        for i in AB[v]:

            if visited[i]:

                continue

            dfs(i)



    visited = [0]*N

    cnt = 0

    for i in range(N):

        if visited[i]:

            continue

        dfs(i)

        cnt += 1

    print((cnt-1))



if __name__ == "__main__":

    main()

from collections import deque

N, M = list(map(int, input().split()))
AB = [[] for _ in range(N)]
for _ in range(M):
    a, b = [int(x) - 1 for x in input().split()]
    AB[a].append(b)
    AB[b].append(a)
visited = [0] * N
ans = []
for i in range(N):
    if visited[i]:
        continue
    group = [i]
    d = deque(AB[i])
    while d:
        s = d.popleft()
        if visited[s]:
            continue
        visited[s] = 1
        group.append(s)
        for k in AB[s]:
            d.append(k)
    ans.append(len(group))
print((len(ans) - 1))

import sys

sys.setrecursionlimit(2 * (10**6))


def main():
    N, M = list(map(int, input().split()))
    AB = [[] for _ in range(N)]
    for _ in range(M):
        a, b = [int(x) - 1 for x in input().split()]
        AB[a].append(b)
        AB[b].append(a)

    def dfs(v):
        visited[v] = 1
        for i in AB[v]:
            if visited[i]:
                continue
            dfs(i)

    visited = [0] * N
    cnt = 0
    for i in range(N):
        if visited[i]:
            continue
        dfs(i)
        cnt += 1
    print((cnt - 1))


if __name__ == "__main__":
    main()

-from collections import deque +import sys -N, M = list(map(int, input().split())) -AB = [[] for _ in range(N)] -for _ in range(M): -    a, b = [int(x) - 1 for x in input().split()] -    AB[a].append(b) -    AB[b].append(a) -visited = [0] * N -ans = [] -for i in range(N): -    if visited[i]: -        continue -    group = [i] -    d = deque(AB[i]) -    while d: -        s = d.popleft() -        if visited[s]: +sys.setrecursionlimit(2 * (10**6)) + + +def main(): +    N, M = list(map(int, input().split())) +    AB = [[] for _ in range(N)] +    for _ in range(M): +        a, b = [int(x) - 1 for x in input().split()] +        AB[a].append(b) +        AB[b].append(a) + +    def dfs(v): +        visited[v] = 1 +        for i in AB[v]: +            if visited[i]: +                continue +            dfs(i) + +    visited = [0] * N +    cnt = 0 +    for i in range(N): +        if visited[i]: -        visited[s] = 1 -        group.append(s) -        for k in AB[s]: -            d.append(k) -    ans.append(len(group)) -print((len(ans) - 1)) +        dfs(i) +        cnt += 1 +    print((cnt - 1)) + + +if __name__ == "__main__": +    main()