# coding: utf-8

def update(t,old,new)
  t.size.times do |i|
    t[i] = new if t[i] == old    
  end
end

n,m,k = $stdin.gets.split.map {|x| x.to_i}
t = Array.new(n,nil)

tmp_idx = m + 1

k.times do |kk|
  c = $stdin.gets.split.map {|x| x.to_i}
  if c[0] == 1 then
    i1 = c[1]-1
    i2 = c[2]-1
    
    if t[i1] == nil then
      if t[i2] == nil then
        t[i1] = tmp_idx
        t[i2] = tmp_idx
        tmp_idx += 1
      else
        t[i1] = t[i2]
      end
    elsif t[i1] <= m then # 正idx
      if t[i2] == nil then
        t[i2] = t[i1]
      elsif t[i2] <= m and t[i2] != t[i1] then
        printf("%d\n",kk+1)
        exit
      elsif t[i2] >  m then
        update(t,t[i2],t[i1])
      end
    else # 仮idx
      if t[i2] == nil then
        t[i2] = t[i1]
      elsif t[i2] <= m then
        update(t,t[i1],t[i2])
      else
        old = [t[i1],t[i2]].max
        new = [t[i1],t[i2]].min
        update(t,old,new)
      end
    end
    
  elsif c[0] == 2 then
    i = c[1] - 1 # idx
    if t[i] == nil then
      t[i] = c[2]
    else
      if t[i] >= m + 1 then # 仮idxが設定されていたら
        tmp = t[i]
        update(t, tmp, c[2])
      else
        if t[i] != c[2] then # 矛盾
          printf("%d\n",kk+1)
          exit
        end
      end
    end
  end
end

printf("0\n")


