object Main {

  class UnionFind(val n: Int) {
    private[this] val par = Array.range(0, n)

    def root(x: Int): Int =
      if(par(x) == x) x else { par(x) = root(par(x)); par(x) }

    def same(x: Int, y: Int): Boolean = root(x) == root(y)
    def unite(x: Int, y: Int): Unit = par(root(x)) = root(y)
  }

  def main(args: Array[String]): Unit = {
    val Array(n,q,_*) = readLine.split(" ").map(_.toInt)
    val uf = new UnionFind(n)

    for(i <- 1 to q){
      val Array(com,x,y,_*) = readLine.split(" ").map(_.toInt)
      if(com==0)uf.unite(x,y)
      else println(if(uf.same(x,y)) 1 else 0)
    }
  }

}