class DisjointSet
  class Node
    include Comparable

    attr_reader :id
    attr_accessor :parent, :rank

    def initialize(id)
      @id = id
      @parent = self
      @rank = 0
    end

    def root?
      @parent == self
    end

    def <=>(other)
      @id <=> other.id
    end
  end

  def initialize(n)
    @nodes = n.times.map {|id| Node.new(id) }
  end

  def unite(x, y)
    node1 = @nodes[x]
    node2 = @nodes[y]

    link(find_set(node1), find_set(node2))
  end

  def same?(x, y)
    find_set(@nodes[x]) == find_set(@nodes[y])
  end

  private

  def find_set(node)
    unless node.root?
      node.parent = find_set(node.parent)
    end

    node.parent
  end

  def link(node1, node2)
    if node1.rank > node2.rank
      node2.parent = node1
    else
      node1.parent = node2

      if node1.rank == node2.rank
        node2.rank += 1
      end
    end
  end
end

if $PROGRAM_NAME == __FILE__
  n, q = gets.chomp.split.map(&:to_i)

  ds = DisjointSet.new(n)

  q.times do
    command, x, y = gets.chomp.split.map(&:to_i)

    case command
    when 0
      ds.unite(x, y)
    when 1
      puts ds.same?(x, y) ? 1 : 0
    else
      raise "unknown command: #{command}"
    end
  end
end