code = r"""

# distutils: language=c++

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

from libcpp cimport *

from libcpp.vector cimport vector





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

    cdef cppclass dsu:

        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()





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

cdef dsu uf = dsu(N)

for _ in range(Q):

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

    if t == 0:

        uf.merge(u, v)

    else:

        if uf.same(u, v):

            print(1)

        else:

            print(0)



"""

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 *

from libcpp.vector cimport vector





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

    cdef cppclass dsu:

        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()



import sys

input = sys.stdin.buffer.readline



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

cdef dsu uf = dsu(N)

for _ in range(Q):

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

    if t == 0:

        uf.merge(u, v)

    else:

        if uf.same(u, v):

            print(1)

        else:

            print(0)



"""

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 *
from libcpp.vector cimport vector
cdef extern from "/opt/atcoder-stl/atcoder/dsu.hpp" namespace "atcoder":
    cdef cppclass dsu:
        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()
N, Q = map(int, input().split())
cdef dsu uf = dsu(N)
for _ in range(Q):
    t, u, v = map(int, input().split())
    if t == 0:
        uf.merge(u, v)
    else:
        if uf.same(u, v):
            print(1)
        else:
            print(0)
"""
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 *
from libcpp.vector cimport vector
cdef extern from "/opt/atcoder-stl/atcoder/dsu.hpp" namespace "atcoder":
    cdef cppclass dsu:
        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()
import sys
input = sys.stdin.buffer.readline
N, Q = map(int, input().split())
cdef dsu uf = dsu(N)
for _ in range(Q):
    t, u, v = map(int, input().split())
    if t == 0:
        uf.merge(u, v)
    else:
        if uf.same(u, v):
            print(1)
        else:
            print(0)
"""
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