#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cctype>
#include<cstdlib>
#include<algorithm>
#include<bitset>
#include<vector>
#include<list>
#include<deque>
#include<queue>
#include<map>
#include<set>
#include<stack>
#include<cmath>
#include<sstream>
#include<fstream>
#include<iomanip>
#include<ctime>
#include<complex>
using namespace std;
#define M_PI 3.141592653589793238
#define EPS 1e-9
#define LINF 1e30  //long long int
#define IINF 1000000000  //int
#define CP complex<long double>
#define REP(i,a,b) for(i=a;i<b;i++)
#define rep(i,n) REP(i,0,n)
static const int dx[] = { 1, 0, -1, 0 }, dy[] = { 0, -1, 0, 1 };
long double ccw(long double kari, long double kari1, long double kari2, long double kari3){  //内積
    return kari*kari3 - kari1*kari2;
}
long long int oneday(int year, int month, int day){  //1年1月1日からの、経過日数
    return 365 * year + (year / 4) - (year / 100) + (year / 400) + (306 * (month + 1) / 10) + day - 429;
}
long long int ppow(int a1, int b1){  //a^b
    long long int rr = a1;
    long long int r = 1;
    while (b1>0){
        if (b1 & 1){
            r *= rr;
        }
        rr *= rr;
        b1 >>= 1;
    }
    return r;
}
class seg{
    vector<int> s1;
    int siz; //size
    seg(int kari){  //size
        siz = kari;
        s1.assign(siz, -1);
    }
    void add(int b, int n){  //bにnを加える
        b += siz - 1;
        s1[b] = n;
        while (b > 0){
            b = (b - 1) / 2;
            /*if (s1[b] == -1 || s1[b] > n){  //min

            }
            s1[b] += n;  //add*/
        }
    }
    int ch(){

    }
    int find(int a, int b){  //a以上b未満の中で

    }
};
/*宣言*/
#define MAX_N 100002
int n = 0;
vector<int> v;
int flag[MAX_N];
int main(){
    cin >> n;
    int countt = 0;
    int m = 0;
    int z = 0;
    int ok = 0;
    int mintn = -1;
    int minti = -1;
    for (int i = 0; i < n; i++){
        int kari = 0;
        cin >> kari;
        if (mintn == -1 || mintn >= kari){
            minti = i;
            mintn = kari;
        }
        if (i == 0){
            m = kari;
            z = kari;
        }
        else{
            if (z > kari){
                countt++;
            }
            if (countt >= 1 && m < kari){
                ok = 1;
                break;
            }
            z = kari;
        }
    }
    if (ok || countt >= 2){
        cout << -1 << endl;
    }
    else{
        if (minti == 0){
            cout << 0 << endl;
        }
        else{
            cout << (n - minti)<< endl;
        }
    }
    cin.get();
    cin.get();
    return 0;
}


#include <iostream>
#include <string.h>
#include <algorithm>

using namespace std;

int positions[100000], sequence1[100000], sequence2[100000], position2, sequence3[100000];

int main(int argc, const char * argv[])
{
    int n=0, tmp=0, changes=0, distance=0;
    cin>>n;
    for (int i=0;i<n;i++){
        cin>>sequence1[i];
        sequence2[i] = sequence1[i];
    }
    int errors;
    for (int i=1;i<n;i++){
        if (sequence1[i] < sequence1[i-1]){
            errors++;
            distance = n-i;
        }
        if (i == n-1){
            if (sequence1[i] > sequence1[0]){
                errors++;
                distance = n-1;
            }
        }
        if (errors == 2){
            cout<<"-1";
            exit(0);
        }
    }
    cout<<distance;
    return 0;
}


#include <iostream>
#include <string.h>
#include <algorithm>

using namespace std;

int sequence1[100000];

int main(int argc, const char * argv[])
{
    int n=0, distance=0;
    cin>>n;
    for (int i=0;i<n;i++){
        cin>>sequence1[i];
    }
    int errors=0;
    for (int i=1;i<n;i++){
        if (sequence1[i] < sequence1[i-1]){
            if (errors == 1){
                cout<<"-1";
                exit(0);
            }
            errors++;
            distance = n-i;
        }
        if (i == (n-1)){
            if (errors != 0){
                if (sequence1[0] < sequence1[i]){
                    if (errors == 1){
                        cout<<"-1";
                        exit(0);
                    }
                                        errors++;
                    distance = n-1;
                }
            }
        }
    }
    cout<<distance;
    return 0;
}


#include <iostream>
#include <string.h>
#include <algorithm>

using namespace std;

int sequence1[100000];

int main(int argc, const char * argv[])
{
    int n=0, distance=0;
    cin>>n;
    for (int i=0;i<n;i++){
        cin>>sequence1[i];
    }
    int errors=0;
    for (int i=1;i<n;i++){
        if (sequence1[i] < sequence1[i-1]){
            if (errors == 1){
                cout<<"-1";
                exit(0);
            }
            errors++;
            distance = n-i;
        }
        if (i == (n-1)){
            if (sequence1[0] < sequence1[i]){
                if (errors == 1){
                    cout<<"-1";
                    exit(0);
                }
                errors++;
                distance = n-1;
            }
        }
    }
    cout<<distance;
    return 0;
}


#include <iostream>
#include <string.h>
#include <algorithm>

using namespace std;

int sequence1[100000];

int main(int argc, const char * argv[])
{
    int n=0, distance=0;
    cin>>n;
    for (int i=0;i<n;i++){
        cin>>sequence1[i];
    }
    int errors=0;
    for (int i=1;i<n;i++){
        if (sequence1[i] < sequence1[i-1]){
            if (errors == 1){
                cout<<"-1";
                exit(0);
            }
            errors++;
            distance = n-i;
        }
        if (i == (n-1)){
            if (errors == 0){
                if (sequence1[0] < sequence1[i]){
                    if (errors == 1){
                        cout<<"-1";
                        exit(0);
                    }
                                        errors++;
                    distance = n-1;
                }
            }
        }
    }
    cout<<distance;
    return 0;
}


#include <iostream>
#include <string.h>
#include <algorithm>

using namespace std;

int sequence1[100000];

int main(int argc, const char * argv[])
{
    int n=0, distance=0;
    cin>>n;
    for (int i=0;i<n;i++){
        cin>>sequence1[i];
    }
    int errors=0;
    for (int i=1;i<n;i++){
        if (sequence1[i] < sequence1[i-1]){
            if (errors == 1){
                cout<<"-1";
                exit(0);
            }
            errors++;
            distance = n-i;
        }
        if (i == (n-1)){
            if (errors == 0){
                if (sequence1[0] < sequence1[i]){
                    if (errors == 1){
                        output<<"-1";
                        exit(0);
                    }
                                        errors++;
                    distance = n-1;
                }
            }
        }
    }
    cout<<distance;
    return 0;
}


#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <cmath>
using namespace std;

typedef long long int lint;

int main(){
	int n;
	cin >> n;
	bool reversed = false;
	lint first, next, counter = 0;
	cin >> first;
	for (int i = 1; i < n; i++){
		cin >> next;
		if (reversed) counter++;
		if (next < first){
			if (reversed){
				cout << -1;
				return 0;
			}
			reversed = true;
			counter++;
		}
	}
	if (!reversed) cout << 0;
	else if (next > first) cout << -1;
	else cout << counter;

	return 0;
}


#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <cmath>
using namespace std;

typedef long long int lint;

int main(){
	int n;
	cin >> n;
	vector <int> Vec;
	for (int i = 0; i < n; i++){
		lint temp;
		cin >> temp;
		Vec.push_back(temp);
	}
	for (int i = 0; i < n; i++){
		if (is_sorted(Vec.begin(),Vec.end())){
			cout << i;
			return 0;
		}
		Vec.insert(Vec.begin(), Vec.back());
		Vec.pop_back();
	}

	cout << -1;	//cannot sort

	return 0;
}


#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <cmath>
using namespace std;

typedef long long int lint;

int main(){
	int n;
	cin >> n;
	bool reversed = false;
	lint first, prev, next, counter = 0;
	cin >> first;
	next = first;
	for (int i = 1; i < n; i++){
		prev = next;
		cin >> next;
		if (reversed) counter++;
		if (next < prev){
			if (reversed){
				cout << -1;
				return 0;
			}
			reversed = true;
			counter++;
		}
	}
	if (!reversed) cout << 0;
	else if (next > first) cout << -1;
	else cout << counter;

	return 0;
}


#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <cmath>
using namespace std;

typedef long long int lint;

int main(){
	int n;
	cin >> n;
	vector <int> Vec;
	for (int i = 0; i < n; i++){
		lint temp;
		cin >> temp;
		Vec.push_back(temp);
	}
	for (int i = 0; i < n; i++){
		if (is_sorted(Vec.begin(),Vec.end())){
			cout << i;
			return 0;
		}
		Vec.insert(Vec.begin(), Vec.back());
		Vec.pop_back();
	}

	cout << -1;	//cannot sort
	return 0;
}


#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <cmath>
using namespace std;

typedef long long int lint;

int main(){
	int n;
	cin >> n;
	vector <int> Vec;
	for (int i = 0; i < n; i++){
		lint temp;
		cin >> temp;
		Vec.push_back(temp);
	}
	for (int i = 0; i < n; i++){
		if (is_sorted(Vec.begin(),Vec.end())){
			cout << i;
			return 0;
		}
		Vec.insert(Vec.begin(), Vec.back());
		Vec.pop_back();
	}
	cout << -1;	//cannot sort

	return 0;
}


#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>
#include<queue>
#define inf 1e9

using namespace std;

int main()
{
    int i, j, k, m, n, t, cas=0, sum, count, flag;
    int a[110000];
    char str[110][110];

    while( scanf( "%d",&n ),n )
    {
        int flag=0;
        scanf( "%d",&a[1] );
        for( i=2;i<=n;i++ )
        {
            scanf( "%d",&a[i] );
            if( a[i]<a[i-1]&&flag==0 )
            {
                flag=1;
                j=i;
            }
        }

        if( n==1 )
        {
            printf( "0\n" );
            continue;
        }

        if( flag==0 )
        {
            printf( "0\n" );
            continue;
        }

        k=j;
        if( a[n]>a[1] )
        {
            printf( "-1\n" );
            continue;
        }

        for( i=j;i<n;i++ )
        {
            if( a[i+1]<a[i] )
            {
                break;
            }
        }
        if( i==n )
        {
            if( a[n]>a[1] )
                printf( "-1\n" );
            else
                printf( "%d\n",n-k+1 );
        }
        else
            printf( "-1\n" );
    }
    return 0;
}


#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>
#include<queue>
#define inf 1e9

using namespace std;
int a[110000];
int main()
{
    int i, j, k, m, n, t, cas=0, sum, count, flag;

    char str[110][110];

    while( scanf( "%d",&n ),n )
    {
        int flag=0,flag2=0;
        m=n;
        scanf( "%d",&a[1] );
        for( i=2;i<=n;i++ )
        {
            scanf( "%d",&a[i] );
            if( a[i]<a[i-1]&&flag==0 )
            {
                flag=1;
                j=i;
            }
            else if( a[i]<a[i-1]&&flag )
            {
                flag2=1;
            }
        }
        if( flag==0 )
        {
            printf( "0\n" );
        }
        else
        {
            if( flag2 )
            {
                printf( "-1\n" );
            }
            else
            {
                if( a[n]>a[1] )
                    printf( "-1\n" );
                else
                    printf( "%d\n",n-j+1 );
            }
        }
    }
    return 0;
}


#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>
#include<queue>
#define inf 1e9

using namespace std;
int a[510000];
int main()
{
    int i, j, k, m, n, t, cas=0, sum, count, flag;

    char str[110][110];

    while( scanf( "%d",&n ),n )
    {
        int flag=0;
        m=n;
        scanf( "%d",&a[1] );
        for( i=2;i<=n;i++ )
        {
            scanf( "%d",&a[i] );
            if( a[i]<a[i-1]&&flag==0 )
            {
                flag=1;
                j=i;
            }
        }

        if( flag==0 )
        {
            printf( "0\n" );
            continue;
        }

        for( i=1;i<j;i++ )
        {
            a[++n]=a[i];
        }

        for( i=j;i<n;i++ )
        {
            if( a[i+1]<a[i] )
            {
                printf( "-1\n" );
                break;
            }
        }

        if( i==n )
            printf( "%d\n",m-j+1 );
    }
    return 0;
}


#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>
#include<queue>
#define inf 1e9

using namespace std;

int main()
{
    int i, j, k, m, n, t, cas=0, sum, count, flag;
    int a[110000],b[110000];
    char str[110][110];

    while( scanf( "%d",&n ),n )
    {

        for( i=1;i<=n;i++ )
        {
            scanf( "%d",&a[i] );
        }

        if( n==1 )
        {
            printf( "0\n" );
            continue;
        }
        for( i=2;i<=n;i++ )
        {
            if( a[i]<a[i-1] )
                break;
        }
        if( i==n+1 )
        {
            printf( "0\n" );
            continue;
        }

        k=i;
        if( a[n]>a[1] )
        {
            printf( "-1\n" );
            continue;
        }

        for( ;i<n;i++ )
        {
            if( a[i+1]<a[i] )
            {
                break;
            }
        }
        if( i==n )
        {
            if( a[n]>a[1] )
                printf( "-1\n" );
            else
                printf( "%d\n",n-k+1 );
        }
        else
            printf( "-1\n" );
    }
    return 0;
}


#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>
#include<queue>
#define inf 1e9

using namespace std;
int a[110000];
int main()
{
    int i, j, k, m, n, t, cas=0, sum, count, flag;

    char str[110][110];

    while( ~scanf( "%d",&n ))
    {
        int flag=0,flag2=0;
        m=n;
        scanf( "%d",&a[1] );
        for( i=2;i<=n;i++ )
        {
            scanf( "%d",&a[i] );
            if( a[i]<a[i-1]&&flag==0 )
            {
                flag=1;
                j=i;
            }
            else if( a[i]<a[i-1]&&flag )
            {
                flag2=1;
            }
        }
        if( flag==0 )
        {
            printf( "0\n" );
        }
        else
        {
            if( flag2 )
            {
                printf( "-1\n" );
            }
            else
            {
                if( a[n]>a[1] )
                    printf( "-1\n" );
                else
                    printf( "%d\n",n-j+1 );
            }
        }
    }
    return 0;
}


#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>
#include<queue>
#define inf 1e9

using namespace std;

int main()
{
    int i, j, k, m, n, t, cas=0, sum, count, flag;
    int a[110000],b[110000];
    char str[110][110];

    while( scanf( "%d",&n ),n )
    {
        queue<int>q;

        for( i=1;i<=n;i++ )
        {
            scanf( "%d",&a[i] );
        }

        if( n==1 )
        {
            printf( "0\n" );
            continue;
        }
        for( i=2;i<=n;i++ )
        {
            if( a[i]<a[i-1] )
                break;
        }
        if( i==n+1 )
        {
            printf( "0\n" );
            continue;
        }

        k=i;
        for( ;i<n;i++ )
        {
            if( a[i+1]<a[i] )
            {
                break;
            }
        }
        if( i==n )
        {
            if( a[n]>a[1] )
                printf( "-1\n" );
            else
                printf( "%d\n",n-k+1 );
        }
        else
            printf( "-1\n" );
    }
    return 0;
}


#include<iostream>
#include<math.h>
#include<algorithm>

using namespace std;

int x[100000];

int main()
{
    int n,i,j,k,m=0;
    cin>>n;
    cin>>x[0];
    m=x[0];
    j=0;
    for(i=1;i<n;i++)
    {
        cin>>x[i];
        if(x[i]<=m)
        {
            m=x[i];
            j=i;
        }
    }
    k=j;
    for(i=j+1;i<n;i++)
    {
        if(x[i]<x[j])
        {
            cout<<-1;
            return 0;
        }
        j++;
    }
    if(k==0)
    {
        cout<<0;
        return 0;
    }
    j=0;
    for(i=1;i<k;i++)
    {
        if(x[i]<x[j])
        {
            cout<<-1;
            return 0;
        }
        j++;
    }
    if(x[0]>=x[n-1]) cout<<n-k;
    else cout<<-1;
    return 0;
}


#include<iostream>
#include<math.h>
#include<algorithm>

using namespace std;

int x[100000];

int main()
{
    int n,i,j,k,m=0;
    cin>>n;
    cin>>x[0];
    m=x[0];
    j=0;
    for(i=1;i<n;i++)
    {
        cin>>x[i];
        if(x[i]<m)
        {
            m=x[i];
            j=i;
        }
    }
    if(j==0)
    {
        i=n-1;
        while((i>0)&&(x[i]==x[j])) i--;
        if(i>0) j=i+1;
    }
    k=j;
    for(i=j+1;i<n;i++)
    {
        if(x[i]<x[j])
        {
            cout<<-1;
            return 0;
        }
        j++;
    }
    if(k==0)
    {
        cout<<0;
        return 0;
    }
    j=0;
    for(i=1;i<k;i++)
    {
        if(x[i]<x[j])
        {
            cout<<-1;
            return 0;
        }
        j++;
    }
    if(x[0]>=x[n-1]) cout<<n-k;
    else cout<<-1;
    return 0;
}


#include<iostream>
#include<math.h>
#include<algorithm>

using namespace std;

int x[100000];

int main()
{
    int n,i,j,k,m=0;
    cin>>n;
    cin>>x[0];
    m=x[0];
    j=0;
    for(i=1;i<n;i++)
    {
        cin>>x[i];
        if(x[i]<=m)
        {
            m=x[i];
            j=i;
        }
    }
    k=j;
    for(i=j+1;i<n;i++)
    {
        if(x[i]<x[j])
        {
            cout<<-1;
            return 0;
        }
        j++;
    }
    if(k==0)
    {
        cout<<0;
        return 0;
    }
    j=0;
    for(i=1;i<k;i++)
    {
        if(x[i]<x[j])
        {
            cout<<-1;
            return 0;
        }
        j++;
    }
    if(k==n-1)
    {
        for(i=0;i<n;i++)
        {
            if(x[i]!=x[k])
            {
                cout<<1;
                return 0;
            }
        }
        cout<<0;
        return 0;
    }
    if(x[0]>=x[n-1]) cout<<n-k;
    else cout<<-1;
    return 0;
}


#include<iostream>
#include<math.h>
#include<algorithm>

using namespace std;

int x[100000];

int main()
{
    int n,i,j,k,m=0;
    cin>>n;
    cin>>x[0];
    m=x[0];
    j=0;
    for(i=1;i<n;i++)
    {
        cin>>x[i];
        if(x[i]<m)
        {
            m=x[i];
            j=i;
        }
    }
    k=j;
    for(i=j+1;i<n;i++)
    {
        if(x[i]<x[j])
        {
            cout<<-1;
            return 0;
        }
        j++;
    }
    if(k==0)
    {
        cout<<0;
        return 0;
    }
    j=0;
    for(i=1;i<k;i++)
    {
        if(x[i]<x[j])
        {
            cout<<-1;
            return 0;
        }
        j++;
    }
    if(x[0]>=x[n-1]) cout<<n-k;
    else cout<<-1;
    return 0;
}


#include <iostream>
#include <string>
using namespace std;

const int maxn = 100000;

int main()
{
	int i, j, n, a[maxn], max, maxi;
	bool f;
	cin >> n;
	for (i = 0; i < n; ++i)
		cin >> a[i];
	max = a[0];
	maxi = 0;
	f = true;
	for (i = 0; i < n - 1; ++i)
	{
		if (a[i] > a[i + 1])
			f = false;
	}
	if (f)
	{
		cout << 0;
		return 0;
	}
	for (i = 0; i < n; ++i)
		if (a[i] > max)
		{
			max = a[i];
			maxi = i;
		}
	for(i = 1; i <= maxi; ++i)
	{
		if (a[i] < a[i - 1])
		{
			cout << -1;
			return 0;
		}
	}

	for (i = maxi + 2; i < n; ++i)
	{
		if (a[i] < a[i - 1])
		{
			cout << -1;
			return 0;
		}
	}
	if ((a[n - 1] > a[0]) && (n != 2) && (maxi != n - 1))
	{
		cout << -1;
		return 0;
	}
	cout << n - maxi - 1;
	return 0;
}


#include <iostream>
#include <string>
using namespace std;

const int maxn = 100000;

int main()
{
	int i, j, n, a[maxn], maxi;
	bool f;
	cin >> n;
	for (i = 0; i < n; ++i)
		cin >> a[i];
	f = true;
	for (i = 0; i < n - 1; ++i)
	{
		if (a[i] > a[i + 1])
			f = false;
	}
	if (f)
	{
		cout << 0;
		return 0;
	}

	i = 0;
	while ((i + 1 < n) && (a[i] <= a[i + 1]))
		++i;
	maxi = i;
	++i;
	while ((i + 1 < n) &&(a[i] <= a[i + 1]))
		++i;
	if (a[n - 1] > a[0])
	{
		cout << -1;
		return 0;
	}
	if ((i + 1) == n)
		cout << n - maxi - 1;
	else
		cout << -1;
	return 0;
}


#include <iostream>
#include <string>
using namespace std;

const int maxn = 100000;

int main()
{
	int i, j, n, a[maxn], max, maxi;
	bool f;
	cin >> n;
	for (i = 0; i < n; ++i)
		cin >> a[i];
	max = a[0];
	maxi = 0;
	f = true;
	for (i = 0; i < n - 1; ++i)
	{
		if (a[i] > a[i + 1])
			f = false;
	}
	if (f)
	{
		cout << 0;
		return 0;
	}
	for (i = 0; i < n; ++i)
		if (a[i] >= max)
		{
			max = a[i];
			maxi = i;
		}
	for(i = 1; i <= maxi; ++i)
	{
		if (a[i] < a[i - 1])
		{
			cout << -1;
			return 0;
		}
	}

	for (i = maxi + 2; i < n; ++i)
	{
		if (a[i] < a[i - 1])
		{
			cout << -1;
			return 0;
		}
	}
	if ((a[n - 1] > a[0]) && (n != 2))
	{
		cout << -1;
		return 0;
	}
	cout << n - maxi - 1;
	return 0;
}


#include <iostream>
#include <string>
using namespace std;

const int maxn = 100000;

int main()
{
	int i, j, n, a[maxn], max, maxi;
	cin >> n;
	for (i = 0; i < n; ++i)
		cin >> a[i];
	max = a[0];
	maxi = 0;
	for (i = 0; i < n; ++i)
		if (a[i] > max)
		{
			max = a[i];
			maxi = i;
		}
	for(i = 1; i <= maxi; ++i)
	{
		if (a[i] < a[i - 1])
		{
			cout << -1;
			return 0;
		}
	}

	for (i = maxi + 2; i < n; ++i)
	{
		if (a[i] < a[i - 1])
		{
			cout << -1;
			return 0;
		}
	}
	if ((a[n - 1] > a[0]) && (n != 2) && (maxi != n - 1))
	{
		cout << -1;
		return 0;
	}
	cout << n - maxi - 1;
	return 0;
}


#include<stdio.h>
#include<iostream>
using namespace std;
int data[100111];
int main(){

	int n;
	cin >> n;
	for(int i =0;i<n;i++)
		cin >>data[i];
	int midx=0;
	int mini=999999999;
	for(int i = 0 ; i < n; i++)
		if(mini > data[i]){
			midx = i ; mini = data[i];
		}
		int cnt=0;
		for(int i=midx;i!=((midx-1)+n)%n; i=(i+1)%n){
		if(data[i]<data[(i+1)%n])
			cnt++;

		}
	if(cnt == n-1)
	{

		cout << (n-midx)%n <<endl;

	}else
	{
		cout << "-1" <<endl;
	}
}


#include<stdio.h>
#include<iostream>
using namespace std;
int data[100111];
int main(){

    int n;
    cin >> n;
    for(int i =0;i<n;i++)
        cin >>data[i];
    int midx=0;
    int mini=999999999;
    int MAXI = -1;
    int Midx=0;
    for(int i = 0 ; i < n; i++)
        if(mini > data[i]){
            midx = i ; mini = data[i];
        }else if(MAXI <= data[i]){
            Midx = i ; MAXI = data[i];
        }
        int cnt=0;
        for(int i=midx;; i=(i+1)%n){
        if(data[i]<=data[(i+1)%n])
            cnt++;

            if(i==((midx-1)+n)%n) break;
        }
        //cout << cnt <<endl;

    if(cnt == n-1)
    {

        cout << max((n-1)-Midx, (n-midx)%n) <<endl;

    }else
    {
        cout << "-1" <<endl;
    }
}


#include<stdio.h>
#include<iostream>
using namespace std;
int data[100111];
int main(){

    int n;
    cin >> n;
    for(int i =0;i<n;i++)
        cin >>data[i];
    int midx=0;
    int mini=999999999;
    int MAXI = -1;
    int Midx=0;
    for(int i = 0 ; i < n; i++)
        if(mini > data[i]){
            midx = i ; mini = data[i];
        }else if(MAXI <= data[i]){
            Midx = i ; MAXI = data[i];
        }
        int cnt=0;
        for(int i=midx;; i=(i+1)%n){
        if(data[i]<=data[(i+1)%n])
            cnt++;

            if(i==((midx-1)+n)%n) break;
        }
        //cout << cnt <<endl;

    if(cnt == n-1)
    {

        cout << (n-1)-Midx <<endl;

    }else
    {
        cout << "-1" <<endl;
    }
}


#include<stdio.h>
#include<iostream>
using namespace std;
int data[100111];
int main(){

    int n;
    cin >> n;
    for(int i =0;i<n;i++)
        cin >>data[i];
    int midx=0;
    int mini=999999999;
    int MAXI = -1;
    int Midx=0;
    for(int i = 0 ; i < n; i++)
        if(mini > data[i]){
            midx = i ; mini = data[i];
        }else if(MAXI <= data[i]){
            Midx = i ; MAXI = data[i];
        }

        int cnt=0;
        for(int i=midx;; i=(i+1)%n){
        if(data[i]<=data[(i+1)%n])
            cnt++;

            if(i==((midx-1)+n)%n) break;
        }
    if(cnt == n){
        cout << "0" <<endl;
    }
    else if(cnt == n-1)
    {

        cout << max((n-1)-Midx, (n-midx)%n) <<endl;

    }else
    {
        cout << "-1" <<endl;
    }
}


#include<string>
#include<map>
#include<vector>
#include<algorithm>
#include<iostream>
#include<cmath>

typedef long long ll;
typedef long float lf;
typedef long double ld;

using namespace std;

void LittlePonyandSortbyShift()
{

    ll n;
    vector<ll> v;
    cin >> n;
    v.resize(n);
    for (ll i = 0;i < n;i++)
    {
        cin >> v[i];
    }

    bool first=true ,issort=true;
    ll idxfirst=0;
    ll idxsec=0;
    for (ll i = 1;i < n&&issort;i++)
    {

            if (first&&v[i - 1] <= v[i])
            {

            }
            else
            {
                if (first)
                {
                    first = false;
                    idxfirst = i-1;
                    idxsec = i ;
                }
                else
                {
                    if (v[i - 1] <= v[i])
                    {

                    }
                    else
                    {
                        issort = false;
                        //idxsec = i-1;
                    }
                }
            }

    }

    if (idxfirst == 0 && idxsec == 0)
    {
        cout << 0 << endl;
    }
    else
    {
        if (issort &&v[0] >= v[n - 1])
        {
            cout << n - idxsec << endl;
        }
        else
        {
            cout << -1 << endl;
        }

    }

}

int main()
{

    LittlePonyandSortbyShift();

    return 0;
}


#include<string>
#include<map>
#include<vector>
#include<algorithm>
#include<iostream>
#include<cmath>

typedef long long ll;
typedef long float lf;
typedef long double ld;

using namespace std;

void LittlePonyandSortbyShift()
{

    ll n;
    vector<ll> v;
    cin >> n;
    v.resize(n);
    for (ll i = 0;i < n;i++)
    {
        cin >> v[i];
    }

    bool first=true ,issort=true;
    ll idxfirst=0;
    ll idxsec=0;
    for (ll i = 1;i < n&&issort;i++)
    {

            if (first&&v[i - 1] < v[i])
            {

            }
            else
            {
                if (first)
                {
                    first = false;
                    idxfirst = i-1;
                    idxsec = i ;
                }
                else
                {
                    if (v[i - 1] < v[i])
                    {

                    }
                    else
                    {
                        issort = false;
                        //idxsec = i-1;
                    }
                }
            }

    }

    if (idxfirst == 0 && idxsec == 0)
    {
        cout << 0 << endl;
    }
    else
    {
        if (issort &&v[0] >= v[n - 1])
        {
            cout << n - idxsec << endl;
        }
        else
        {
            cout << -1 << endl;
        }

    }

}

int main()
{

    LittlePonyandSortbyShift();

    return 0;
}


#include<string>
#include<map>
#include<vector>
#include<algorithm>
#include<iostream>
#include<cmath>

typedef long long ll;
typedef long float lf;
typedef long double ld;

using namespace std;

void LittlePonyandSortbyShift()
{

    ll n;
    vector<ll> v;
    cin >> n;
    v.resize(n);
    for (ll i = 0;i < n;i++)
    {
        cin >> v[i];
    }

    bool first=true ,issort=true;
    ll idxfirst=0;
    ll idxsec=0;
    for (ll i = 1;i < n&&issort;i++)
    {

            if (first&&v[i - 1] < v[i])
            {

            }
            else
            {
                if (first)
                {
                    first = false;
                    idxfirst = i-1;
                    idxsec = i ;
                }
                else
                {
                    if (v[i - 1] < v[i])
                    {

                    }
                    else
                    {
                        issort = false;
                        //idxsec = i-1;
                    }
                }
            }
            if (idxfirst == 0&& idxsec == 0)
            {
                cout << 0 << endl;
            }
            else
            {
                if (issort &&v[0] >= v[n - 1])
                {
                    cout << n- idxsec << endl;
                }
                else
                {
                    cout << -1<< endl;
                }

            }

    }

}

int main()
{

    LittlePonyandSortbyShift();

    return 0;
}


#include<iostream>
using namespace std;
int main()
{
    int n, a[123456], ans = 0; cin >> n >> a[0]; for (int i = 1; i < n; i++){ cin >> a[i]; if (a[i] < a[0]){ ans++; a[0] = a[i]; } else if (a[i] < a[i - 1]){ cout << -1 << endl; return 0; } }
    cout << ans << endl;
    return 0;
}


#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
    int n, a[123456], x = 0, ans = 0; cin >> n >> a[0]; int i = 0;
    while (cin >> a[i] && a[i] >= a[i - 1]){ if (i == n - 1)break; i++; }
    if (i!=n-1)
    { i++; ans++; while (cin >> a[i] && a[i] >= a[i - 1]){ ans++; if (i == n - 1)break; i++; }}
    if (i == n - 1)cout << ans << endl;
    else cout << -1 << endl;
    return 0;
}


#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
    int n, a[123456], x = 0, ans = 0; cin >> n >> a[0]; int i = 0;
    while (cin >> a[i] && a[i] >= a[i - 1]){ if (i == n - 1)break; i++; }
    if (i != n - 1)
    {
        i++; ans++; while (cin >> a[i] && a[i] >= a[i - 1]){ ans++; if (i == n - 1)break; i++; }
    }
    if (i == n - 1 && a[n - 1] >= a[n - 2])cout << ans << endl;
    else cout << -1 << endl;
    return 0;
}


#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
    int n, a[123456], x = 0, ans = 0; cin >> n >> a[0]; int i = 0;
    while (cin >> a[i] && a[i] >= a[i - 1]){ if (i == n - 1)break; i++; }
    if (i != n - 1)
    {
        i++; ans++; while (cin >> a[i] && a[i] >= a[i - 1]){ ans++; if (i == n - 1)break; i++; }
    }
    if (i == n - 1 && (a[n - 1] >= a[n - 2]||n==2))cout << ans << endl;
    else cout << -1 << endl;
    return 0;
}


#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
    int n, a[123456], x = 0, ans = 0; cin >> n >> a[0]; int i = 0;
    while (cin >> a[i] && a[i] >= a[i - 1]){ if (i == n - 1)break; i++; }
    if (i != n - 1 && i != n - 2)
    {
        i++; ans++; while (cin >> a[i] && a[i] >= a[i - 1]){ ans++; if (i == n - 1)break; i++; }
    }
    if (i == n - 1)cout << ans << endl;
    else cout << -1 << endl;
    return 0;
}


#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
    int n, a[123456], x = 0, ans = 0; cin >> n >> a[0]; int i = 0;
    while (cin >> a[i] && a[i] >= a[i - 1]){ if (i == n - 1)break;  }
    if (i!=n-1)
    { i++; ans++; while (cin >> a[i] && a[i] >= a[i - 1]){ ans++; if (i == n - 1)break; i++; }}
    if (i == n - 1)cout << ans << endl;
    else cout << -1 << endl;
    return 0;
}


#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
    int n, a[123456], x = 0, ans = 0; cin >> n >> a[0]; int i = 0;
    while (cin >> a[i] && a[i] >= a[i - 1]){ if (i == n - 1)break; i++; }
    if (i != n - 1 || i != n - 2)
    {
        i++; ans++; while (cin >> a[i] && a[i] >= a[i - 1]){ ans++; if (i == n - 1)break; i++; }
    }
    if (i == n - 1)cout << ans << endl;
    else cout << -1 << endl;
    return 0;
}


#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
    int n, a[123456], x = 0, ans = 0; cin >> n >> a[0]; int i = 0;
    while (cin >> a[i] && a[i] >= a[i - 1]){ if (i == n - 1)break; i++; }
    if (i != n - 2 || i != n - 2)
    {
        i++; ans++; while (cin >> a[i] && a[i] >= a[i - 1]){ ans++; if (i == n - 1)break; i++; }
    }
    if (i == n - 1)cout << ans << endl;
    else cout << -1 << endl;
    return 0;
}


#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
    int n, a[1234567], b[1234567], c[1234567], z = 0, v = 0, ans = 0; cin >> n>>a[0];
    int i = 0;
    for ( i = 1; i < n; i++)
    {
        cin >> a[i];
        if (a[i] < a[i - 1])break;
    }
    if (i == n){ cout << 0 << endl; return 0; }
    int x = i;
    b[z++] = a[i];
    for (; i < n-1; i++)
    {
        cin >> b[z++];
    }
    for (int q = 0; q < z; q++)c[v++] = b[q];
    for (int w = 0; w < x; w++)c[v++] = a[w];
    if (is_sorted(c, c + v))
        cout <<z << endl;
    else
        cout << -1 << endl;
    return 0;
}


#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
    int n, a[12345], b[12345], c[12345], z = 0, v = 0, ans = 0; cin >> n>>a[0];
    int i = 0;
    for ( i = 1; i < n; i++)
    {
        cin >> a[i];
        if (a[i] < a[i - 1])break;
    }
    if (i == n){ cout << 0 << endl; return 0; }
    int x = i;
    b[z++] = a[i];
    for (; i < n-1; i++)
    {
        cin >> b[z++];
    }
    for (int q = 0; q < z; q++)c[v++] = b[q];
    for (int w = 0; w < x; w++)c[v++] = a[w];
    if (is_sorted(c, c + v))
        cout <<z << endl;
    else
        cout << -1 << endl;
    return 0;
}


#include<iostream>
#include<algorithm>
#include<string>
using namespace std;

int main(){
	long long int n , min  = 999999999 , min1 = 999999999, pos = 0;
	cin >> n;
	long long int arr[100010];
	for (int i = 0; i < n; i++){
		cin >> arr[i];
		if (arr[i] < min){
			min = arr[i];
			pos = i;
		}
	}
	if (is_sorted(arr, arr + n)){
		cout << 0;
		return 0;
	}
		int pos2 = pos;
	if (pos == 0){
		for (int i = n - 1; i > pos; i--){
			if (arr[i] == arr[pos]){
				pos2 = i;
			}
		}
	}

	int loop;
	if (pos2 + 1 == n){
		loop = 0;
	}
	else loop = pos2+1;
	do{
		if ((loop + 1 == pos2) || (loop == n - 1 && pos2 == 0)){
			break;
		}
		if (arr[loop] > arr[loop + 1]){
			cout << -1;
			return 0;
		}
		loop++;
		if ((loop + 1 == pos2) || (loop == n - 1 && pos2 == 0)){
			break;
		}
		if (loop + 1 == n){
			loop = 0;
		}

	} while (true);

	cout << n - pos2;
	return 0;
}


#include<iostream>
#include<algorithm>
#include<string>
using namespace std;

int main(){
	long long int n , min  = 999999999 , pos = 0;
	cin >> n;
	long long int arr[100010];
	for (int i = 0; i < n; i++){
		cin >> arr[i];
		if (arr[i] < min){
			min = arr[i];
			pos = i;
		}
	}
	int loop;
	if (pos + 1 == n){
		loop = 0;
	}
	else loop = pos+1;
	do{
		if ((loop + 1 == pos) || (loop == n - 1 && pos == 0)){
			break;
		}
		if (arr[loop] > arr[loop + 1]){
			cout << -1;
			return 0;
		}
		loop++;
		if ((loop + 1 == pos) || (loop == n - 1 && pos == 0)){
			break;
		}
		if (loop + 1 == n){
			loop = 0;
		}

	} while (true);

	if (pos == 0){
		cout << 0;
		return 0;
	}
	cout << n - pos;

	return 0;
}


#include<iostream>
#include<algorithm>
#include<string>
using namespace std;

int main(){
	long long int n , min  = 999999999 , pos = 0;
	cin >> n;
	long long int arr[100010];
	for (int i = 0; i < n; i++){
		cin >> arr[i];
		if (arr[i] < min){
			min = arr[i];
			pos = i;
		}
	}
	if (is_sorted(arr, arr + n)){
		cout << 0;
		return 0;
	}
	int loop;
	if (pos + 1 == n){
		loop = 0;
	}
	else loop = pos+1;
	do{
		if ((loop + 1 == pos) || (loop == n - 1 && pos == 0)){
			break;
		}
		if (arr[loop] > arr[loop + 1]){
			cout << -1;
			return 0;
		}
		loop++;
		if ((loop + 1 == pos) || (loop == n - 1 && pos == 0)){
			break;
		}
		if (loop + 1 == n){
			loop = 0;
		}

	} while (true);
	int pos2 = pos;
	for (int i = n-1; i > pos; i--){
		if (arr[i] == arr[pos]){
			pos2 = i;
		}
	}

	cout << n - pos2;
	return 0;
}


#include<iostream>
#include<algorithm>
#include<string>
using namespace std;

int main(){
	long long int n , min  = 999999999 , pos = 0;
	cin >> n;
	long long int arr[100010];
	for (int i = 0; i < n; i++){
		cin >> arr[i];
		if (arr[i] <= min){
			min = arr[i];
			pos = i;
		}
	}
	int loop;
	if (pos + 1 == n){
		loop = 0;
	}
	else loop = pos+1;
	do{
		if ((loop + 1 == pos) || (loop == n - 1 && pos == 0)){
			break;
		}
		if (arr[loop] > arr[loop + 1]){
			cout << -1;
			return 0;
		}
		loop++;
		if ((loop + 1 == pos) || (loop == n - 1 && pos == 0)){
			break;
		}
		if (loop + 1 == n){
			loop = 0;
		}

	} while (true);

	if (is_sorted(arr , arr+n)){
		cout << 0;
		return 0;
	}
	cout << n - pos;

	return 0;
}


#include<iostream>
#include<algorithm>
#include<string>
using namespace std;

int main(){
	long long int n , min  = 999999999 , pos = 0;
	cin >> n;
	long long int arr[100010];
	for (int i = 0; i < n; i++){
		cin >> arr[i];
		if (arr[i] < min){
			min = arr[i];
			pos = i;
		}
	}
	int loop;
	if (pos + 1 == n){
		loop = 0;
	}
	else loop = pos+1;
	do{
		if ((loop + 1 == pos) || (loop == n - 1 && pos == 0)){
			break;
		}
		if (arr[loop] > arr[loop + 1]){
			cout << -1;
			return 0;
		}
		loop++;
		if ((loop + 1 == pos) || (loop == n - 1 && pos == 0)){
			break;
		}
		if (loop + 1 == n){
			loop = 0;
		}

	} while (true);

	if (pos == 0){
		cout << 0;
		return 0;
	}
	cout << n - pos;

	system("pause");
	return 0;
}


#include<iostream>
#include<algorithm>
#include<string>
using namespace std;

int main(){
	long long int n;
	cin >> n;
	long long int arr[100010];
	for (int i = 0; i < n; i++){
		cin >> arr[i];
	}
	if (is_sorted(arr, arr + n)){
		cout << 0;
		return 0;
	}
	swap(arr[0], arr[n - 1]);
	if (is_sorted(arr, arr + n)){
		cout << 1;
		return 0;
	}
	cout << -1;

	return 0;
}


#include<iostream>
#include<algorithm>
#include<string>
using namespace std;

int main(){
	long long int n , min  = 999999999 , pos = 0;
	cin >> n;
	long long int arr[100010];
	for (int i = 0; i < n; i++){
		cin >> arr[i];
		if (arr[i] <= min){
			min = arr[i];
			pos = i;
		}
	}
	if (is_sorted(arr, arr + n)){
		cout << 0;
		return 0;
	}
	int loop;
	if (pos + 1 == n){
		loop = 0;
	}
	else loop = pos+1;
	do{
		if ((loop + 1 == pos) || (loop == n - 1 && pos == 0)){
			break;
		}
		if (arr[loop] > arr[loop + 1]){
			cout << -1;
			return 0;
		}
		loop++;
		if ((loop + 1 == pos) || (loop == n - 1 && pos == 0)){
			break;
		}
		if (loop + 1 == n){
			loop = 0;
		}

	} while (true);
	int pos2 = pos;
	if (pos == 0){
		for (int i = n - 1; i > pos; i--){
			if (arr[i] == arr[pos]){
				pos2 = i;
			}
		}
	}

	cout << n - pos2;
	return 0;
}


#include<iostream>
#include<algorithm>
#include<string>
using namespace std;

int main(){
	long long int n , min  = 999999999 , pos = 0;
	cin >> n;
	long int *arr= new long int[n];
	for (int i = 0; i < n; i++){
		cin >> arr[i];
		if (arr[i] < min){
			min = arr[i];
			pos = i;
		}
	}
	if (is_sorted(arr, arr + n)){
		cout << 0;
		return 0;
	}
		int pos2 = pos;
	if (pos == 0){
		int end = 0;
		for (int i = 1; i < n; i++){
			if (arr[i] > arr[0]){
				end = i;
			break;
			}
		}
		for (int i = n - 1; i > end; i--){
			if (arr[i] == arr[pos]){
				pos2 = i;
			}
		}
	}

	long int *arr2 = new long int[n];
	int j = pos2;
	for (int i = 0; i < n; i++){
		arr2[i] = arr[j];
		j++;
		if (j == n)j = 0;
		if (j == pos2)break;
	}
	if (is_sorted(arr2, arr2 + n)){
		cout << n - pos2;
		return 0;
	}
	else cout << -1;
	return 0;
}


#include<iostream>
#include<algorithm>
#include<string>
using namespace std;

int main(){
	long long int n , min  = 999999999 , pos = 0;
	cin >> n;
	long long int arr[100010];
	for (int i = 0; i < n; i++){
		cin >> arr[i];
		if (arr[i] < min){
			min = arr[i];
			pos = i;
		}
	}
	if (is_sorted(arr, arr + n)){
		cout << 0;
		return 0;
	}
	int loop;
	if (pos + 1 == n){
		loop = 0;
	}
	else loop = pos+1;
	do{
		if ((loop + 1 == pos) || (loop == n - 1 && pos == 0)){
			break;
		}
		if (arr[loop] > arr[loop + 1]){
			cout << -1;
			return 0;
		}
		loop++;
		if ((loop + 1 == pos) || (loop == n - 1 && pos == 0)){
			break;
		}
		if (loop + 1 == n){
			loop = 0;
		}

	} while (true);
	int pos2 = pos;
	if (pos == 0){
		for (int i = n - 1; i > pos; i--){
			if (arr[i] == arr[pos]){
				pos2 = i;
			}
		}
	}

	cout << n - pos2;
	return 0;
}


#include<iostream>
#include<algorithm>
#include<string>
using namespace std;

int main(){
	long long int n , min  = 999999999 , pos = 0;
	cin >> n;
	long long int arr[100010];
	for (int i = 0; i < n; i++){
		cin >> arr[i];
		if (arr[i] <= min){
			min = arr[i];
			pos = i;
		}
	}
	int loop;
	if (pos + 1 == n){
		loop = 0;
	}
	else loop = pos+1;
	do{
		if ((loop + 1 == pos) || (loop == n - 1 && pos == 0)){
			break;
		}
		if (arr[loop] > arr[loop + 1]){
			cout << -1;
			return 0;
		}
		loop++;
		if ((loop + 1 == pos) || (loop == n - 1 && pos == 0)){
			break;
		}
		if (loop + 1 == n){
			loop = 0;
		}

	} while (true);

	if (pos == 0){
		cout << 0;
		return 0;
	}
	cout << n - pos;

	return 0;
}


#include<iostream>
#include<algorithm>
#include<string>
using namespace std;

int main(){
	long long int n , min  = 999999999 , min1 = 999999999, pos = 0;
	cin >> n;
	long long int arr[100010];
	for (int i = 0; i < n; i++){
		cin >> arr[i];
		if (arr[i] < min){
			min = arr[i];
			pos = i;
		}
	}
	if (is_sorted(arr, arr + n)){
		cout << 0;
		return 0;
	}
		int pos2 = pos;
	if (pos == 0){
		int end = 0;
		for (int i = 1; i < n; i++){
			if (arr[i] > arr[0]){
				end = i;
			break;
			}
		}
		for (int i = n - 1; i > end; i--){
			if (arr[i] == arr[pos]){
				pos2 = i;
			}
		}
	}

	int loop;
	if (pos2 + 1 == n){
		loop = 0;
	}
	else loop = pos2+1;
	do{
		if ((loop + 1 == pos2) || (loop == n - 1 && pos2 == 0)){
			break;
		}
		if (loop + 1 != n){
			if (arr[loop] > arr[loop + 1]){
				cout << -1;
				return 0;
			}
		}
		else {
			if (arr[loop] > arr[0]){
				cout << -1;
				return 0;
			}
		}
		loop++;
		if ((loop + 1 == pos2) || (loop == n - 1 && pos2 == 0)){
			break;
		}
		if (loop + 1 >= n){
			loop = 0;
		}

	} while (true);

	cout << n - pos2;
	return 0;
}


//#define MYDEBUG
#pragma comment(linker, "/STACK:33554432")
#define _CRT_SECURE_NO_WARNINGS
#include <cstdio>
#include <iostream>
#include <iomanip>
#include <cmath>
#include <ctime>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <functional>

#define MOD 1000000007
#define INF 1000000001
#define EPS 1e-6
#define HASH_POW 37
const double PI = acos(-1.0);

using namespace std;

int main()
{
	//cin.sync_with_stdio(0);
	#ifdef MYDEBUG
	freopen("input.txt", "rt", stdin);
	freopen("output.txt", "wt", stdout);
	#else
	//freopen("weakconn.in", "rt", stdin);
	//freopen("weakconn.out", "wt", stdout);
	#endif

	int n;
	scanf("%d", &n);
	int p[100010];
	for (int i = 0; i < n; ++i)
		scanf("%d", &p[i]);
	int m = 0;
	for (int i = 1; i < n; ++i)
		if (p[m] >= p[i])
			m = i;
	for (int i = m, t = 0; t < n - 1; ++t)
	{
		int next = (i + 1) % n;
		if (p[next] < p[i])
		{
			printf("-1");
			return 0;
		}
		i = next;
	}
	printf("%d", (n - m) % n);

	#ifdef MYDEBUG
		cout << "\nTime = " << fixed << setprecision(3) << double(clock()) / CLOCKS_PER_SEC << endl;
	#endif
	return 0;
}


//#define MYDEBUG
#pragma comment(linker, "/STACK:33554432")
#define _CRT_SECURE_NO_WARNINGS
#include <cstdio>
#include <iostream>
#include <iomanip>
#include <cmath>
#include <ctime>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <functional>

#define MOD 1000000007
#define INF 1000000001
#define EPS 1e-6
#define HASH_POW 37
const double PI = acos(-1.0);

using namespace std;

int main()
{
	//cin.sync_with_stdio(0);
	#ifdef MYDEBUG
	freopen("input.txt", "rt", stdin);
	freopen("output.txt", "wt", stdout);
	#else
	//freopen("weakconn.in", "rt", stdin);
	//freopen("weakconn.out", "wt", stdout);
	#endif

	int n;
	scanf("%d", &n);
	int p[100010];
	for (int i = 0; i < n; ++i)
		scanf("%d", &p[i]);
	int m = 0;
	for (int i = 1; i < n; ++i)
		if (p[m] > p[i])
			m = i;
	for (int i = m, t = 0; t < n - 1; ++t)
	{
		int next = (i + 1) % n;
		if (p[next] < p[i])
		{
			printf("-1");
			return 0;
		}
		i = next;
	}
	printf("%d", (n - m) % n);

	#ifdef MYDEBUG
		cout << "\nTime = " << fixed << setprecision(3) << double(clock()) / CLOCKS_PER_SEC << endl;
	#endif
	return 0;
}


//#define MYDEBUG
#pragma comment(linker, "/STACK:33554432")
#define _CRT_SECURE_NO_WARNINGS
#include <cstdio>
#include <iostream>
#include <iomanip>
#include <cmath>
#include <ctime>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <functional>

#define MOD 1000000007
#define INF 1000000001
#define EPS 1e-6
#define HASH_POW 37
const double PI = acos(-1.0);

using namespace std;

int main()
{
	//cin.sync_with_stdio(0);
	#ifdef MYDEBUG
	freopen("input.txt", "rt", stdin);
	freopen("output.txt", "wt", stdout);
	#else
	//freopen("weakconn.in", "rt", stdin);
	//freopen("weakconn.out", "wt", stdout);
	#endif

	int n;
	scanf("%d", &n);
	int p[100010];
	for (int i = 0; i < n; ++i)
		scanf("%d", &p[i]);
	int m = 0;
	for (int i = 1; i < n; ++i)
	{
		if (p[i] >= p[i - 1])
			continue;
		else
		{
			if (m != 0)
			{
				printf("-1");
				return 0;
			}
			else
				m = i;
		}
	}
	printf("%d", (n - m) % n);

	#ifdef MYDEBUG
		cout << "\nTime = " << fixed << setprecision(3) << double(clock()) / CLOCKS_PER_SEC << endl;
	#endif
	return 0;
}


//#define MYDEBUG
#pragma comment(linker, "/STACK:33554432")
#define _CRT_SECURE_NO_WARNINGS
#include <cstdio>
#include <iostream>
#include <iomanip>
#include <cmath>
#include <ctime>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <functional>

#define MOD 1000000007
#define INF 1000000001
#define EPS 1e-6
#define HASH_POW 37
const double PI = acos(-1.0);

using namespace std;

int main()
{
	//cin.sync_with_stdio(0);
	#ifdef MYDEBUG
	freopen("input.txt", "rt", stdin);
	freopen("output.txt", "wt", stdout);
	#else
	//freopen("weakconn.in", "rt", stdin);
	//freopen("weakconn.out", "wt", stdout);
	#endif

	int n;
	scanf("%d", &n);
	int p[100010];
	for (int i = 0; i < n; ++i)
		scanf("%d", &p[i]);
	int m = 0;
	for (int i = 1; i < n; ++i)
	{
		if (p[i] >= p[i - 1])
			continue;
		else
		{
			if (m != 0)
			{
				printf("-1");
				return 0;
			}
			else
				m = i;
		}
	}
	if (m != 0 && p[n - 1] > p[0])
	{
		printf("-1");
		return 0;
	}
	printf("%d", (n - m) % n);

	#ifdef MYDEBUG
		cout << "\nTime = " << fixed << setprecision(3) << double(clock()) / CLOCKS_PER_SEC << endl;
	#endif
	return 0;
}


#include<iostream>
#include<vector>
#include <algorithm>
#include <map>
#include <string>
#include<cmath>
#include <functional>
using namespace std;
void main()
{
	int n, x,f=1,sum=0, j=0;
	vector <int>a,b;
	cin >> n;
	for (int i = 0; i < n; i++)
	{
		cin >> x;
		a.push_back(x);
		b.push_back(x);
	}
	sort(b.begin(), b.end());
	while (j<=a.size())
	{
		if (a[n - 1] <= a[0])
		{
			a.insert(a.begin(), a[n - 1]);
			a.pop_back();
			sum++;
		}
		else break;
		j++;
	}
	if (a == b)
	cout << sum;
	else cout << -1;
}


#include<iostream>
#include<vector>
#include <algorithm>
#include <map>
#include <string>
#include<cmath>
#include <functional>
using namespace std;
void main()
{
	int n,f=1,cnt=0,i,s=0;
	vector <int>a;

	cin >> n;
	for ( i = 0; i<n; i++){
		cin >> f;
		a.push_back(f);
			if (i)
			{
				if (a[i] < a[i - 1] ){ cnt++; s = i; }
			}
	}
	if (!cnt)cout << "0";
	else if (cnt == 1 && a[0] >= a[n - 1])cout << n - s;
	else cout << "-1";

}


#include<iostream>
#include<vector>
#include <algorithm>
#include <map>
#include <string>
#include<cmath>
#include <functional>
using namespace std;
void main()
{
	int n, x,f=1,sum=0, j=0;
	vector <int>a,b;
	cin >> n;
	for (int i = 0; i < n; i++)
	{
		cin >> x;
		a.push_back(x);
		b.push_back(x);
	}
	sort(b.begin(), b.end());
	while (j<a.size())
	{
		if (a[n - 1] < a[0])
		{
			a.insert(a.begin(), a[n - 1]);
			a.pop_back();
			sum++;
		}
		else break;
		j++;
	}
	if (a == b)
	cout << sum;
	else cout << -1;
}


#include<iostream>
#include<vector>
#include <algorithm>
#include <map>
#include <string>
#include<cmath>
#include <functional>
using namespace std;
void main()
{
	long long sum = 0, j = 0;
	int n, x,f=1;
	vector <int>a,b;
	cin >> n;
	for (int i = 0; i < n; i++)
	{
		cin >> x;
		a.push_back(x);
		b.push_back(x);
	}
	sort(b.begin(), b.end());
	while (j<a.size())
	{
		if (a[n - 1] < a[0])
		{
			a.insert(a.begin(), a[n - 1]);
			a.pop_back();
			sum++;
		}
		else break;
		j++;
	}
	if (a == b)
	cout << sum;
	else cout << -1;
}


#include<iostream>
#include<vector>
#include <algorithm>
#include <map>
#include <string>

#include <functional>
using namespace std;
void main()
{
	int n, x,f=1,sum=0, j=0;
	vector <int>a,b;
	cin >> n;
	for (int i = 0; i < n; i++)
	{
		cin >> x;
		a.push_back(x);
		b.push_back(x);
	}
	sort(b.begin(), b.end());
	while (j<a.size())
	{
		if (a[n - 1] < a[0])
		{
			a.insert(a.begin(), a[n - 1]);
			a.pop_back();
			sum++;
		}
		else break;
		j++;
	}
	if (a == b)
	cout << sum;
	else cout << -1;
}


#include<iostream>
#include<vector>
#include <algorithm>
#include <map>
#include <string>
#include<cmath>
#include <functional>
using namespace std;
void main()
{
	int n, x,f=1,sum=0,i,j=0;
	vector <int>a;
	cin >> n;
	for (i = 0; i < n; i++)
	{
		cin >> x;
		a.push_back(x);
		if (i){
			if (a[i] < a[i - 1])sum++, j = i;
		}
	}
	if (sum == 0)
		cout << 0;
	else if (a[n - 1] <= a[0])cout << sum;
	else cout << -1;

	//system("PAUSE");
}


#include<iostream>
#include<vector>
#include <algorithm>
#include <map>
#include <string>
#include<cmath>
#include <functional>
using namespace std;
void main()
{
	int n, x,f=1,sum=0,i,j=0;
	vector <int>a;
	cin >> n;
	for (i = 0; i < n; i++)
	{
		cin >> x;
		a.push_back(x);
		if (i){
			if (a[i] < a[i - 1])sum++;
		}
	}
	if (sum == 0)
		cout << 0;
	else if (a[n - 1] < a[0])cout << sum;
	else cout << -1;

	//system("PAUSE");
}


#include <iostream>
#include <string>
using namespace std;
int compare(string a, string b)
{
    int c = 0;
    for (int k = 0; k < a.length(); k++)
    {
        if (a[k] != b[k])
            c++;
    }
    return c;
}
/*int main()
{
    string a, b;
    cin >> a >> b;
    int c = 0;
    int h = a.length();
    int onces = b.length() - a.length()+1;
    for (int i = 0; i < onces; i++)
    {

        for (int k = 0; k < h; k++)
        {
            if (a[k] != b[k+i])
                c++;
        }

    }
    cout << c << endl;
    return 0;
}

*/

/*
int main()
{
    string a, b;
    cin >> a >> b;
    int c = 0;
    int al = a.length();
    int bl = b.length();
    for (int i = 0; i < al; i++)
    {
        for (int j = i; j < bl - al+i+1; j++)
        {
            if (a[i] != b[j])
                c++;
        }
        cout << endl;
    }

    cout << c << endl;
    return 0;

}*/

int main()
{
    int n; cin >> n;
    int*arr = new int[n];
    int s = 0;
    for (int i = 0; i < n; i++)
        cin >> arr[i];

    bool f = true;
    for (int i = 0; i < n - 1; i++)
    {
        if (arr[i]>arr[i + 1])
        {
            f = false;
            break;
        }
    }
    if (f)
    {
        cout << 0 << endl;
        return 0;
    }
    if (n == 2)
    {
        if (arr[0] > arr[1])
            cout << 1 << endl;
        return 0;
    }

    int h=0;
    for (int i = 0; i < n-1; i++)
    {
        if (arr[i]>arr[i + 1])
        {
            h = i+1;
            break;
        }
    }
    //cout << h << endl;

        for (int j = h ; j < n-1; j++)
        {
            if (arr[j]>arr[j + 1]||arr[j]>arr[0])
            {
                cout << -1 << endl;
                return 0;
            }
            if (j == n - 2)
            {
                if (arr[n - 1] > arr[0])
                {
                    cout << -1 << endl;
                    return 0;
                }
            }
        }

        if (h == n - 1)
        {
            if (arr[n - 1] > arr[0])
            {
                cout << -1 << endl;
                return 0;
            }
        }

    s = n - h;
    cout << s << endl;
    return 0;
}


#include <iostream>
#include <string>
using namespace std;
int compare(string a, string b)
{
    int c = 0;
    for (int k = 0; k < a.length(); k++)
    {
        if (a[k] != b[k])
            c++;
    }
    return c;
}
/*int main()
{
    string a, b;
    cin >> a >> b;
    int c = 0;
    int h = a.length();
    int onces = b.length() - a.length()+1;
    for (int i = 0; i < onces; i++)
    {

        for (int k = 0; k < h; k++)
        {
            if (a[k] != b[k+i])
                c++;
        }

    }
    cout << c << endl;
    return 0;
}

*/

/*
int main()
{
    string a, b;
    cin >> a >> b;
    int c = 0;
    int al = a.length();
    int bl = b.length();
    for (int i = 0; i < al; i++)
    {
        for (int j = i; j < bl - al+i+1; j++)
        {
            if (a[i] != b[j])
                c++;
        }
        cout << endl;
    }

    cout << c << endl;
    return 0;

}*/

int main()
{
    int n; cin >> n;
    int*arr = new int[n];
    int s = 0;
    for (int i = 0; i < n; i++)
        cin >> arr[i];

    bool f = true;
    for (int i = 0; i < n - 1; i++)
    {
        if (arr[i]>arr[i + 1])
        {
            f = false;
            break;
        }
    }
    if (f)
    {
        cout << 0 << endl;
        return 0;
    }
    if (n == 2)
    {
        if (arr[0] > arr[1])
            cout << 1 << endl;
        return 0;
    }

    int h=0;
    for (int i = 0; i < n-1; i++)
    {
        if (arr[i]>arr[i + 1])
        {
            h = i+1;
            break;
        }
    }
    //cout << h << endl;

        for (int j = h ; j < n-1; j++)
        {
            if (arr[j]>arr[j + 1]||arr[j]>arr[0])
            {

                cout << -1 << endl;
                return 0;
            }
        }

        if (h == n - 1)
        {
            if (arr[n - 1] > arr[0])
            {
                cout << -1 << endl;
                return 0;
            }
        }

    s = n - h;
    cout << s << endl;
    return 0;
}


#include <iostream>
#include <string>
using namespace std;
int compare(string a, string b)
{
    int c = 0;
    for (int k = 0; k < a.length(); k++)
    {
        if (a[k] != b[k])
            c++;
    }
    return c;
}
/*int main()
{
    string a, b;
    cin >> a >> b;
    int c = 0;
    int h = a.length();
    int onces = b.length() - a.length()+1;
    for (int i = 0; i < onces; i++)
    {

        for (int k = 0; k < h; k++)
        {
            if (a[k] != b[k+i])
                c++;
        }

    }
    cout << c << endl;
    return 0;
}

*/

/*
int main()
{
    string a, b;
    cin >> a >> b;
    int c = 0;
    int al = a.length();
    int bl = b.length();
    for (int i = 0; i < al; i++)
    {
        for (int j = i; j < bl - al+i+1; j++)
        {
            if (a[i] != b[j])
                c++;
        }
        cout << endl;
    }

    cout << c << endl;
    return 0;

}*/

int main()
{
    int n; cin >> n;
    int*arr = new int[n];
    int s = 0;
    for (int i = 0; i < n; i++)
        cin >> arr[i];

    bool f = true;
    for (int i = 0; i < n - 1; i++)
    {
        if (arr[i]>arr[i + 1])
        {
            f = false;
            break;
        }
    }
    if (f)
    {
        cout << 0 << endl;
        return 0;
    }
    if (n == 2)
    {
        if (arr[0] > arr[1])
            cout << 1 << endl;
        return 0;
    }

    int h=0;
    for (int i = 0; i < n-1; i++)
    {
        if (arr[i]>arr[i + 1])
        {
            h = i;
        }
    }
    //cout << h << endl;
    if (arr[h] <= arr[0])
    {
        for (int j = h ; j < n-1; j++)
        {
            if (arr[j]>arr[j + 1]||arr[j]>arr[0])
            {

                cout << -1 << endl;
                return 0;
            }
        }
        if (arr[n - 1] > arr[0])
        {

            cout << -1 << endl;
            return 0;
        }
    }
    else
    {
        cout << -1 << endl;
        return 0;
    }
    s = n - h;
    cout << s << endl;
    return 0;
}


#include <iostream>
#include <string>
using namespace std;
int compare(string a, string b)
{
    int c = 0;
    for (int k = 0; k < a.length(); k++)
    {
        if (a[k] != b[k])
            c++;
    }
    return c;
}
/*int main()
{
    string a, b;
    cin >> a >> b;
    int c = 0;
    int h = a.length();
    int onces = b.length() - a.length()+1;
    for (int i = 0; i < onces; i++)
    {

        for (int k = 0; k < h; k++)
        {
            if (a[k] != b[k+i])
                c++;
        }

    }
    cout << c << endl;
    return 0;
}

*/

/*
int main()
{
    string a, b;
    cin >> a >> b;
    int c = 0;
    int al = a.length();
    int bl = b.length();
    for (int i = 0; i < al; i++)
    {
        for (int j = i; j < bl - al+i+1; j++)
        {
            if (a[i] != b[j])
                c++;
        }
        cout << endl;
    }

    cout << c << endl;
    return 0;

}*/

int main()
{
    int n; cin >> n;
    int*arr = new int[n];
    int s = 0;
    for (int i = 0; i < n; i++)
        cin >> arr[i];

    bool f = true;
    for (int i = 0; i < n - 1; i++)
    {
        if (arr[i]>arr[i + 1])
        {
            f = false;
            break;
        }
    }
    if (f)
    {
        cout << 0 << endl;
        return 0;
    }
    if (n == 2)
    {
        if (arr[0] > arr[1])
            cout << 1 << endl;
        return 0;
    }

    int h=0;
    for (int i = 0; i < n-1; i++)
    {
        if (arr[i]>arr[i + 1])
        {
            h = i+1;
        }
    }
    //cout << h << endl;
    if (arr[h] <= arr[0])
    {
        for (int j = h ; j < n-1; j++)
        {
            if (arr[j]>arr[j + 1]||arr[j]>arr[0])
            {

                cout << -1 << endl;
                return 0;
            }
        }
        if (arr[n - 1] > arr[0])
        {

            cout << -1 << endl;
            return 0;
        }
    }
    else
    {
        cout << -1 << endl;
        return 0;
    }
    s = n - h;
    cout << s << endl;
    return 0;
}


#include <iostream>
#include <string>
using namespace std;
int compare(string a, string b)
{
    int c = 0;
    for (int k = 0; k < a.length(); k++)
    {
        if (a[k] != b[k])
            c++;
    }
    return c;
}
/*int main()
{
    string a, b;
    cin >> a >> b;
    int c = 0;
    int h = a.length();
    int onces = b.length() - a.length()+1;
    for (int i = 0; i < onces; i++)
    {

        for (int k = 0; k < h; k++)
        {
            if (a[k] != b[k+i])
                c++;
        }

    }
    cout << c << endl;
    return 0;
}

*/

/*
int main()
{
    string a, b;
    cin >> a >> b;
    int c = 0;
    int al = a.length();
    int bl = b.length();
    for (int i = 0; i < al; i++)
    {
        for (int j = i; j < bl - al+i+1; j++)
        {
            if (a[i] != b[j])
                c++;
        }
        cout << endl;
    }

    cout << c << endl;
    return 0;

}*/

int main()
{
    int n; cin >> n;
    int*arr = new int[n];
    int s = 0;
    for (int i = 0; i < n; i++)
        cin >> arr[i];

    bool f = true;
    for (int i = 0; i < n - 1; i++)
    {
        if (arr[i]>arr[i + 1])
        {
            f = false;
            break;
        }
    }
    if (f)
    {
        cout << 0 << endl;
        return 0;
    }
    if (n == 2)
    {
        if (arr[0] > arr[1])
            cout << 1 << endl;
        return 0;
    }

    int h=0;
    for (int i = 0; i < n-1; i++)
    {
        if (arr[i]>arr[i + 1])
        {
            h = i+1;
        }
    }
    //cout << h << endl;

        for (int j = h ; j < n-1; j++)
        {
            if (arr[j]>arr[j + 1]||arr[j]>arr[0])
            {

                cout << -1 << endl;
                return 0;
            }
        }

        if (h == n - 1)
        {
            if (arr[n - 1] > arr[0])
            {
                cout << -1 << endl;
                return 0;
            }
        }

    s = n - h;
    cout << s << endl;
    return 0;
}


#include <iostream>
#include <string>
using namespace std;
int compare(string a, string b)
{
    int c = 0;
    for (int k = 0; k < a.length(); k++)
    {
        if (a[k] != b[k])
            c++;
    }
    return c;
}
/*int main()
{
    string a, b;
    cin >> a >> b;
    int c = 0;
    int h = a.length();
    int onces = b.length() - a.length()+1;
    for (int i = 0; i < onces; i++)
    {

        for (int k = 0; k < h; k++)
        {
            if (a[k] != b[k+i])
                c++;
        }

    }
    cout << c << endl;
    return 0;
}

*/

/*
int main()
{
    string a, b;
    cin >> a >> b;
    int c = 0;
    int al = a.length();
    int bl = b.length();
    for (int i = 0; i < al; i++)
    {
        for (int j = i; j < bl - al+i+1; j++)
        {
            if (a[i] != b[j])
                c++;
        }
        cout << endl;
    }

    cout << c << endl;
    return 0;

}*/

int main()
{
    int n; cin >> n;
    int*arr = new int[n];
    int s = 0;
    for (int i = 0; i < n; i++)
        cin >> arr[i];

    bool f = true;
    for (int i = 0; i < n - 1; i++)
    {
        if (arr[i]>arr[i + 1])
        {
            f = false;
            break;
        }
    }
    if (f)
    {
        cout << 0 << endl;
        return 0;
    }

    int h=0;
    for (int i = 0; i < n-1; i++)
    {
        if (arr[i]>arr[i + 1])
        {
            h = i;
        }
    }
    //cout << h << endl;
    if (arr[h] <= arr[0])
    {
        for (int j = h ; j < n-1; j++)
        {
            if (arr[j]>arr[j + 1]||arr[j]>arr[0])
            {
                cout << -1 << endl;
                return 0;
            }
        }
        if (arr[n - 1] > arr[0])
        {
            cout << -1 << endl;
            return 0;
        }
    }
    else
    {
        cout << -1 << endl;
        return 0;
    }
    s = n - h;
    cout << s << endl;
    return 0;
}


#include <iostream>
#include <string>
using namespace std;
int compare(string a, string b)
{
    int c = 0;
    for (int k = 0; k < a.length(); k++)
    {
        if (a[k] != b[k])
            c++;
    }
    return c;
}
/*int main()
{
    string a, b;
    cin >> a >> b;
    int c = 0;
    int h = a.length();
    int onces = b.length() - a.length()+1;
    for (int i = 0; i < onces; i++)
    {

        for (int k = 0; k < h; k++)
        {
            if (a[k] != b[k+i])
                c++;
        }

    }
    cout << c << endl;
    return 0;
}

*/

/*
int main()
{
    string a, b;
    cin >> a >> b;
    int c = 0;
    int al = a.length();
    int bl = b.length();
    for (int i = 0; i < al; i++)
    {
        for (int j = i; j < bl - al+i+1; j++)
        {
            if (a[i] != b[j])
                c++;
        }
        cout << endl;
    }

    cout << c << endl;
    return 0;

}*/

int main()
{
    int n; cin >> n;
    int*arr = new int[n];
    int s = 0;
    for (int i = 0; i < n; i++)
        cin >> arr[i];

    bool f = true;
    for (int i = 0; i < n - 1; i++)
    {
        if (arr[i]>arr[i + 1])
        {
            f = false;
            break;
        }
    }
    if (f)
    {
        cout << 0 << endl;
        return 0;
    }
    if (n == 2)
    {
        if (arr[0] > arr[1])
            cout << 1 << endl;
        return 0;
    }

    int h=0;
    for (int i = 0; i < n-1; i++)
    {
        if (arr[i]>arr[i + 1])
        {
            h = i+1;
        }
    }
    //cout << h << endl;

        for (int j = h ; j < n-1; j++)
        {
            if (arr[j]>arr[j + 1]||arr[j]>arr[0])
            {

                cout << -1 << endl;
                return 0;
            }
        }

    s = n - h;
    cout << s << endl;
    return 0;
}


#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <climits>
#include <vector>
#include <queue>
#include <cstdlib>
#include <string>
#include <set>
#include<stack>
using namespace std;
const int N=100010;
int a[N];
int main()
{
    int n,flag;
    while(scanf("%d",&n)!=EOF)
    {
        for(int i=0;i<n;i++)
            scanf("%d",&a[i]);
        flag=0;
        int cnt=0;
        for(int i=1;i<n;i++)
        {
            if(a[i]<a[i-1])
            {
                cnt++;
                flag=i;
            }
        }
        if(cnt>1||cnt==1&&a[n-1]>a[0])
            printf("-1\n");
        else if(flag==0)
                printf("0\n");
        else
            printf("%d\n",n-flag);
    }
    return 0;
}


#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <climits>
#include <vector>
#include <queue>
#include <cstdlib>
#include <string>
#include <set>
#include<stack>
using namespace std;
const int N=100010;
int a[N];
int main()
{
    int n,flag;
    while(scanf("%d",&n)!=EOF)
    {
        for(int i=0;i<n;i++)
            scanf("%d",&a[i]);
        flag=0;
        int cnt=0;
        for(int i=1;i<n;i++)
        {
            if(a[i]<a[i-1])
            {
                cnt++;
                flag=i;
            }
        }
        if(cnt>1||cnt==1&&a[n-1]>a[0])
            printf("-1\n");
        else
            printf("%d\n",min(flag,n-flag));
    }
    return 0;
}


#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <climits>
#include <vector>
#include <queue>
#include <cstdlib>
#include <string>
#include <set>
#include<stack>
using namespace std;
const int N=100010;
int a[N];
int main()
{
    int n,flag;
    while(scanf("%d",&n)!=EOF)
    {
        for(int i=0;i<n;i++)
            scanf("%d",&a[i]);
        flag=0;
        int cnt=0;
        for(int i=1;i<n;i++)
        {
            if(a[i]<a[i-1])
            {
                cnt++;
                flag=i;
            }
        }
        if(cnt>1||cnt==1&&a[flag]>a[0])
            printf("-1\n");
        else
            printf("%d\n",min(flag,n-flag));
    }
    return 0;
}


#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <climits>
#include <vector>
#include <queue>
#include <cstdlib>
#include <string>
#include <set>
#include<stack>
using namespace std;
const int N=100010;
int a[N];
int main()
{
    int n,flag;
    while(scanf("%d",&n)!=EOF)
    {
        for(int i=0;i<n;i++)
            scanf("%d",&a[i]);
        flag=0;
        int cnt=0;
        for(int i=1;i<n;i++)
        {
            if(a[i]<a[i-1])
            {
                cnt++;
                flag=i;
            }
        }
        if(cnt>1||cnt==1&&a[n-1]>a[0])
            printf("-1\n");
        else
            printf("%d\n",n-flag);
    }
    return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <cstdlib>
#include <cmath>
#include <cstdio>
#include <algorithm>
#include <set>
#include <queue>
#include <deque>
#include <map>
#include <stack>
#include <string>
#include <sstream>
#include <vector>
#include <ctime>
#include <cstring>

#define ll long long
#define ld long double
#define vi vector<int>
#define vvi vector<vi >
#define pii pair<int,int>
#define pll pair<ll,ll>
#define vpii vector<pii >
#define vb vector<bool>
#define min(x,y) (x < y ? x : y)
#define min3(x,y,z) (min(min(x,y),z))
#define max(x,y) (x > y ? x : y)
#define max3(x,y,z) (max(max(x,y),z))
#define forn(i,n) for(int i = 0;i < n;i++)
#define sqrt(x) (pow(x,0.5))
#define sqr(x) (x * x)
#define mp make_pair
#define TASKNAME ""

const int inf = 1e9;
const ll infll = 1e18;
const int mod = 1e9 + 7;
const ld eps = 1e-9;

using namespace std;

void solve() {
	int n;
	cin >> n;
	vi vec(n);
	int ind = -1;
	forn(i, n){
		cin >> vec[i];
		if (i > 0){
			if (vec[i] < vec[i - 1] && ind == -1){
				ind = i;
			}
		}
	}
	for (int i = ind; i < n; i++){
		if (i + 1 < n){
			if (vec[i + 1] < vec[i]){
				cout << "-1";
				return;
			}
		}
	}
	cout << (n - ind);
}

int main(const int argc, const char** argv) {
	if (argc == 2){
		string argv1(argv[1]);
		if (argv1 == "__DEBUG__"){
			freopen("INPUT.TXT", "r", stdin); freopen("OUTPUT.TXT", "w", stdout);
		}
	}
	//freopen(TASKNAME".in","r",stdin); freopen(TASKNAME".out","w",stdout);
	solve();
	return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <cstdlib>
#include <cmath>
#include <cstdio>
#include <algorithm>
#include <set>
#include <queue>
#include <deque>
#include <map>
#include <stack>
#include <string>
#include <sstream>
#include <vector>
#include <ctime>
#include <cstring>

#define ll long long
#define ld long double
#define vi vector<int>
#define vvi vector<vi >
#define pii pair<int,int>
#define pll pair<ll,ll>
#define vpii vector<pii >
#define vb vector<bool>
#define min(x,y) (x < y ? x : y)
#define min3(x,y,z) (min(min(x,y),z))
#define max(x,y) (x > y ? x : y)
#define max3(x,y,z) (max(max(x,y),z))
#define forn(i,n) for(int i = 0;i < n;i++)
#define sqrt(x) (pow(x,0.5))
#define sqr(x) (x * x)
#define mp make_pair
#define TASKNAME ""

const int inf = 1e9;
const ll infll = 1e18;
const int mod = 1e9 + 7;
const ld eps = 1e-9;

using namespace std;

void solve() {
	int n;
	cin >> n;
	vi vec(n);
	int ind = -1;
	forn(i, n){
		cin >> vec[i];
		if (i > 0){
			if (vec[i] < vec[i - 1] && ind == -1){
				ind = i;
			}
		}
	}
	for (int i = ind; i < n; i++){
		if (i + 1 < n){
			if (vec[i + 1] < vec[i]){
				cout << "-1";
				return;
			}
		}
	}
	if (vec[n - 1] < vec[0])
		cout << (n - ind);
	else cout << "-1";
}

int main(const int argc, const char** argv) {
	if (argc == 2){
		string argv1(argv[1]);
		if (argv1 == "__DEBUG__"){
			freopen("INPUT.TXT", "r", stdin); freopen("OUTPUT.TXT", "w", stdout);
		}
	}
	//freopen(TASKNAME".in","r",stdin); freopen(TASKNAME".out","w",stdout);
	solve();
	return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <cstdlib>
#include <cmath>
#include <cstdio>
#include <algorithm>
#include <set>
#include <queue>
#include <deque>
#include <map>
#include <stack>
#include <string>
#include <sstream>
#include <vector>
#include <ctime>
#include <cstring>

#define ll long long
#define ld long double
#define vi vector<int>
#define vvi vector<vi >
#define pii pair<int,int>
#define pll pair<ll,ll>
#define vpii vector<pii >
#define vb vector<bool>
#define min(x,y) (x < y ? x : y)
#define min3(x,y,z) (min(min(x,y),z))
#define max(x,y) (x > y ? x : y)
#define max3(x,y,z) (max(max(x,y),z))
#define forn(i,n) for(int i = 0;i < n;i++)
#define sqrt(x) (pow(x,0.5))
#define sqr(x) (x * x)
#define mp make_pair
#define TASKNAME ""

const int inf = 1e9;
const ll infll = 1e18;
const int mod = 1e9 + 7;
const ld eps = 1e-9;

using namespace std;

void solve() {
	int n;
	cin >> n;
	vi vec(n);
	int ind = -1;
	forn(i, n){
		cin >> vec[i];
		if (i > 0){
			if (vec[i] < vec[i - 1] && ind == -1){
				ind = i;
			}
		}
	}
	if (ind == -1){
		cout << 0;
		return;
	}
	for (int i = ind; i < n; i++){
		if (i + 1 < n){
			if (vec[i + 1] < vec[i]){
				cout << "-1";
				return;
			}
		}
	}
	if (vec[n - 1] <= vec[0])
		cout << (n - ind);
	else cout << "-1";
}

int main(const int argc, const char** argv) {
	if (argc == 2){
		string argv1(argv[1]);
		if (argv1 == "__DEBUG__"){
			freopen("INPUT.TXT", "r", stdin); freopen("OUTPUT.TXT", "w", stdout);
		}
	}
	//freopen(TASKNAME".in","r",stdin); freopen(TASKNAME".out","w",stdout);
	solve();
	return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <cstdlib>
#include <cmath>
#include <cstdio>
#include <algorithm>
#include <set>
#include <queue>
#include <deque>
#include <map>
#include <stack>
#include <string>
#include <sstream>
#include <vector>
#include <ctime>
#include <cstring>

#define ll long long
#define ld long double
#define vi vector<int>
#define vvi vector<vi >
#define pii pair<int,int>
#define pll pair<ll,ll>
#define vpii vector<pii >
#define vb vector<bool>
#define min(x,y) (x < y ? x : y)
#define min3(x,y,z) (min(min(x,y),z))
#define max(x,y) (x > y ? x : y)
#define max3(x,y,z) (max(max(x,y),z))
#define forn(i,n) for(int i = 0;i < n;i++)
#define sqrt(x) (pow(x,0.5))
#define sqr(x) (x * x)
#define mp make_pair
#define TASKNAME ""

const int inf = 1e9;
const ll infll = 1e18;
const int mod = 1e9 + 7;
const ld eps = 1e-9;

using namespace std;

void solve() {
	int n;
	cin >> n;
	vi vec(n);
	int ind = -1;
	forn(i, n){
		cin >> vec[i];
		if (i > 0){
			if (vec[i] < vec[i - 1] && ind == -1){
				ind = i;
			}
		}
	}
	if (ind == -1){
		cout << 0;
		return;
	}
	for (int i = ind; i < n; i++){
		if (i + 1 < n){
			if (vec[i + 1] < vec[i]){
				cout << "-1";
				return;
			}
		}
	}
	if (vec[n - 1] < vec[0])
		cout << (n - ind);
	else cout << "-1";
}

int main(const int argc, const char** argv) {
	if (argc == 2){
		string argv1(argv[1]);
		if (argv1 == "__DEBUG__"){
			freopen("INPUT.TXT", "r", stdin); freopen("OUTPUT.TXT", "w", stdout);
		}
	}
	//freopen(TASKNAME".in","r",stdin); freopen(TASKNAME".out","w",stdout);
	solve();
	return 0;
}


#include <stdio.h>
#include <algorithm>
#include <vector>
#include <math.h>
#include <map>
#include <string>
using namespace std;
int n,a[100007],b[100007];
void solve(){
    int i,j,min=10000000,l,k=0,l1;
    for(i=0;i<n;i++){
        scanf("%d",&a[i]);
        b[i]=a[i];
        if(a[i]<min){
            l=i;
            min=a[i];
        }
    }
    sort(b,b+n);
    if(i==0){
        for(i=1;i<n;i++){
            if(a[i]==min&&k==0){
                l1=i;
                min=a[i];
                k++;
                if(l1!=l+1){
                    l=l1;
                }
            }
        }
    }
    for(i=0;i<n;i++){
        j=(l+i)%n;
        if(b[i]!=a[j]){
            printf("-1\n");
            return;
        }
    }
    printf("%d\n",(n-l)%n);
}
int main() {
    #pragma comment(linker, "/STACK:67108864")
    #ifndef ONLINE_JUDGE
    freopen("input.txt", "rt", stdin);
    freopen("output.txt", "wt", stdout);
    #endif
    while(scanf("%d",&n)!=EOF){
        solve();
    }
    return 0;
}


#include <stdio.h>
#include <algorithm>
#include <vector>
#include <math.h>
#include <map>
#include <string>
using namespace std;
int n,a[100007],b[100007];
void solve(){
    int i,j,min=10000000,l,k=0,l1;
    for(i=0;i<n;i++){
        scanf("%d",&a[i]);
        b[i]=a[i];
        if(a[i]<min){
            l=i;
            min=a[i];
        }
    }
    sort(b,b+n);
    if(l==0){
        while(l<n&&a[l]==a[l+1]){
            l++;
        }
        for(i=l+1;i<n;i++){
            if(a[i]==min&&k==0){
                l1=i;
                min=a[i];
                k++;
                if(l1!=l+1){
                    l=l1;
                }
            }
        }
    }
    for(i=0;i<n;i++){
        j=(l+i)%n;
        if(b[i]!=a[j]){
            printf("-1\n");
            return;
        }
    }
    printf("%d\n",(n-l)%n);
}
int main() {
    #pragma comment(linker, "/STACK:67108864")
    #ifndef ONLINE_JUDGE
    freopen("input.txt", "rt", stdin);
    freopen("output.txt", "wt", stdout);
    #endif
    while(scanf("%d",&n)!=EOF){
        solve();
    }
    return 0;
}


#include <stdio.h>
#include <algorithm>
#include <vector>
#include <math.h>
#include <map>
#include <string>
using namespace std;
int n,a[100007],b[100007];
void solve(){
    int i,j,min=10000000,l,k=0,l1;
    for(i=0;i<n;i++){
        scanf("%d",&a[i]);
        b[i]=a[i];
        if(a[i]<min){
            l=i;
            min=a[i];
        }
    }
    sort(b,b+n);
    if(l==0){
        for(i=1;i<n;i++){
            if(a[i]==min&&k==0){
                l1=i;
                min=a[i];
                k++;
                if(l1!=l+1){
                    l=l1;
                }
            }
        }
    }
    for(i=0;i<n;i++){
        j=(l+i)%n;
        if(b[i]!=a[j]){
            printf("-1\n");
            return;
        }
    }
    printf("%d\n",(n-l)%n);
}
int main() {
    #pragma comment(linker, "/STACK:67108864")
    #ifndef ONLINE_JUDGE
    freopen("input.txt", "rt", stdin);
    freopen("output.txt", "wt", stdout);
    #endif
    while(scanf("%d",&n)!=EOF){
        solve();
    }
    return 0;
}


#include <iostream>
using namespace std;
int main() {
    int t;
    cin >> t;
    while (t--) {
        int n, m;
        cin >> n >> m;
        const int N = 1001;
        int a[N] = {};
        for (int i = 0; i < n; ++i) {
            int num;
            cin >> num;
            a[num] = true;
        }
        int c = -1;
        for (int i = 0; i < m; ++i) {
            int num;
            cin >> num;
            if (a[num]) c = num;
        }
        if (c == -1) {
            cout << "NO" << endl;
        } else {
            cout << "YES" << endl << 1 << " " << c;
        }
        cout << endl;
    }

return 0;
}


#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<cmath>
using namespace std;
int main()
{
    int i,j,k,n,m,a[1000],b[1000],t;
    cin>>t;
    while(t--)
    {
        cin>>n>>m;
        for(j=0;j<n;j++)
        {
            cin>>a[j];
        }
        for(j=0;j<n;j++)
        {
            cin>>b[j];
        }
        for(j=0;j<n;j++)
        {
            for(k=0;k<n;k++)
            {
                if(a[j]==b[k])
                {
                    cout<<"YES"<<endl;

                    cout<<"1 "<<a[j]<<endl;
                    break;
                }
            }
        }
        if(j==n)
        {
            cout<<"NO"<<endl;
        }
    }
    return 0;
}


#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<cmath>
using namespace std;
int main()
{
    int i,j,k,n,m,a[1000],b[1000],t;
    cin>>t;
    while(t--)
    {
        cin>>n>>m;
        for(j=0;j<n;j++)
        {
            cin>>a[j];
        }
        for(j=0;j<n;j++)
        {
            cin>>b[j];
        }
        for(j=0;j<n;j++)
        {
            for(k=0;k<n;k++)
            {
                if(a[j]==b[k]);
                {
                    cout<<"YES"<<endl;
                    cout<<a[j]<<endl;
                    break;
                }
            }
        }
        if(j==n)
        {
            cout<<"NO"<<endl;
        }
    }
    return 0;
}


#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<cmath>
using namespace std;
int main()
{
    int i,j,k,n,m,a[1000],b[1000],t,flag =0;
    cin>>t;
    while(t--)
    {
        flag = 0;
        cin>>n>>m;
        for(j=0;j<n;j++)
        {
            cin>>a[j];
        }
        for(j=0;j<m;j++)
        {
            cin>>b[j];
        }
        for(j=0;j<n;j++)
        {
            for(k=0;k<m;k++)
            {
                if(a[j]==b[k])
                {
                    cout<<"YES"<<endl;

                    cout<<"1 "<<a[j]<<endl;
                    flag = 1;
                    break;
                }
            }
            if(flag==1)
                break;
        }
        if(flag==0)
        {
            cout<<"NO"<<endl;
        }
    }
    return 0;
}


#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<cmath>
using namespace std;
int main()
{
    int i,j,k,n,m,a[1000],b[1000],t,flag =0;
    cin>>t;
    while(t--)
    {
        cin>>n>>m;
        for(j=0;j<n;j++)
        {
            cin>>a[j];
        }
        for(j=0;j<n;j++)
        {
            cin>>b[j];
        }
        for(j=0;j<n;j++)
        {
            for(k=0;k<n;k++)
            {
                if(a[j]==b[k])
                {
                    cout<<"YES"<<endl;

                    cout<<"1 "<<a[j]<<endl;
                    flag = 1;
                    break;
                }
            }
        }
        if(flag==0)
        {
            cout<<"NO"<<endl;
        }
    }
    return 0;
}


#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<cmath>
using namespace std;
int main()
{
    int i,j,k,n,m,a[1000],b[1000],t,flag =0;
    cin>>t;
    while(t--)
    {
        flag = 0;
        cin>>n>>m;
        for(j=0;j<n;j++)
        {
            cin>>a[j];
        }
        for(j=0;j<m;j++)
        {
            cin>>b[j];
        }
        for(j=0;j<n;j++)
        {
            for(k=0;k<m;k++)
            {
                if(a[j]==b[k])
                {
                    cout<<"YES"<<endl;

                    cout<<"1 "<<a[j]<<endl;
                    flag = 1;
                    return 0;
                }
            }
        }
        if(flag==0)
        {
            cout<<"NO"<<endl;
        }
    }
    return 0;
}


#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<cmath>
using namespace std;
int main()
{
    int i,j,k,n,m,a[1000],b[1000],t;
    cin>>t;
    while(t--)
    {
        cin>>n>>m;
        for(j=0;j<n;j++)
        {
            cin>>a[j];
        }
        for(j=0;j<n;j++)
        {
            cin>>b[j];
        }
        for(j=0;j<n;j++)
        {
            for(k=0;k<n;k++)
            {
                if(a[j]==b[k]);
                {
                    cout<<"YES"<<endl;

                    cout<<"1"<<a[j]<<endl;
                    break;
                }
            }
        }
        if(j==n)
        {
            cout<<"NO"<<endl;
        }
    }
    return 0;
}


#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<cmath>
using namespace std;
int main()
{
    int i,j,k,n,m,a[1000],b[1000],t,flag =0;
    cin>>t;
    while(t--)
    {
        flag = 0;
        cin>>n>>m;
        for(j=0;j<n;j++)
        {
            cin>>a[j];
        }
        for(j=0;j<m;j++)
        {
            cin>>b[j];
        }
        for(j=0;j<n;j++)
        {
            for(k=0;k<m;k++)
            {
                if(a[j]==b[k])
                {
                    cout<<"YES"<<endl;

                    cout<<"1 "<<a[j]<<endl;
                    flag = 1;
                    break;
                }
            }
        }
        if(flag==0)
        {
            cout<<"NO"<<endl;
        }
    }
    return 0;
}


#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<cmath>
using namespace std;
int main()
{
    int i,j,k,n,m,a[1000],b[1000],t,flag =0;
    cin>>t;
    while(t--)
    {
        flag = 0;
        cin>>n>>m;
        for(j=0;j<n;j++)
        {
            cin>>a[j];
        }
        for(j=0;j<m;j++)
        {
            cin>>b[j];
        }
        for(j=0;j<n;j++)
        {
            for(k=0;k<m;k++)
            {
                if(a[j]==b[k])
                {
                    cout<<"YES"<<endl;

                    cout<<"1 "<<a[j]<<endl;
                    flag = 1;
                    return;
                }
            }
        }
        if(flag==0)
        {
            cout<<"NO"<<endl;
        }
    }
    return 0;
}


#include<cstdio>
#include<iostream>
#include<algorithm>
using namespace std;
const int maxn = 1e3+5;
int a[maxn],b[maxn];
int main()
{
	int T;cin >> T;
	while(T--)
	{
		int n,m;scanf("%d%d",&n,&m);
		for(int i = 0;i < n;++i)
			scanf("%d",a+i);
		for(int i = 0;i < m;++i)
			scanf("%d",b+i);
		sort(a,a+n);
		sort(b,b+m);
		bool flag = 1;
		for(int i = 0;i < n&&flag;++i)
			for(int j = 0;j < m;++j)
			{
				if(a[i] == b[j])
				{
					printf("YES\n1 %d\n",a[i]);
					flag = 0;
					break;
				}
			}
		if(flag)
			cout << "NO\n";
	}
	return 0;
 }


#include<cstdio>
#include<iostream>
#include<algorithm>
using namespace std;
const int maxn = 1e3+5;
int a[maxn],b[maxn];
int main()
{
	int T;cin >> T;
	while(T--)
	{
		int n,m;scanf("%d%d",&n,&m);
		for(int i = 0;i < n;++i)
			scanf("%d",a+i);
		for(int i = 0;i < m;++i)
			scanf("%d",b+i);
		sort(a,a+n);
		sort(b,b+m);
		bool flag = 1;
		for(int i = 0;i < n&&flag;++i)
			for(int j = 0;j < m;++j)
			{
				if(a[i] == b[j])
				{
					printf("YES\n1 %d\n",a[i]);
					flag = 0;
					break;
				}
				else if(a[i] < b[j]&&i+1 < n)
					++i;
			}
		if(flag)
			cout << "NO\n";
	}
	return 0;
 }


#include<iostream>
using namespace std;
int main(){
	int t;
	cin >> t;
	while(t--){
		int n,m;
		int flag[100001];
		cin >> n >> m;
		int sign = 0;
		int same;
		for(int i=0;i<n;i++){
			int a;
			cin >> a;
			flag[a] = 1;
		}
		for(int i=0;i<m;i++){
			int a;
			cin >> a;
			if(flag[a] == 1){
				sign = 1;
				same = a;
			}
		}
		if(sign == 0) cout << "NO" << endl;
		else{
			cout <<"YES" << endl;
			cout << 1 << " " << same << endl;
		}
	}
	return 0;
}


#include<iostream>
#include<cstring>
using namespace std;
int main(){
	int t;
	cin >> t;
	while(t--){
		int n,m;
		int flag[100001];
		memset(flag,0,sizeof(flag));
		cin >> n >> m;
		int sign = 0;
		int same;
		for(int i=0;i<n;i++){
			int a;
			cin >> a;
			flag[a] = 1;
		}
		for(int i=0;i<m;i++){
			int a;
			cin >> a;
			if(flag[a]){
				sign = 1;
				same = a;
			}
		}
		if(sign == 0) cout << "NO" << endl;
		else{
			cout <<"YES" << endl;
			cout << 1 << " " << same << endl;
		}
	}
	return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

int main() {
    int t, n, m, x;
    cin >> t;
    for (int q = 0; q < t; q++) {
        cin >> n >> m;
        vector<int> a, b;
        for (int i = 0; i < n; i++) {
            cin >> x;
            a.push_back(x);
        }
        for (int i = 0; i < m; i++) {
            cin >> x;
            b.push_back(x);
        }
        sort(a.begin(), a.end());
        sort(b.begin(), b.end());
        int i = 0, j = 0;
        bool found = false;
        while (i < n && j < m && !found) {
            if (a[i] > b[j]) {
                j++;
            }
            else if (a[i] < b[j]) {
                i++;
            }
            else {
                found = true;
                x = a[i];
            }
        }
        if (found) {
            cout << "YES" << endl;
            cout << "1 " << x << endl;
        }
        else {
            cout << "NO" << endl;
        }
    }
    return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <vector>
#include <queue>
#include <utility>
#include <functional>
#include <algorithm>

using namespace std;

long long t;

int main(){
	scanf("%lld", &t);
	while(t--){
		long long m,n;
		bool a[1050];

		for(long i=0; i<1050; ++i){a[i]=false;}

		scanf("%lld %lld", &m, &n);
		for(long i=0; i<m; ++i){
			long tmp;
			scanf("%ld", &tmp);
			a[tmp]=true;
		}

		bool yes=false;
		long long value=0;
		for(long i=0; i<n; ++i){
			long tmp;
			scanf("%ld", &tmp);
			if(!yes&&a[tmp]){yes=true; value=tmp;}
		}

		if(yes){
			printf("YES\n1 %lld\n", value);
		} else {
			printf("NO\n");
		}
	}
}


#include <iostream>
#include <cstring>
#include <cctype>
#include <string>
#include <math.h>
using namespace std;
int main()
{
    int t; cin >> t;
    while (t--) {
        int ct = 0;
        int n, m; cin >> n >> m;
        int* a = new int[n];
        int* b = new int[m];
        for (int i = 0; i < n; i++) cin >> a[i];
        for (int i = 0; i < m; i++) cin >> b[i];

        for (int j = 0; j < m; j++) {
            for (int i = 0; i < n; i++) {
                if (a[i] == b[j]) {
                    ct++;
                }
            }
            if (ct > 0) {
                cout << "YES" << endl;
                cout << 1 << " " << b[j] << endl;
                break;
            }
        }
        if (ct == 0)    cout << "NO" << endl;

    }
    return 0;
}


#include <iostream>
using namespace std;

int comp(const void* a, const void* b)
{
	return *(int*)a < *(int*)b ? -1 : *(int*)a > *(int*)b;
}

int main()
{
	int t,n,m;
	int* dataN;
	int* dataM;
	cin >> t;
	while (t-- > 0) {
		cin >> n >> m;
		dataN = new int[n];
		dataM = new int[m];
		for (int i = 0; i < n; i++) {
			cin >> dataN[i];
		}

		qsort(dataN, n, sizeof(int), comp);

		for (int i = 0; i < m; i++) {
			cin >> dataM[i];
		}

		for (int i = 0; i < m; i++)
		{
			for (int j = 0; j<n && dataN[j] <= dataM[i]; j++) {
				if (dataN[j] == dataM[i])
				{
					cout << "YES" << endl << 1 << ' ' << dataM[i] << endl;
					goto finish;
				}
			}

		}

        cout << "NO" << endl;

		finish:
		delete[] dataN;
		delete[] dataM;
	}

	return 0;
}


#include <iostream>
using namespace std;

int comp(const void* a, const void* b)
{
	return *(int*)a < *(int*)b ? -1 : *(int*)a > *(int*)b;
}

int main()
{
	int t,n,m;
	int* dataN;
	int* dataM;
	cin >> t;
	while (t-- > 0) {
		cin >> n >> m;
		dataN = new int[n];
		dataM = new int[m];
		for (int i = 0; i < n; i++) {
			cin >> dataN[i];
		}

		qsort(dataN, n, sizeof(int), comp);

		for (int i = 0; i < m; i++) {
			cin >> dataM[i];
		}

		for (int i = 0; i < m; i++)
		{
			for (int j = 0; dataN[j] <= dataM[i]; j++) {
				if (dataN[j] == dataM[i])
				{
					cout << "YES" << endl << 1 << ' ' << dataM[i] << endl;
					goto finish;
				}
			}

		}

        cout << "NO" << endl;

		finish:
		delete[] dataN;
		delete[] dataM;
	}

	return 0;
}


#include<iostream>
#include<unordered_map>
#define long long ll 1000;
using namespace std;
void solve()
{
    int n, m;
    cin >> n >> m;
    unordered_map<int, int> mp;
     /* code */

    while(n--)
    {
        int a;
        cin >> a;
        mp[a] = 1;
    }

    int ans = -1;

    while(m--)
    {
        int a;
        cin >> a;
        if(mp[a] == 1)
        {
            ans = a;
        }
    }
    if(ans!=1){
        cout << "YES\n"<<1<<" "<<ans<<"\n";
    }
    else
    {
        /* code */
        cout << "NO\n";
    }

    }
int main()
{
    ios_base :: sync_with_stdio(0);
    cin.tie(0);
int t;
cin>> t;
while (t--)
{

solve();
}
return 0;

}


#include<iostream>
#include<unordered_map>
#define long long ll 1000;
using namespace std;
void solve()
{
    int n, m;
    cin >> n >> m;
    unordered_map<int, int> mp;
     /* code */

    while(n--)
    {
        int a;
        cin >> a;
        mp[a] = 1;
    }

    int ans = -1;

    while(m--)
    {
        int a;
        cin >> a;
        if(mp[a] == 1)
        {
            ans = a;
        }
    }
    if(ans!=-1){
        cout << "YES\n"<<1<<" "<<ans<<"\n";
    }
    else
    {
        /* code */
        cout << "NO\n";
    }

    }
int main()
{
    ios_base :: sync_with_stdio(0);
    cin.tie(0);
int t;
cin>> t;
while (t--)
{

solve();
}
return 0;

}


#include<bits/stdc++.h>
#include<unordered_map>
#define long long ll 1000;
using namespace std;
void solve()
{
    int n, m;
    cin >> n >> m;
    map<int, int> mp;
     /* code */

    while(n--)
    {
        int a;
        cin >> a;
        mp[a] = 1;
    }

    int ans = -1;

    while(m--)
    {
        int a;
        cin >> a;
        if(mp[a] == 1)
        {
            ans = a;
        }
    }
    if(ans!=1){
        cout << "YES\n"<<1<<" "<<ans<<"\n";
    }
    else
    {
        /* code */
        cout << "NO\n";
    }

    }
int main()
{
    ios_base :: sync_with_stdio(0);
    cin.tie(0);
int t;
cin>> t;
while (t--)
{

solve();
}
return 0;

}


#include<iostream>
#include<math.h>
#include<cmath>
#include<vector>
#include<queue>
#include<algorithm>
#include<list>
#include<stack>
#include<map>
#include<deque>
#include<string>
#include<set>
#include<unordered_map>
#define IOT ios_base::sync_with_stdio(0);cin.tie(0);
#define Mod 1000000007
#define repi(a,n) for(int i=a;i<n;i++)
#define repj(a,n) for(int j=a;j<n;j++)
#define repk(a,n) for(int k=a;k<n;k++)
#define repe(a,n) for(int i=a;i<=n;i++)
#define TC  ll x; cin>>x; while(x--)
#define ull unsigned long long
#define ll long long
#define ld long double
using namespace std;
ll max(ll a, ll b)
{
	if (a >= b)return a;
	else return b;
}
void main(){
	IOT;
	TC
	{

		ll n, m;
		cin >> n >> m;
		map<ll, ll>mp1, mp2;
		map<ll, ll>::iterator it1 = mp1.begin();
		map<ll, ll>::iterator it2 = mp2.begin();
		repi(0, n){ ll k; cin >> k; mp1[k] = 1; }
		repj(0, m){ ll k; cin >> k; mp2[k] = 1; }
		bool x1 = 0;
		for (it1 = mp1.begin(); it1 != mp1.end(); it1++)
		{
			if (mp2.find(it1->first) != mp2.end()){
				cout << "YES" << endl;
				cout << 1 << ' ' << it1->first << endl; x1 = 1; break;
			}
			else x1 = 0;
		}
		bool x2 = 0;
		if (!x1)
		{
			for (it2 = mp2.begin(); it2 != mp2.end(); it2++)
			{
				if (mp1.find(it2->first) != mp1.end()){
					cout << "YES" << endl;
					cout << 1 << ' ' << it2->first << endl; x2 = 1; break;
				}
		else x2 = 0;
			}
		}
		if (!x1&&!x2){ cout << "NO" << endl;  }
	}

}


/* Task description
https://codeforces.com/problemset/problem/1382/A
*/

#include <iostream>
#include <set>

int main()
{
	int cases, n, m, num;
	bool found;
	std::set<int> collection;

	std::cin >> cases;
	for (int t = 0; t < cases; t++)
	{
		found = false;
		collection.clear();

		std::cin >> n >> m;
		for (int i = 0; i < n; i++)
		{
			std::cin >> num;
			collection.insert(num);
		}

		for (int i = 0; i < m; i++)
		{
			std::cin >> num;
			if (collection.find(num) != collection.end())
			{
				if (!found)
					std::cout << "YES" << std::endl << "1 " << num << std::endl;
				found = true;
			}
		}

		if (!found)
			std::cout << "NO" << std::endl;
	}
}


#include <iostream>
using namespace std;

int main() {
	int t;
	cin >> t;
	for (int test = 1; test <= t; test++) {
		int a[1000], b[1000], n, m, nr;
		bool exista = false;
		cin >> n >> m;
		for (int i = 0; i < n; i++)
			cin >> a[i];
		for (int i = 0; i < m; i++) {
			cin >> b[i];
			for (int j = 0; j < n; j++) {
				if (a[j] == b[i]) {
					exista = true;
					nr = a[j];
				}
			}
		}
		if (!exista)
			cout << "NO\n";
		else
			cout << "YES\n" << 1 << ' ' << nr << '\n';
	}
}


# include <iostream>
# include <cstdio>
# include <cstring>
# include <algorithm>
using namespace std;
int main() {
	int T, n, m, i;
	int a[1005], b[1005], flag[1005];
	scanf_s("%d", &T);
	while (T--) {
		scanf_s("%d %d", &n, &m);
		memset(flag, 0, sizeof(flag));
		for (i = 0; i < n; i++) {
			scanf_s("%d", &a[i]);
		}
		for (i = 0; i < m; i++) {
			scanf_s("%d", &b[i]);
			flag[b[i]] = 1;
		}
		for (i = 0; i < n; i++) {
			if (flag[a[i]]) {
				printf("YES\n1 %d\n", a[i]);
				break;
			}
		}
		if (i == n) {
			printf("NO\n");
		}
	}
	return 0;
}


#include<iostream>
#include<vector>

using namespace std;

void getans(vector<int>, vector<int> );

int main()
{
    int t;
    cin>>t;
    int i, j, l, m;
    vector<vector<int>> a(t), b(t);
    for(i=0; i<t; i++)
    {
        cin>>m;
        cin>>l;
        a[i].resize(m);
        b[i].resize(l);
        for(j=0; j<m; j++)
        cin>>a[i][j];
        for(j=0; j<l; j++)
        cin>>b[i][j];
    }
    for(i=0; i<t; i++)
    getans(a[i], b[i]);
}

void getans(vector<int> a, vector<int> b)
{
    int n=a.size(), m=b.size(), i, j, l;
    for(i=0; i<n; i++)
    {
        auto it = find(b.begin(), b.end(), a[i]);
        if(it!=b.end())
        {
            l = a[i];
            break;
        }
    }
    if(i!=n)
    {
        cout<<"YES"<<endl;
        cout<<"1"<<" "<<l<<endl;
    }
    else
    cout<<"NO"<<endl;
}


#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

int main() {
    int T;
    vector<int> arr1;
    vector<int> arr2;
    vector<int> result;

    cin >> T;
    while (T--) {
        arr1.clear();
        arr2.clear();
        result.clear();
        int n, m;

        cin >> n >> m;

        for (int i = 0; i < n; i++) {
            int num;

            cin >> num;
            arr1.push_back(num);
        }

        for (int i = 0; i < m; i++) {
            int num;

            cin >> num;
            arr2.push_back(num);
        }

        sort(arr1.begin(), arr1.end());
        sort(arr2.begin(), arr2.end());

        bool flag = true;

        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                if (arr1[i] == arr2[j]) {
                    cout << "YES\n";
                    cout << 1 << " " << arr1[i] << "\n";
                    flag = false;
                    break;
                }
            }
            if (!flag) break;
        }

        if (flag) cout << "NO\n";
    }
}


#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

int main() {
    int T;
    vector<int> arr1;
    vector<int> arr2;
    vector<int> result;

    cin >> T;
    while (T--) {
        arr1.clear();
        arr2.clear();
        result.clear();
        int n, m;

        cin >> n >> m;

        for (int i = 0; i < n; i++) {
            int num;

            cin >> num;
            arr1.push_back(num);
        }

        for (int i = 0; i < m; i++) {
            int num;

            cin >> num;
            arr2.push_back(num);
        }

        sort(arr1.begin(), arr1.end());
        sort(arr2.begin(), arr2.end());

        bool flag = true;

        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                if (arr1[i] == arr2[j]) {
                    cout << 1 << " " << arr1[i] << "\n";
                    flag = false;
                    break;
                }
            }
            if (!flag) break;
        }

        if (flag) cout << "NO\n";
    }
}


#include<iostream>
#include<string>
#include<algorithm>
#include<vector>
using namespace std;
int main() {
	int a, b, q, res;
	cin >> q;
	while (q--) {
		res = -1;
		cin >> a >> b;
		long* x = new long[a], * y = new long[b];
		for (int i = 0; i < a; i++)cin >> x[i];
		for (int i = 0; i < b; i++)cin >> y[i];
		sort(x, x + a);
		sort(y, y + b);
		//cout << x[0] << y[0];
		for (int i = 0; i < a; i++)
			for (int j = 0; j < b; j++)
				if (x[i] == y[j]) {
					//cout << x[i] << " " << y[j];
				res = x[i]; i = a; b = j;
			    }

		if (res == -1)cout << "NO\n";
		else cout << "YES\n" << 1 << " " << res << endl;
	}
}


#include <iostream>
#include <algorithm>
using namespace std;

int main()
{
    int t;
    cin >> t;

    while (t--)
    {
        int n, m, t[1001], ans = 0;
        cin >> n >> m;

        for (int i = 0; i <= 1000; i++) t[i] = 0;
        for (int in = 0; in < n; in++)
        {
            int a;
            cin >> a;
            t[a] = 1;
        }
        for (int in = 0; in < m; in++)
        {
            int a;
            cin >> a;
            if (t[a] == 1) ans = a;
        }

        if (ans) cout << "Yes\n" << 1 << " " << ans;
        else cout << "No\n";
    }
}


#include <iostream>
#include <algorithm>
using namespace std;

int main()
{
    int t;
    cin >> t;

    while (t--)
    {
        int n, m, t[1001], ans = 0;
        cin >> n >> m;

        for (int i = 0; i <= 1000; i++) t[i] = 0;
        for (int in = 0; in < n; in++)
        {
            int a;
            cin >> a;
            t[a] = 1;
        }
        for (int in = 0; in < m; in++)
        {
            int a;
            cin >> a;
            if (t[a] == 1) ans = a;
        }

        if (ans) cout << "Yes\n" << 1 << " " << ans << endl;
        else cout << "No\n";
    }
}


#include <iostream>
#include <algorithm>
using namespace std;

int main()
{
    int t;
    cin >> t;

    while (t--)
    {
        int n, m, a[1000], b[1000];
        cin >> n >> m;
        for (int in = 0; in < n; in++)
        {
            cin >> a[in];
        }
        for (int in = 0; in < m; in++)
        {
            cin >> b[in];
        }

        bool ok = 0;
        for (int i = 0; i < n; i++)
        {
            for (int j = 0; j < m; j++)
            {
                if (a[i] == b[j])
                {
                    cout << "Yes\n" << 1 << " " << a[i] << endl;
                    ok = 1;
                }
            }

            if (ok) break;
        }

        if (!ok) cout << "No\n";
    }
}


#include<iostream>
#include<set>
#include<vector>
using namespace std;
int main() {
	int t;
	cin >> t;
	set <int> A;
	for (; t >= 1; t--) {
		vector <int> answer;
		int n, m, f = 1;
		cin >> n >> m;
		for (int i = 0; i < n; i++) {
			int x;
			cin >> x;
			A.insert(x);
		}
		for (int i = 0; i < m; i++) {
			int x;
			cin >> x;
			if (A.find(x) != A.end() && f == 1) {
				answer.push_back(x);
				f = 0;
			}
		}
		if (answer.size()) {
			cout << "YES\n" << answer.size() << " ";
			for (auto n : answer) {
				cout << n << "\n";
			}
		}
		else {
			cout << "NO\n";
		}
	}
	return 0;
}