object Main extends App {
  val Seq(n,q): Seq[Int] = io.StdIn.readLine().split(" ").map(_.toInt)
  val ds = new DisjointSets(n)

  (0 until q) foreach {_ =>
    io.StdIn.readLine().split(" ").map(_.toInt) match {
      case Array(0, x, y) => ds.union(x, y)
      case Array(1, x, y) => println(if (ds.same(x,y)) 1 else 0)
    }
  }
}

class DisjointSets(size: Int) {
  val sets: Array[Tree] = Array.tabulate(size)(make)
  def make(x: Int): Node = Node(x, 0)

  def union(x: Int, y: Int): Unit = {
    val i = find(x)
    val j = find(y)
    val rootX = sets(i)
    val rootY = sets(j)

    if (rootX.rank > rootY.rank) sets(j) = Node(i, rootY.rank)
    else sets(i) = Node(j, rootX.rank + (if (rootX.rank == rootY.rank) 1 else 0))
  }

  def find(x: Int): Int = sets(x) match {
    case Node(p, _) if x == p => x
    case Node(p, _) => {
      val i = find(p)
      sets(x) = Node(i, 0)
      i
    }
  }

  def same(x: Int, y: Int):Boolean = find(x) == find(y)

  sealed trait Tree {val rank: Int}
  case class Node(parent: Int, rank: Int) extends Tree
}