class DisjointSet
  def initialize(n)
    @parents = Array.new(n, &:to_i)
    @ranks = Array.new(n, 0)
  end

  def unite(x, y)
    link(find_set(x), find_set(y))
  end

  def same?(x, y)
    find_set(x) == find_set(y)
  end

  private

  def link(x, y)
    if @ranks[x] > @ranks[y]
      @parents[y] = x
    else
      @parents[x] = y

      if @ranks[x] == @ranks[y]
        @ranks[y] += 1
      end
    end
  end

  def find_set(v)
    if v != @parents[v]
      @parents[v] = find_set(@parents[v])
    end

    @parents[v]
  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