n = '3'.to_i
a = []

n.times do |i|
  a << gets.split.map(&:to_i)
end

ans = 0
while true
  break if a.all? {|i| i[0].nil? }
  ans += 1
  today = []
  n.times do
    today << 0
  end
  n.times do |i|
    next_comp = a[i][0]
    next if next_comp.nil?
    next if a[next_comp-1][0].nil?
    if today[i] == 0 &&a[next_comp-1][0] == i + 1 && today[next_comp-1] == 0
      today[i] += 1
      b = a[i].shift - 1
      today[b] += 1
      a[b].shift
    end
  end
  if ans == n*(n-1)
    ans = -1
    break
  end
end
p ans
