class Graph
	def initialize
		@nodes = Set.new
		@edges = Set.new
	end

	def add_node(label = nil)
		Node.new(label)
	end

	def add_edge(u, v)
		e = Edge.new(u, v)
		u.outgoings << e
		v.incomings << e
		e
	end
	
	def del_edge(e)
		u, v = e.from, e.to
		u.outgoings.delete(e)
		v.incomings.delete(e)
		e
	end

	class Node
		def initialize(label)
			@label = label
			@outgoings = Set.new
			@incomings = Set.new
		end
		
		def to_s
			@label
		end
		attr_accessor :incomings, :outgoings
		attr_reader :label
	end

	class Edge
		def initialize(u, v)
			@from = u	
			@to = v
		end
		attr_reader :from, :to
	end

	def Graph::get_path_bfs(s, t)
		path_edge = {}
		reached = Set[s]
		queue = [s]
		while !queue.empty? 
			u = queue.shift
			u.outgoings.each{|e|
				v = e.to
				next if reached.include?(v)	
				reached << v
				queue << v
				path_edge[v] = e

				if v == t
					path = []
					while e = path_edge[v]
						path.unshift e
						v = e.from
					end
					return path
				end
			}
		end	
		return nil
	end

	def Graph::get_path_dfs(s, t, reached = Set.new)
		reached << s	
		s.outgoings.each{|e|
			v = e.to
			next if reached.include?(v)
			return [e] if v == t
			
			path = get_path_dfs(v, t, reached) 
			return [e] + path if path
		}
		nil
	end

	def Graph::reachable_from(s, reached = Set.new, root = true)
		reached << s	
		s.outgoings.each{|e|
			v = e.to
			next if reached.include?(v)
			reachable_from(v, reached, false) 
		}
		root ? reached : nil
	end
end

loop {
	n = gets.to_i
	break if n == 0

	schedule = (0..29).map{[]}
	(0..n-1).each{|member|
		gets.split(/\s+/)[1..-1].map(&:to_i).each{|day|
			schedule[day-1] << member
		}
	}

	g = Graph.new
	day_nodes = (0..29).map{|day|
		(0..n-1).map{|i| g.add_node(i)}
	}

	(0..29).each{|day|
		members = schedule[day]
		nodes = day_nodes[day]
		for i in 0..members.size-1
			u = nodes[members[i]]
			v = nodes[members[(i+1) % members.size]]
			g.add_edge(u,v)
		end
		if day > 0
			(0..n-1).each{|i|
				g.add_edge(nodes[i], day_nodes[day-1][i])
			}
		end
	}

	x_day = -1
	(0..29).each{|day|
		members = schedule[day]
		p "day: #{day}, member: #{members}"
		next if members.empty?
		s = day_nodes[day][members[0]]
		nodes = Graph::reachable_from(s)
		reached_index = Set.new
		nodes.each{|v| reached_index << v.label}
		if reached_index.size == n
			x_day = day + 1
			break
		end
	}
	puts x_day
}