program main
  implicit none
  integer i,N,fin,pin,ans,j,con
  integer,allocatable :: A(:,:),B(:),tired(:)
  read(*, *) N
  allocate(A(N,N))
  allocate(B(N))
  allocate(tired(N))
  do j= 1,N
    read(*, *) (A(j,i), i = 1,N-1)
    A(j,N) = 0
  end do
  B = 1
  tired = 0
  pin = 0
  fin = 1
  ans = 1
  do
    do i = 1,N
      con = A(i,B(i))
      if (A(con,B(con)) == i .and. tired(con) == 0 .and. tired(i) == 0) then
        B(i) = B(i) + 1
        B(con) = B(con) + 1
        tired(con) = 1
        tired(i) = 1
        pin = 1
      end if
    end do
    if (pin == 0) then
      ans = -1
      exit
    elseif (sum(B) == N**2) then
      exit
    else
      ans = ans + 1
      tired = 0
      pin = 0
    end if
  end do
    
  write(*, *) ans
end program main
