n = gets.to_i
a = []

n.times do |i|
  a << gets.split.map(&:to_i)
end

fin_match = []
n.times do
  fin_match << 0
end

ans = 0
while fin_match.all? {|i| i != n }
  ans += 1
  today = []
  n.times do
    today << 0
  end
  order1 = fin_match.map.with_index do |fin, index|
    [fin, index]
  end
  order2 = order1.sort.map do |p|
    p[1]
  end
  order2.each do |i|
    next_comp = a[i][0]
    next if next_comp.nil?
    if today[i] == 0 && fin_match[i] != n && today[next_comp-1] == 0 && fin_match[next_comp-1] != n
      fin_match[i] += 1
      today[i] += 1
      b = a[i].shift - 1
      fin_match[b] += 1
      today[b] += 1
    end
  end
  if ans == n*(n-1)
    ans = -1
    break
  end
end
p ans
