
sealed class UnionFindTree{
    abstract fun parent(): UnionFindTree
    abstract fun isSameUnion(that: UnionFindTree): Boolean
    abstract fun join(that: UnionFindTree): Unit
    abstract fun hasBoss(): Boolean
    class Boss: UnionFindTree() {
        override fun hasBoss(): Boolean {
            return true
        }

        override fun parent(): UnionFindTree {
            return this
        }

        override fun isSameUnion(that: UnionFindTree): Boolean{
            return that.parent() == this
        }

        override fun join(that: UnionFindTree) {
            if (!isSameUnion(that)) {
                val parent = that.parent()
                when (parent) {
                    is Member -> parent.join(this)
                    else -> throw Exception("Boss Can't Belong Same Union With The Other Boss")
                }
            }
        }
    }

    class Member: UnionFindTree() {
        override fun hasBoss(): Boolean {
            return parent() is Boss
        }

        private var mParent: UnionFindTree? = null
        override fun parent(): UnionFindTree {
            return when(mParent){
                null -> this
                else -> {
                    mParent = mParent?.parent()
                    mParent!!
                }
            }
        }

        override fun isSameUnion(that: UnionFindTree): Boolean {
            return parent() == that.parent()
        }

        override fun join(that: UnionFindTree) {
            if (!isSameUnion(that)){
                val thisParent = parent()
                val thatParent = that.parent()
                if (thisParent is Member){
                    thisParent.mParent = thatParent
                }else if (thatParent is Member){
                    thatParent.mParent = thisParent
                }else throw Exception("Boss Can't Belong Same Union With The Other Boss")
            }
        }
    }
}

fun main(args:Array<String>): Unit {
    val (n, m, k) = readLine()!!.trim().split(' ').map(String::toInt)
    val students = Array(n){UnionFindTree.Member()}
    val clubs = Array(m){UnionFindTree.Boss()}
    repeat(k){count ->
        val (o, a, b) = readLine()!!.trim().split(' ').map(String::toInt)
        when(o){
            1 -> if (students[a - 1].hasBoss() && students[b - 1].hasBoss() && !students[a - 1].isSameUnion(students[b - 1])) {
                println(count + 1)
                return
            }else {
                students[a - 1].join(students[b - 1])
            }
            2 -> if (students[a - 1].hasBoss() && !students[a - 1].isSameUnion(clubs[b - 1])) {
                println(count + 1)
                return
            }else {
                students[a - 1].join(clubs[b - 1])
            }
        }
    }
    println(0)
}
