class Node
  attr_reader :val
  attr_accessor :parent

  def initialize(val)
    @val = val
    @parent = nil
  end

  def unite(other)
    return if root.val == other.root.val
    root.val < other.root.val ? other.root.parent = root : root.parent = other.root
  end

  def same(other)
    root == other.root ? 1 : 0
  end

  def root
    return self if parent.nil?

    parent.root
  end
 
end

n, q = gets.split.map(&:to_i)
nodes = (0...n).map{|v| Node.new(v)}
q.times do
  com, x, y = gets.split.map(&:to_i)
  case com
  when 0
    nodes[x].unite(nodes[y])
  when 1
    puts nodes[x].same(nodes[y])
  end
end