# class dfs
#   def init
#     @time = 1
#     @
#   end
# end

def dfs(src)
  D[src] = $time
  $time += 1
  (0..N - 1).each do |dst|
    if D[dst] == nil && G[src - 1][dst - 1]
      Back[dst] = src
      dfs(dst) 
    end
  end
  F[src] = $time
  $time += 1
  dfs(Back[src])
end

N = gets.to_i
G = (1..n).map{ Array.new(n, 0) }
n.times do
  u, k, *v = gets.split(' ').map(&:to_i)
  v.each do |vi|
    G[u - 1][vi - 1] = 1
  end
end

D = Array.new(n)
F = Array.new(n)
Back = Array.new(n) #??????n????????¨?????????????????\??????

$time = 1

(0..n-1).each do |id|
  dfs(id) unless D[id]
end