class UnionFind
	def initialize
		@obj_to_node = {}
	end

	def size(x = nil)
		x ? @obj_to_node[x].root.size : @obj_to_node.size
	end

	class Node
		def initialize(obj)
			@obj = obj
			@parent = self
			@size = 1
		end

		def root
			return self if @parent == self
			@parent = @parent.root
		end	

		attr_accessor :parent, :size
	end

	def union(x, y)
		u = (@obj_to_node[x] ||= Node.new(x)).root
		v = (@obj_to_node[y] ||= Node.new(y)).root
		return if u == v
		u.parent = v.root
		v.size += u.size
	end
		
end

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

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

	u = UnionFind.new
	gathered = -1
	(1..30).each{|day|
		schedule[day].each_cons(2){|x, y|
			u.union(x, y)
			if u.size(x) == n
				gathered = day
				break
			end
		}
		break if gathered > 0
	}
	puts gathered 
}