import sys

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

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

edges = [[] for i in range(n)]

visit = [False for i in range(n)]

def dfs(v):

    visit[v] = True

    for i in edges[v]:

        if not visit[i]:

            dfs(i)

for i in range(m):

    a, b = list(map(int, input().split()))

    edges[a - 1].append(b - 1)

    edges[b - 1].append(a - 1)

cnt = 0

for i in range(n):

    if not visit[i]:

        dfs(i)

        cnt += 1

print((cnt - 1))
class UnionFind():

    def __init__(self, n):

        self.n = n

        self.par = list(range(self.n))

        self.rank = [1] * n

        self.cnt = n

    def find(self, x):

        if self.par[x] == x:

            return x

        else:

            self.par[x] = self.find(self.par[x])

            return self.par[x]

    def unite(self, x, y):

        p = self.find(x)

        q = self.find(y)

        if p == q:

            return None

        if p > q:

            p, q = q, p

        self.rank[p] += self.rank[q]

        self.par[q] = p

        self.cnt -= 1

    def same(self, x, y):

        return self.find(x) == self.find(y)

    def size(self, x):

        return self.rank[x]

    def count(self):

        return self.cnt

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

UF = UnionFind(n)

for _ in range(m):

    a, b = list(map(int, input().split()))

    UF.unite(a - 1, b - 1)

print((UF.count() - 1))
import sys

sys.setrecursionlimit(2 * (10**6))
n, m = list(map(int, input().split()))
edges = [[] for i in range(n)]
visit = [False for i in range(n)]


def dfs(v):
    visit[v] = True
    for i in edges[v]:
        if not visit[i]:
            dfs(i)


for i in range(m):
    a, b = list(map(int, input().split()))
    edges[a - 1].append(b - 1)
    edges[b - 1].append(a - 1)
cnt = 0
for i in range(n):
    if not visit[i]:
        dfs(i)
        cnt += 1
print((cnt - 1))

class UnionFind:
    def __init__(self, n):
        self.n = n
        self.par = list(range(self.n))
        self.rank = [1] * n
        self.cnt = n

    def find(self, x):
        if self.par[x] == x:
            return x
        else:
            self.par[x] = self.find(self.par[x])
            return self.par[x]

    def unite(self, x, y):
        p = self.find(x)
        q = self.find(y)
        if p == q:
            return None
        if p > q:
            p, q = q, p
        self.rank[p] += self.rank[q]
        self.par[q] = p
        self.cnt -= 1

    def same(self, x, y):
        return self.find(x) == self.find(y)

    def size(self, x):
        return self.rank[x]

    def count(self):
        return self.cnt


n, m = list(map(int, input().split()))
UF = UnionFind(n)
for _ in range(m):
    a, b = list(map(int, input().split()))
    UF.unite(a - 1, b - 1)
print((UF.count() - 1))

-import sys +class UnionFind: +    def __init__(self, n): +        self.n = n +        self.par = list(range(self.n)) +        self.rank = [1] * n +        self.cnt = n -sys.setrecursionlimit(2 * (10**6)) -n, m = list(map(int, input().split())) -edges = [[] for i in range(n)] -visit = [False for i in range(n)] +    def find(self, x): +        if self.par[x] == x: +            return x +        else: +            self.par[x] = self.find(self.par[x]) +            return self.par[x] + +    def unite(self, x, y): +        p = self.find(x) +        q = self.find(y) +        if p == q: +            return None +        if p > q: +            p, q = q, p +        self.rank[p] += self.rank[q] +        self.par[q] = p +        self.cnt -= 1 + +    def same(self, x, y): +        return self.find(x) == self.find(y) + +    def size(self, x): +        return self.rank[x] + +    def count(self): +        return self.cnt -def dfs(v): -    visit[v] = True -    for i in edges[v]: -        if not visit[i]: -            dfs(i) - - -for i in range(m): +n, m = list(map(int, input().split())) +UF = UnionFind(n) +for _ in range(m): -    edges[a - 1].append(b - 1) -    edges[b - 1].append(a - 1) -cnt = 0 -for i in range(n): -    if not visit[i]: -        dfs(i) -        cnt += 1 -print((cnt - 1)) +    UF.unite(a - 1, b - 1) +print((UF.count() - 1))