n=gets.to_i
a=(0...n).map{gets.split.map{|s|s.to_i-1}}

count=0
loop do
    tmp = (0...n).map{false}
    change=false
    (0...n).each{|i|
        if a[i].size>0
            x=a[i][0]
            if tmp[i] == false && tmp[x]==false && a[x].size > 0 && a[x][0] == i
                a[i].shift
                a[x].shift
                tmp[i]=true
                tmp[x]=true
                change=true
            end
        end
    }
    break if change == false;
    count+=1
end

if a.any? {|aa|aa.size != 0}
    print -1
else
    print count
end