integer N
integer,allocatable,dimension(:,:)::A
integer,allocatable,dimension(:)::B,Anum
integer aite
integer cnt
logical nogame
read*,N
allocate(A(N,N-1),B(N),Anum(N))
Anum=1
do i=1,N
  read*,A(i,:)
end do

cnt=0
do i=1,N*N
  nogame=.true.
  B=0
  do j=1,N
    if(B(j)==0.and.Anum(J)/=N)then
      aite=A(J,Anum(J))
      if(A(aite,Anum(aite))==J.and.B(aite)==0)then
        Anum(j)=Anum(j)+1
        Anum(aite)=Anum(aite)+1
        nogame=.false.
        B(j)=1;B(aite)=1
        if(Anum(J)==N)cnt=cnt+1
        if(Anum(aite)==N)cnt=cnt+1
      endif
    endif
  end do
  if(cnt==N)then
    print"(i0)",i
    stop
  endif
  if(nogame)then
    print"(i0)",-1
    stop
  endif
end do
end