
class Union_Find
    def initialize(n); @parent_or_size = Array.new(n, -1); end

    def merge(a, b)
        x = leader(a)
        y = leader(b)

        return x if x == y
        x, y = y, x if @parent_or_size[x] > @parent_or_size[y]
        @parent_or_size[x] += @parent_or_size[y]
        @parent_or_size[y] = x
    end

    def same(a, b); leader(a) == leader(b); end
    def leader(a); @parent_or_size[a] < 0 ? a : (@parent_or_size[a] = leader(@parent_or_size[a])); end
    def size(a); -@parent_or_size[leader(a)]; end

    def groups
        n = @parent_or_size.size
        result = Array.new(n) {[]}

        n.times {|i| result[leader(i)] << i}
        result.compact!
    end
end

n, q = gets.split.map(&:to_i)
uf = Union_Find.new(n)

q.times do
    t, u, v = gets.split.map(&:to_i)
    t == 0 ? uf.merge(u,v) : (puts uf.same(u,v) ? 1 : 0)
end
