n, m, k = gets.split.map(&:to_i)
h = {}
(1..n).each do |i|
    h[i] = i
end

def root(h, i)
    if h[i].class == String
        return h[i]
    elsif h[i] == i
        return i
    else
        return h[i] = root(h, h[i])
    end
end

ans = nil
k.times do |i|
    t, x, y = gets.split.map(&:to_i)
    if t == 1
        # x, y = y, x if rand(2) == 1
        r1 = root(h, x)
        r2 = root(h, y)
        if r1.class == String and r2.class == String and r1 != r2
            ans = i + 1
            break
        elsif r1.class == String and r2.class == String

        elsif r1.class == String
            h[r2] = r1
        else
            h[r1] = r2
        end
    else
        r = root(h, x)
        if r.class == String and r != y.to_s
            ans = i + 1
            break
        elsif r.class == String
        else
            h[r] = y.to_s
        end
    end
end
puts ans ? ans : 0

