require 'set'

N = gets.to_i
A = Array.new(N) { gets.split.map(&:to_i) }

G = A.map.with_index(1) {|a, i| a.map {|j| [i, j].sort.join(?-)}}

q = Array.new(N)
day = 1
loop do
  q = q.map.with_index do |x, i|
    next(x) if x
    next if G[i].empty?
    G[i].shift
  end

  set = q.group_by {|x| x}.select {|k, v| v.size > 1}.map(&:first).to_set
  after_q = q.map {|x| x unless set.include?(x)}

  break(day = -1) if after_q.none?(&:nil?)
  break if G.all?(&:empty?)

  q = after_q
  day += 1
end

puts day
