class DSU:

    def __init__(self, n):

        self._n = n

        self.parent_or_size = [-1] * n



    def merge(self, a, b):

        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, b):

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



    def leader(self, a):

        while self.parent_or_size[a] >= 0:

            a = self.parent_or_size[a]

        return a



    def size(self, a):

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



    def groups(self):

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

dsu = DSU(n)



for _ in range(q):

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

    if t == 0:

        dsu.merge(u, v)

    else:

        print((1 if dsu.same(u, v) else 0))

import sys

input = sys.stdin.readline



class DSU:

    def __init__(self, n):

        self._n = n

        self.parent_or_size = [-1] * n



    def merge(self, a, b):

        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, b):

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



    def leader(self, a):

        while self.parent_or_size[a] >= 0:

            a = self.parent_or_size[a]

        return a



    def size(self, a):

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



    def groups(self):

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

dsu = DSU(n)



for _ in range(q):

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

    if t == 0:

        dsu.merge(u, v)

    else:

        print((1 if dsu.same(u, v) else 0))

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

    def merge(self, a, b):
        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, b):
        return self.leader(a) == self.leader(b)

    def leader(self, a):
        while self.parent_or_size[a] >= 0:
            a = self.parent_or_size[a]
        return a

    def size(self, a):
        return -self.parent_or_size[self.leader(a)]

    def groups(self):
        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()))
dsu = DSU(n)
for _ in range(q):
    t, u, v = list(map(int, input().split()))
    if t == 0:
        dsu.merge(u, v)
    else:
        print((1 if dsu.same(u, v) else 0))

import sys

input = sys.stdin.readline


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

    def merge(self, a, b):
        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, b):
        return self.leader(a) == self.leader(b)

    def leader(self, a):
        while self.parent_or_size[a] >= 0:
            a = self.parent_or_size[a]
        return a

    def size(self, a):
        return -self.parent_or_size[self.leader(a)]

    def groups(self):
        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()))
dsu = DSU(n)
for _ in range(q):
    t, u, v = list(map(int, input().split()))
    if t == 0:
        dsu.merge(u, v)
    else:
        print((1 if dsu.same(u, v) else 0))

+import sys + +input = sys.stdin.readline + +