n,m=map(int,input().split()) p=list(map(int,input().split())) B=450 st1=[0]*B*B st2=[0]*B f=0 for v in p[::-1]: l=0 r=v yl=l//B yr=l//B if yl==yr: f+=sum(st1[l:r+1]) else: f+=sum(st1[l:yl*B+B]) f+=sum(st2[yl+1:yr]) f+=sum(st1[yr*B:r+1]) st1[v]+=1 st2[v//B]+=1 m*=(f+m-1)//m print(m if (m-f)%2==0 else -1)