n, m, k = gets.split.map(&:to_i)

$uf = (0..n).to_a

def root(i)
  return i if $uf[i]==i
  return i if $uf[i]<0
  $uf[i] = root($uf[i])
  return $uf[i]
end

def same(i, j)
  return root(i) == root(j)
end

def group(i)
  x = root(i)
  g = $uf[x]
  return -g if g<0
  return 0
end

def set_group(i, g)
  x = root(i)
  $uf[x] = -g
end

def unite(i, j)
  x, y = root(i), root(j)
  g, h = $uf[x], $uf[y]
  if g<0
    $uf[y] = x
  else
    $uf[x] = y
  end
end

(1..k).each{|i|
  r, a, b = gets.split.map(&:to_i)
  if r==1
    ga, gb = group(a), group(b)
    if ga>0 and gb>0 and ga!=gb
      puts i
      exit
    else
      unite(a, b)
    end
  elsif r==2
    g = group(a)
    if g==0
      set_group(a, b)
    elsif g>0 && g!=b
      puts i
      exit
    end
  end
}
puts 0
