n = gets.to_i
a = []
y = []

n.times do |i|
  a[i] = gets.split("\s").map(&:to_i)
  y[i] = i
end

can = true
flag = true
result = 0

while(! a.all?{|k| k == []})
  yet = y.dup
  if(!flag)
    puts -1
    can = false
    break
  end
  flag = false
  n.times do |i|
    next if(a[i].nil?)
    i.times do |j|
      next if(a[i].nil? || i == j)
      if(i+1 == a[j][0] && yet[j] >= 0)
        a[i].shift
        a[j].shift
        flag = true
yet[j] = -1
        next
      end
    end
  end
  result += 1
end
if(can)
  puts result+1
end