One day, Twilight Sparkle is interested in how to sort a sequence of integers a1, a2, ..., an in non-decreasing order. Being a young unicorn, the only operation she can perform is a unit shift. That is, she can move the last element of the sequence to its beginning:

a1, a2, ..., an → an, a1, a2, ..., an - 1.

Help Twilight Sparkle to calculate: what is the minimum number of operations that she needs to sort the sequence?

Input

The first line contains an integer n (2 ≤ n ≤ 105). The second line contains n integer numbers a1, a2, ..., an (1 ≤ ai ≤ 105).

Output

If it's impossible to sort the sequence output -1. Otherwise output the minimum number of operations Twilight Sparkle needs to sort it.

Examples

Input

2
2 1


Output

1


Input

3
1 3 2


Output

-1


Input

2
1 2


Output

0
'''
def main():
    from sys import stdin,stdout
if __name__=='__main__':
    main()
'''
#401
'''
def main():
    import sys
    a,b=map(int,sys.stdin.readline().split())
    if a>b:
        if a-b==1:
            s=''
            for i in range(a+b):
                if i&1:
                    s+='1'
                else:
                    s+='0'
        else:
            s='-1'
    elif b>a:
        if b<=2*(a+1):
            s=''
            if b-a==1:
                for i in range(a+b):
                    if i&1:
                        s+='0'
                    else:
                        s+='1'
            else:
                n=b-a-1
                i=0
                flag=0
                while i<a+b:
                    if flag&1:
                        s+='0'
                    else:
                        if n:
                            s+='11'
                            n-=1
                            i+=1
                        else:
                            s+='1'
                    i+=1
                    flag=~flag
        else:
            s='-1'
    else:
        s=''
        for i in range(a+b):
            if i&1:
                s+='1'
            else:
                s+='0'
    sys.stdout.write(s)
if __name__=='__main__':
    main()
'''
#508B
'''
def main():
    import sys
    n=sys.stdin.readline().strip()
    lodd=int(n[-1])
    odd=0
    even=0
    s=''
    j=0
    for i in n:
        if ord(i)&1:
            odd+=1
        else:
            even+=1
            feven=int(i)
            feindex=j
            if(feven<lodd):
                break
        j+=1
    if even:
        for i in range(len(n)):
            if i==feindex:
                sys.stdout.write(n[-1])
            elif i==len(n)-1:
                sys.stdout.write(str(feven))
            else:
                sys.stdout.write(n[i])
    else:
        sys.stdout.write('-1')
if __name__=='__main__':
    main()
'''
#447B
'''
def main():
    import sys
    s=sys.stdin.readline().strip()
    n=int(sys.stdin.readline())
    tup=tuple(map(int,sys.stdin.readline().split()))
    maxim=max(tup)
    score=0
    for i in range(len(s)):
        score+=(i+1)*tup[ord(s[i])-97]
    d=n*(2*len(s)+n+1)//2
    score+=maxim*d
    sys.stdout.write(str(score))
if __name__=='__main__':
    main()
'''
#507B
'''
def main():
    from sys import stdin,stdout
    from math import ceil,sqrt
    def sqr(a):
        return a*a
    r,x,y,a,b=map(int,stdin.readline().split())
    mid=((x+a)/2,(y+b)/2)
    #print(mid)
    dis=sqrt(sqr(x-mid[0])+sqr(y-mid[1]))
    #print(dis)
    n=ceil(dis/r)
    stdout.write(str(n)+'\n')
if __name__=='__main__':
    main()
'''
#454B
def main():
    from sys import stdin,stdout
    n=int(stdin.readline())
    tup=tuple(map(int,stdin.readline().split()))
    minim=min(tup)
    maxim=max(tup)
    maximindex=tup.index(maxim)
    flag=1
    i=0
    pv=tup[-1]
    while i<n:
        if tup[i]<pv:
            if tup[i]==minim and pv==maxim:
                l=i+1
                while l<max(maximindex,n):
                    if tup[l]<tup[l-1]:
                        flag=0
                        break
                    l+=1
                break
            else:
                flag=0
                break
        pv=tup[i]
        i+=1
    #print(i)
    if flag:
        if i:
            stdout.write(str(n-i))
        else:
            stdout.write('0')
    else:
        stdout.write('-1')
if __name__=='__main__':
    main()
