N = gets.chomp.to_i
a = Array.new(N+1){Array.new(N-1)}
x = Array.new(N+1){Array.new(N-1,1)}
for i in 1..N do
    str = gets.split
    for j in 0...N-1 do
        a[i][j] = str[j].to_i
    end
end
max = 1
max2 = 1

for i in 1..N do
    for j in 0...N-1 do
        last = j == 0 ? 0 : last
        p = a[i][j]
        q = p > i ? i : p
        r = ((p-i).abs)-1
        x[q][r] = (last + 1) > x[q][r] ? last + 1 : x[q][r]
        last = x[q][r]
    end
    max = last > max ? last : max
end

for i in 1..N do
    for j in 0...N-1 do
        last = j == 0 ? 0 : last
        p = a[i][j]
        q = p > i ? i : p
        r = ((p-i).abs)-1
        x[q][r] = (last + 1) > x[q][r] ? last + 1 : x[q][r]
        last = x[q][r]
    end
    max2 = last > max2 ? last : max2
end

answer = max == max2 ? max : -1 
puts answer