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 parent: Array[Int] = Array.tabulate(size)(i => i)
  val rank: Array[Int] = Array.fill(size)(0)
  def union(x: Int, y: Int): Unit = {
    val i = find(x)
    val j = find(y)
    if (rank(i) > rank(j)) parent(j) = i
    else {
      parent(i) = j
      rank(i) += (if (rank(i) == rank(j)) 1 else 0)
    }
  }
  def find(x: Int): Int = parent(x) match {
    case p if x == p => x
    case p =>
      val i = find(p)
      parent(x) = i
      rank(x) = 0
      i
  }
  def same(x: Int, y: Int):Boolean = find(x) == find(y)
}