class Factor
  attr_reader :id
  def initialize(id)
    @id = id
    @parent = nil
  end

  def union
    if @parent
      @parent.union
    else
      self
    end
  end

  def unite(other)
    @parent.union.unite(other.union) if @parent
    @parent = other.union
  end

  def same(other)
    if union == other.union
      puts 1
    else
      puts 0
    end
  end

  def ==(other)
    @id == other.id
  end
  
end
class String
  def com(x, y)
    x.send(self, y)
  end
end
def main
  n, q = gets.split(" ").map(&:to_i)
  array = Array.new(n){|i|Factor.new(i)}
  comand = ["unite", "same"]
  q.times do
    arg = gets.split(" ").map(&:to_i)
    comand[arg[0]].com(array[arg[1]], array[arg[2]])
  end
end
$stdin = File.open('data_set.txt')
main
$stdin.close