from typing import List





class DSU:

    def __init__(self, n: int) -> None:

        self._n = n

        self.parent_or_size = [-1] * n



    def merge(self, a: int, b: int) -> int:

        assert 0 <= a <= self._n

        assert 0 <= b <= self._n

        x, y = self.leader(a), self.leader(b)

        if x == y:

            return x

        if -self.parent_or_size[x] < -self.parent_or_size[y]:

            x, y = y, x

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

        self.parent_or_size[y] = x

        return x



    def same(self, a: int, b: int) -> bool:

        assert 0 <= a <= self._n

        assert 0 <= b <= self._n

        return self.leader(a) == self.leader(b)



    def leader(self, a: int) -> int:

        assert 0 <= a < self._n

        while self.parent_or_size[a] >= 0:

            a = self.parent_or_size[a]

        return a



    def size(self, a: int) -> int:

        assert 0 <= a <= self._n

        return -self.parent_or_size[self.leader(a)]



    def groups(self) -> List[List[int]]:

        leader_buf = [self.leader(i) for i in range(self._n)]

        group_size = [0] * self._n

        for i in leader_buf:

            group_size[i] += 1

        result = [[] for _ in range(self._n)]

        for i in range(self._n):

            result[leader_buf[i]].append(i)

        result = [i for i in result if i]

        return result





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

uf = DSU(n)

for i in range(q):

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

    if t:

        print((int(uf.same(u, v))))

    else:

        uf.merge(u, v)

class UnionFind:

    def __init__(self, N):

        """

        N:要素数

        root:各要素の親要素の番号を格納するリスト.

             ただし, root[x] < 0 ならその頂点が根で-root[x]が木の要素数.

        rank:ランク

        """

        self.N = N

        self.root = [-1] * N

        self.rank = [0] * N



    def __repr__(self):

        return '\n'.join('{}: {}'.format(r, self.members(r)) for r in self.roots())



    def find(self, x):

        """頂点xの根を見つける"""

        while self.root[x] >= 0:

            x = self.root[x]

        return x



    def union(self, x, y):

        """x,yが属する木をunion"""

        # 根を比較する

        # すでに同じ木に属していた場合は何もしない.

        # 違う木に属していた場合はrankを見てくっつける方を決める.

        # rankが同じ時はrankを1増やす

        x = self.find(x)

        y = self.find(y)

        if x == y:

            return

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

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

            self.root[y] = x

        else:

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

            self.root[x] = y

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

                self.rank[y] += 1



    def same(self, x, y):

        """xとyが同じグループに属するかどうか"""

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



    def count(self, x):

        """頂点xが属する木のサイズを返す"""

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



    def members(self, x):

        """xが属する木の要素を列挙"""

        _root = self.find(x)

        return [i for i in range(self.N) if self.find == _root]



    def roots(self):

        """森の根を列挙"""

        return [i for i, x in enumerate(self.root) if x < 0]



    def group_count(self):

        """連結成分の数"""

        return len(self.roots())



    def all_group_members(self):

        """{ルート要素: [そのグループに含まれる要素のリスト], ...}の辞書を返す"""

        return {r: self.members(r) for r in self.roots()}





import sys

input = sys.stdin.readline

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

uf = UnionFind(n)

for i in range(q):

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

    if t:

        print((int(uf.same(u, v))))

    else:

        uf.union(u, v)

from typing import List


class DSU:
    def __init__(self, n: int) -> None:
        self._n = n
        self.parent_or_size = [-1] * n

    def merge(self, a: int, b: int) -> int:
        assert 0 <= a <= self._n
        assert 0 <= b <= self._n
        x, y = self.leader(a), self.leader(b)
        if x == y:
            return x
        if -self.parent_or_size[x] < -self.parent_or_size[y]:
            x, y = y, x
        self.parent_or_size[x] += self.parent_or_size[y]
        self.parent_or_size[y] = x
        return x

    def same(self, a: int, b: int) -> bool:
        assert 0 <= a <= self._n
        assert 0 <= b <= self._n
        return self.leader(a) == self.leader(b)

    def leader(self, a: int) -> int:
        assert 0 <= a < self._n
        while self.parent_or_size[a] >= 0:
            a = self.parent_or_size[a]
        return a

    def size(self, a: int) -> int:
        assert 0 <= a <= self._n
        return -self.parent_or_size[self.leader(a)]

    def groups(self) -> List[List[int]]:
        leader_buf = [self.leader(i) for i in range(self._n)]
        group_size = [0] * self._n
        for i in leader_buf:
            group_size[i] += 1
        result = [[] for _ in range(self._n)]
        for i in range(self._n):
            result[leader_buf[i]].append(i)
        result = [i for i in result if i]
        return result


n, q = list(map(int, input().split()))
uf = DSU(n)
for i in range(q):
    t, u, v = list(map(int, input().split()))
    if t:
        print((int(uf.same(u, v))))
    else:
        uf.merge(u, v)

class UnionFind:
    def __init__(self, N):
        """
        N:要素数
        root:各要素の親要素の番号を格納するリスト.
             ただし, root[x] < 0 ならその頂点が根で-root[x]が木の要素数.
        rank:ランク
        """
        self.N = N
        self.root = [-1] * N
        self.rank = [0] * N

    def __repr__(self):
        return "\n".join("{}: {}".format(r, self.members(r)) for r in self.roots())

    def find(self, x):
        """頂点xの根を見つける"""
        while self.root[x] >= 0:
            x = self.root[x]
        return x

    def union(self, x, y):
        """x,yが属する木をunion"""
        # 根を比較する
        # すでに同じ木に属していた場合は何もしない.
        # 違う木に属していた場合はrankを見てくっつける方を決める.
        # rankが同じ時はrankを1増やす
        x = self.find(x)
        y = self.find(y)
        if x == y:
            return
        elif self.rank[x] > self.rank[y]:
            self.root[x] += self.root[y]
            self.root[y] = x
        else:
            self.root[y] += self.root[x]
            self.root[x] = y
            if self.rank[x] == self.rank[y]:
                self.rank[y] += 1

    def same(self, x, y):
        """xとyが同じグループに属するかどうか"""
        return self.find(x) == self.find(y)

    def count(self, x):
        """頂点xが属する木のサイズを返す"""
        return -self.root[self.find(x)]

    def members(self, x):
        """xが属する木の要素を列挙"""
        _root = self.find(x)
        return [i for i in range(self.N) if self.find == _root]

    def roots(self):
        """森の根を列挙"""
        return [i for i, x in enumerate(self.root) if x < 0]

    def group_count(self):
        """連結成分の数"""
        return len(self.roots())

    def all_group_members(self):
        """{ルート要素: [そのグループに含まれる要素のリスト], ...}の辞書を返す"""
        return {r: self.members(r) for r in self.roots()}


import sys

input = sys.stdin.readline
n, q = list(map(int, input().split()))
uf = UnionFind(n)
for i in range(q):
    t, u, v = list(map(int, input().split()))
    if t:
        print((int(uf.same(u, v))))
    else:
        uf.union(u, v)

-from typing import List +class UnionFind: +    def __init__(self, N): +        """ +        N:要素数 +        root:各要素の親要素の番号を格納するリスト. +             ただし, root[x] < 0 ならその頂点が根で-root[x]が木の要素数. +        rank:ランク +        """ +        self.N = N +        self.root = [-1] * N +        self.rank = [0] * N + +    def __repr__(self): +        return "\n".join("{}: {}".format(r, self.members(r)) for r in self.roots()) + +    def find(self, x): +        """頂点xの根を見つける""" +        while self.root[x] >= 0: +            x = self.root[x] +        return x + +    def union(self, x, y): +        """x,yが属する木をunion""" +        # 根を比較する +        # すでに同じ木に属していた場合は何もしない. +        # 違う木に属していた場合はrankを見てくっつける方を決める. +        # rankが同じ時はrankを1増やす +        x = self.find(x) +        y = self.find(y) +        if x == y: +            return +        elif self.rank[x] > self.rank[y]: +            self.root[x] += self.root[y] +            self.root[y] = x +        else: +            self.root[y] += self.root[x] +            self.root[x] = y +            if self.rank[x] == self.rank[y]: +                self.rank[y] += 1 + +    def same(self, x, y): +        """xとyが同じグループに属するかどうか""" +        return self.find(x) == self.find(y) + +    def count(self, x): +        """頂点xが属する木のサイズを返す""" +        return -self.root[self.find(x)] + +    def members(self, x): +        """xが属する木の要素を列挙""" +        _root = self.find(x) +        return [i for i in range(self.N) if self.find == _root] + +    def roots(self): +        """森の根を列挙""" +        return [i for i, x in enumerate(self.root) if x < 0] + +    def group_count(self): +        """連結成分の数""" +        return len(self.roots()) + +    def all_group_members(self): +        """{ルート要素: [そのグループに含まれる要素のリスト], ...}の辞書を返す""" +        return {r: self.members(r) for r in self.roots()} -class DSU: -    def __init__(self, n: int) -> None: -        self._n = n -        self.parent_or_size = [-1] * n +import sys -    def merge(self, a: int, b: int) -> int: -        assert 0 <= a <= self._n -        assert 0 <= b <= self._n -        x, y = self.leader(a), self.leader(b) -        if x == y: -            return x -        if -self.parent_or_size[x] < -self.parent_or_size[y]: -            x, y = y, x -        self.parent_or_size[x] += self.parent_or_size[y] -        self.parent_or_size[y] = x -        return x - -    def same(self, a: int, b: int) -> bool: -        assert 0 <= a <= self._n -        assert 0 <= b <= self._n -        return self.leader(a) == self.leader(b) - -    def leader(self, a: int) -> int: -        assert 0 <= a < self._n -        while self.parent_or_size[a] >= 0: -            a = self.parent_or_size[a] -        return a - -    def size(self, a: int) -> int: -        assert 0 <= a <= self._n -        return -self.parent_or_size[self.leader(a)] - -    def groups(self) -> List[List[int]]: -        leader_buf = [self.leader(i) for i in range(self._n)] -        group_size = [0] * self._n -        for i in leader_buf: -            group_size[i] += 1 -        result = [[] for _ in range(self._n)] -        for i in range(self._n): -            result[leader_buf[i]].append(i) -        result = [i for i in result if i] -        return result - - +input = sys.stdin.readline -uf = DSU(n) +uf = UnionFind(n) -        uf.merge(u, v) +        uf.union(u, v)