import sys

input = sys.stdin.buffer.readline

sys.setrecursionlimit(10 ** 7)





class UF_tree:

    def __init__(self, n):

        self.root = [-1] * (n + 1)

        self.rank = [0] * (n + 1)



    def find(self, x):

        if self.root[x] < 0:

            return x

        else:

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

            return self.root[x]



    def isSame(self, x, y):

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



    def unite(self, x, y):

        x = self.find(x)

        y = self.find(y)

        if x == y:

            return

        elif self.rank[x] < self.rank[y]:

            self.root[y] += self.root[x]

            self.root[x] = y

        else:

            self.root[x] += self.root[y]

            self.root[y] = x

            if self.rank[x] == self.rank[y]:

                self.rank[x] += 1



    def size(self, x):

        return -self.root[self.find(x)]





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

uf = UF_tree(N + 1)

for _ in range(Q):

    t, u, v = list(map(int, input().split()))

    if t == 0:

        uf.unite(u, v)

    else:

        if uf.isSame(u, v):

            print((1))

        else:

            print((0))
code = r"""

# distutils: language=c++

# distutils: include_dirs=/home/contestant/.local/lib/python3.8/site-packages/numpy/core/include

from libcpp cimport bool

from libcpp.vector cimport vector

cdef extern from "/opt/atcoder-stl/atcoder/dsu.hpp" namespace "atcoder":

    cdef cppclass dsu:

        dsu(int n)

        int merge(int a, int b)

        bool same(int a, int b)

        int leader(int a)

        int size(int a)

        vector[vector[int]] groups()



from libc.stdio cimport scanf, printf

cdef int i

cdef int N, Q

cdef int t, u, v

scanf("%d%d", &N, &Q)

cdef dsu *d = new dsu(N)

for i in range(Q):

    scanf("%d%d%d", &t, &u, &v)

    if t == 0:

        d.merge(u, v)

    else:

        printf("%d\n", d.same(u, v))



"""

import os

import sys

if sys.argv[-1] == 'ONLINE_JUDGE':

    open('solve.pyx', 'w').write(code)

    os.system('cythonize -i -3 -b solve.pyx')

import solve
import sys

input = sys.stdin.buffer.readline
sys.setrecursionlimit(10**7)


class UF_tree:
    def __init__(self, n):
        self.root = [-1] * (n + 1)
        self.rank = [0] * (n + 1)

    def find(self, x):
        if self.root[x] < 0:
            return x
        else:
            self.root[x] = self.find(self.root[x])
            return self.root[x]

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

    def unite(self, x, y):
        x = self.find(x)
        y = self.find(y)
        if x == y:
            return
        elif self.rank[x] < self.rank[y]:
            self.root[y] += self.root[x]
            self.root[x] = y
        else:
            self.root[x] += self.root[y]
            self.root[y] = x
            if self.rank[x] == self.rank[y]:
                self.rank[x] += 1

    def size(self, x):
        return -self.root[self.find(x)]


N, Q = list(map(int, input().split()))
uf = UF_tree(N + 1)
for _ in range(Q):
    t, u, v = list(map(int, input().split()))
    if t == 0:
        uf.unite(u, v)
    else:
        if uf.isSame(u, v):
            print((1))
        else:
            print((0))

code = r"""
# distutils: language=c++
# distutils: include_dirs=/home/contestant/.local/lib/python3.8/site-packages/numpy/core/include
from libcpp cimport bool
from libcpp.vector cimport vector
cdef extern from "/opt/atcoder-stl/atcoder/dsu.hpp" namespace "atcoder":
    cdef cppclass dsu:
        dsu(int n)
        int merge(int a, int b)
        bool same(int a, int b)
        int leader(int a)
        int size(int a)
        vector[vector[int]] groups()
from libc.stdio cimport scanf, printf
cdef int i
cdef int N, Q
cdef int t, u, v
scanf("%d%d", &N, &Q)
cdef dsu *d = new dsu(N)
for i in range(Q):
    scanf("%d%d%d", &t, &u, &v)
    if t == 0:
        d.merge(u, v)
    else:
        printf("%d\n", d.same(u, v))
"""
import os
import sys

if sys.argv[-1] == "ONLINE_JUDGE":
    open("solve.pyx", "w").write(code)
    os.system("cythonize -i -3 -b solve.pyx")
import solve

+code = r""" +# distutils: language=c++ +# distutils: include_dirs=/home/contestant/.local/lib/python3.8/site-packages/numpy/core/include +from libcpp cimport bool +from libcpp.vector cimport vector +cdef extern from "/opt/atcoder-stl/atcoder/dsu.hpp" namespace "atcoder": +    cdef cppclass dsu: +        dsu(int n) +        int merge(int a, int b) +        bool same(int a, int b) +        int leader(int a) +        int size(int a) +        vector[vector[int]] groups() +from libc.stdio cimport scanf, printf +cdef int i +cdef int N, Q +cdef int t, u, v +scanf("%d%d", &N, &Q) +cdef dsu *d = new dsu(N) +for i in range(Q): +    scanf("%d%d%d", &t, &u, &v) +    if t == 0: +        d.merge(u, v) +    else: +        printf("%d\n", d.same(u, v)) +""" +import os -input = sys.stdin.buffer.readline -sys.setrecursionlimit(10**7) - - -class UF_tree: -    def __init__(self, n): -        self.root = [-1] * (n + 1) -        self.rank = [0] * (n + 1) - -    def find(self, x): -        if self.root[x] < 0: -            return x -        else: -            self.root[x] = self.find(self.root[x]) -            return self.root[x] - -    def isSame(self, x, y): -        return self.find(x) == self.find(y) - -    def unite(self, x, y): -        x = self.find(x) -        y = self.find(y) -        if x == y: -            return -        elif self.rank[x] < self.rank[y]: -            self.root[y] += self.root[x] -            self.root[x] = y -        else: -            self.root[x] += self.root[y] -            self.root[y] = x -            if self.rank[x] == self.rank[y]: -                self.rank[x] += 1 - -    def size(self, x): -        return -self.root[self.find(x)] - - -N, Q = list(map(int, input().split())) -uf = UF_tree(N + 1) -for _ in range(Q): -    t, u, v = list(map(int, input().split())) -    if t == 0: -        uf.unite(u, v) -    else: -        if uf.isSame(u, v): -            print((1)) -        else: -            print((0)) +if sys.argv[-1] == "ONLINE_JUDGE": +    open("solve.pyx", "w").write(code) +    os.system("cythonize -i -3 -b solve.pyx") +import solve