# frozen_string_literal: true

def main
  def check(table, queue, idx)
    return if table[idx].empty?

    opponent = table[idx][0]
    return if table[opponent].empty?

    return unless table[opponent][0] == idx

    idx, opponent = opponent, idx if idx > opponent
    queue << [idx, opponent]
  end
  n = gets.to_i

  a = []

  n.times do
    a << gets.chomp.split(' ').map(&:to_i).map { |i| i - 1 }
  end

  queue = []

  n.times do |i|
    check(a, queue, i)
  end

  day = 0
  until queue.empty?
    day += 1
    queue.uniq!

    prev_q = []
    prev_q, queue = queue, prev_q

    prev_q.each do |pair|
      first, second = pair

      a[first].shift
      a[second].shift

      check(a, queue, first)
      check(a, queue, second)
    end

  end

  a.each do |elem|
    unless elem.empty?
      puts '-1'
      return 0
    end
  end

  puts day
end
main
