fun main(args : Array<String>) {
    val (n, q) = readLine()!!.split(" ").map { it.toInt() }
    val uf = UnionFind(n)

    repeat(q) {
        val (t, u, v) = readLine()!!.split(" ").map { it.toInt() }

        if (t == 0) {
            uf.merge(u, v)
        } else {
            println(if (uf.isSame(u, v)) 1 else 0)
        }
    }
}

// Union Find
// NOTE: initSize is the number of elements
private data class UnionFind(val initSize: Int) {
    private val parents = IntArray(this.initSize) { it }
    private val sizes = LongArray(this.initSize) { 1L }

    fun root(idx: Int): Int {
        if (idx == parents[idx]) return idx

        val rootP = root(parents[idx])
        parents[idx] = rootP
        return rootP
    }

    // NOTE: merge to the larger group
    fun merge(p1: Int, p2: Int) {
        if (this.isSame(p1, p2)) return

        val rootP1 = root(p1)
        val rootP2 = root(p2)

        if (this.sizes[rootP1] >= this.sizes[rootP2]) {
            this.sizes[rootP1] += this.sizes[rootP2]
            this.parents[rootP2] = this.parents[rootP1]
        } else {
            this.sizes[rootP2] += this.sizes[rootP1]
            this.parents[rootP1] = this.parents[rootP2]
        }
    }

    fun isSame(p1: Int, p2: Int): Boolean {
        return this.root(p1) == this.root(p2)
    }

    fun size(idx: Int): Long {
        return this.sizes[this.root(idx)]
    }
}