Imports System

Module Program
    Sub Main(args As String())

        Dim NM() As Integer = Console.ReadLine().Split(" ").Select(Function(x) Integer.Parse(x)).ToArray()
        Dim CITY(NM(0)) As Integer
        Dim sima As Integer = 0

        For i As Integer = 0 To NM(1) - 1
            Dim AB() As Integer = Console.ReadLine().Split(" ").Select(Function(x) Integer.Parse(x)).ToArray()
            setOya(CITY, AB(0), AB(1))
        Next

        For i As Integer = 0 To NM(0) - 1
            If CITY(i) = i Then
                sima += 1
            End If
        Next

        Console.WriteLine(sima - 1)

    End Sub

    Private Sub setOya(CITY() As Integer, A As Integer, B As Integer)

        If CITY(A) <> 0 Then
            If CITY(B) <> 0 Then
                If CITY(A) <> CITY(B) Then
                    If CITY(A) < CITY(B) Then
                        CITY(B) = CITY(A)
                    Else
                        CITY(A) = CITY(B)
                    End If
                Else
                    ' continue
                End If
            Else
                CITY(B) = CITY(A)
            End If
        Else
            If CITY(B) <> 0 Then
                CITY(A) = CITY(B)
            Else
                If A < B Then
                    CITY(A) = A
                    CITY(B) = A
                Else
                    CITY(A) = B
                    CITY(B) = B
                End If
            End If
        End If

    End Sub

End Module
