# union-find

class UnionFind:

    def __init__(self, n):

        self.prsz = [-1] * n # parent & size

    def find(self, v): # root

        if self.prsz[v] < 0: return v

        self.prsz[v] = self.find(self.prsz[v])

        return self.prsz[v]

    def union(self, v, u): # unite

        v = self.find(v); u = self.find(u)

        if v == u: return False

        if self.prsz[v] > self.prsz[u]: v, u = u, v # size(v) < size(u)

        self.prsz[v] += self.prsz[u]

        self.prsz[u] = v

        return True



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

uf = UnionFind(n)

for i in range(m):

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

    uf.union(a, b)

ans = set()

for i in range(n):

    ans.add(uf.find(i))

print((len(ans) - 1))

# union-find

class UnionFind:

    def __init__(self, n):

        self.prsz = [-1] * n # parent & size

    def find(self, v): # root

        if self.prsz[v] < 0: return v

        self.prsz[v] = self.find(self.prsz[v])

        return self.prsz[v]

    def union(self, v, u): # unite

        v = self.find(v); u = self.find(u)

        if v == u: return False

        if self.prsz[v] > self.prsz[u]: v, u = u, v # size(v) < size(u)

        self.prsz[v] += self.prsz[u]

        self.prsz[u] = v

        return True



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

uf = UnionFind(n)

for i in range(m):

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

    uf.union(a, b)

ans = -1

for a in uf.prsz:

    if a < 0: ans += 1

print(ans)

# union-find
class UnionFind:
    def __init__(self, n):
        self.prsz = [-1] * n  # parent & size

    def find(self, v):  # root
        if self.prsz[v] < 0:
            return v
        self.prsz[v] = self.find(self.prsz[v])
        return self.prsz[v]

    def union(self, v, u):  # unite
        v = self.find(v)
        u = self.find(u)
        if v == u:
            return False
        if self.prsz[v] > self.prsz[u]:
            v, u = u, v  # size(v) < size(u)
        self.prsz[v] += self.prsz[u]
        self.prsz[u] = v
        return True


n, m = list(map(int, input().split()))
uf = UnionFind(n)
for i in range(m):
    a, b = [int(x) - 1 for x in input().split()]
    uf.union(a, b)
ans = set()
for i in range(n):
    ans.add(uf.find(i))
print((len(ans) - 1))

# union-find
class UnionFind:
    def __init__(self, n):
        self.prsz = [-1] * n  # parent & size

    def find(self, v):  # root
        if self.prsz[v] < 0:
            return v
        self.prsz[v] = self.find(self.prsz[v])
        return self.prsz[v]

    def union(self, v, u):  # unite
        v = self.find(v)
        u = self.find(u)
        if v == u:
            return False
        if self.prsz[v] > self.prsz[u]:
            v, u = u, v  # size(v) < size(u)
        self.prsz[v] += self.prsz[u]
        self.prsz[u] = v
        return True


n, m = list(map(int, input().split()))
uf = UnionFind(n)
for i in range(m):
    a, b = [int(x) - 1 for x in input().split()]
    uf.union(a, b)
ans = -1
for a in uf.prsz:
    if a < 0:
        ans += 1
print(ans)

-ans = set() -for i in range(n): -    ans.add(uf.find(i)) -print((len(ans) - 1)) +ans = -1 +for a in uf.prsz: +    if a < 0: +        ans += 1 +print(ans)