N = gets.chomp.to_i
a = {}
N.times do |i|
  a[i+1] = gets.chomp.split(" ").map(&:to_i)
end

require "set"

days = 0
cnt = 0
vs = Set.new
players = Set.new((1..N))
total_cnt = N * (N - 1) / 2

until players.empty?
  days += 1
  players.each do |i|
    next if vs.include?(i) || a[i].empty?

    i2 = a[i].first
    next if i != a[i2].first || vs.include?(i2)

    vs << a[i].shift
    vs << a[i2].shift
    cnt += 1
  end
  if cnt >= total_cnt
    puts days
    exit 0
  end
  players = vs
  vs = Set.new
end

puts "-1"
