class UnionFind
  attr :parent, :rank

  def initialize
    @parent = Hash.new
    @rank = Hash.new
  end

  def make(x)
    @parent[x] = x
    @rank[x] = 0
  end

  def union(a, b)
    x, y = find(a), find(b)
#     x, y = y, x if @rank[x] < @rank[y]
    x, y = y, x unless x < y
    @parent[y] = x
    @rank[x] += 1 if @rank[x] == @rank[y]
  end

  def find(x)
    @parent[x] == x ? x : @parent[x] = find(@parent[x])
  end

  def size
    @parent.keys.map{|k| find(k)}.sort.uniq.size
  end
end

N, M, K = gets.split.map &:to_i

group = UnionFind.new
1.upto(N) do |i|
  group.make(i)
end
1.upto(M) do |i|
  group.make(-i)
end

ans = 0
1.upto(K) do |i|
  a, b, c = gets.split.map &:to_i
  case a
    when 1
      x = group.find(b)
      y = group.find(c)
      if x < 0 && y < 0 && x != y
        ans = i
        break
      end
      group.union(x, y)
    when 2
      x = group.find(b)
      y = -c
      if x < 0 && x != y
        ans = i
        break
      end
      group.union(x, y)
  end
end
p ans