def league(n, graph)
  queue = []
  check = ->(i) {
    return if graph[i].empty?
    opponent = graph[i][0]
    return if graph[opponent].empty?
    if graph[opponent][0] == i
      queue << [i, opponent]
    end
  }

  0.upto(n - 1) { |i| check.call(i) }
  ans = 0
  until queue.empty?
    ans += 1
    prev_queue = queue
    queue = []
    until prev_queue.empty?
      pair = prev_queue.shift
      if graph[pair[0]][0] == pair[1] && graph[pair[1]][0] == pair[0]
        graph[pair[0]].shift
        graph[pair[1]].shift
        check.call(pair[0])
        check.call(pair[1])
      end
    end
  end

  ans = -1 if graph.any? { |e| !e.empty? }
  puts ans
end

n = gets.to_i
graph = []
n.times{ graph << gets.split.map{|i| i.to_i - 1 } }
puts league(n, graph)