#include<bits/stdc++.h>
using namespace std;
#define all(v) v.begin(),v.end()
#define V vector
#define S string
#define pb push_back
#define pf push_front
#define sz size()
#define get cin>>
#define print cout<<
#define OO 1e6
#define set(a,b) memset(a,b,sizeof(a))
#define mp(a,b) make_pair(a,b)
#define pii pair<int,int>
#define getArr(ar,n) for(int i=0;i<n;get ar[i++])
#define printArr(ar,n) for(int i=0;i<n;print ar[i++]<<' ')
#define Sort(ar,n) sort(ar,ar+n)
#define getPoint(x,y) get x>>y
#define dist(x1,y1,x2,y2) (x1-x2)*(x1-x2) + ((y1-y2)*(y1-y2))
typedef long long ll;
typedef unsigned long long ull;
int main(){
    cin.tie(0);
    std::ios::sync_with_stdio(false);
    //freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);
    int n; get n; deque<int>ar;
    for(int i=0;i<n;i++){int x; get x; ar.pb(x);}
    int nonDec=1,dec=1;
    for(int i=0;i<n-1;i++){
        if(ar[i]>=ar[i+1])dec++;
        else {
            if(dec>1)break;
        }
    }
    for(int i=0;i<n-1;i++){
        if(ar[i]<=ar[i+1])nonDec++;
        else {
            if(nonDec>1)break;
        }
    }
    if(nonDec==n){print 0; return 0;}
    if(dec==n){print n-1; return 0;}
    //if(dec+nonDec!=n){print -1; return 0;}
    int mn = min(dec,nonDec);
    int ans = mn;
    while (mn--){
        int x = ar.back();
        ar.pf(x); ar.pop_back();
    }
    for(int i=0;i<n-1;i++){
        if(ar[i]>ar[i+1]){print -1; return 0;}
    }print dec<<' '<<nonDec<<' ' << ans;
}
/*
*/


#include<bits/stdc++.h>
using namespace std;
#define all(v) v.begin(),v.end()
#define V vector
#define S string
#define pb push_back
#define pf push_front
#define sz size()
#define get cin>>
#define print cout<<
#define OO 1e6
#define set(a,b) memset(a,b,sizeof(a))
#define mp(a,b) make_pair(a,b)
#define pii pair<int,int>
#define getArr(ar,n) for(int i=0;i<n;get ar[i++])
#define printArr(ar,n) for(int i=0;i<n;print ar[i++]<<' ')
#define Sort(ar,n) sort(ar,ar+n)
#define getPoint(x,y) get x>>y
#define dist(x1,y1,x2,y2) (x1-x2)*(x1-x2) + ((y1-y2)*(y1-y2))
typedef long long ll;
typedef unsigned long long ull;
int main(){
    cin.tie(0);
    std::ios::sync_with_stdio(false);
    //freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);
    int n; get n; deque<int>ar;
    for(int i=0;i<n;i++){int x; get x; ar.pb(x);}
    int b =-1;
    for(int i=0;i<n-1;i++)if(ar[i]>ar[i+1]){b=i; break;}
    if(b==-1){print 0;}
    else {
        int mn,ans ; mn=ans= n-b-1;
        while (mn--){
            ar.pf(ar.back()); ar.pop_back();
        }
        for(int i=0;i<n-1;i++)if(ar[i]>ar[i+1]){print -1; return 0;}
        print ans;
    }
}
/*
*/


#include<bits/stdc++.h>
using namespace std;
#define all(v) v.begin(),v.end()
#define V vector
#define S string
#define pb push_back
#define pf push_front
#define sz size()
#define get cin>>
#define print cout<<
#define OO 1e6
#define set(a,b) memset(a,b,sizeof(a))
#define mp(a,b) make_pair(a,b)
#define pii pair<int,int>
#define getArr(ar,n) for(int i=0;i<n;get ar[i++])
#define printArr(ar,n) for(int i=0;i<n;print ar[i++]<<' ')
#define Sort(ar,n) sort(ar,ar+n)
#define getPoint(x,y) get x>>y
#define dist(x1,y1,x2,y2) (x1-x2)*(x1-x2) + ((y1-y2)*(y1-y2))
typedef long long ll;
typedef unsigned long long ull;
int main(){
    cin.tie(0);
    std::ios::sync_with_stdio(false);
    //freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);
    int n; get n; deque<int>ar;
    for(int i=0;i<n;i++){int x; get x; ar.pb(x);}
    int nonDec=1,dec=1;
    for(int i=0;i<n-1;i++){
        if(ar[i]>=ar[i+1])dec++;
        else {
            if(dec>1)break;
        }
    }
    for(int i=0;i<n-1;i++){
        if(ar[i]<=ar[i+1])nonDec++;
        else {
            if(nonDec>1)break;
        }
    }
    if(nonDec==n){print 0; return 0;}
    if(dec==n){print n-1; return 0;}
    //if(dec+nonDec!=n){print -1; return 0;}
    int mn = min(dec,nonDec);
    int ans = mn;
    while (mn--){
        int x = ar.back();
        ar.pf(x); ar.pop_back();
    }
    for(int i=0;i<n-1;i++){
        if(ar[i]>ar[i+1]){print ans<<' '<<-1; return 0;}
    }print ans;
}
/*
*/


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

int main()
{
    #ifdef VSP4
    //freopen("input.txt", "rt", stdin);
    //freopen("output.txt", "wt", stdout);
    #endif // VSP4

    ios_base::sync_with_stdio(false);

    int n, arr[100000], i, minindex = 0;

    cin >> n;
    for (i = 0; i < n; i++)
    {
        cin >> arr[i];
        if (arr[i] < arr[minindex])
            minindex = i;
    }

    bool possible = true;

    for (i = 1; i < minindex; i++)
    {
        if (!(arr[i-1] <= arr[i]))
        {
            possible = false;
            break;
        }
    }

    for (i = minindex + 1; i < n; i++)
    {
        if (!(arr[i-1] <= arr[i]))
        {
            possible = false;
            break;
        }
    }

    if (minindex == 0 && possible)
        cout << 0;
    else if (possible)
        cout << n - minindex;
    else
        cout << -1;

    return 0;
}


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

bool isNonDecreasing(int arr[], int N)
{
    for (int i = 1; i < N; i++)
        if (arr[i - 1] > arr[i])
            return false;
    return true;
}

void shiftArr(int arr[], int N)
{
    int last = arr[N - 1];
    for (int i = N - 1; i > 0; i--)
        arr[i] = arr[i-1];
    arr[0] = last;
}

int main()
{
    #ifdef VSP4
    //freopen("input.txt", "rt", stdin);
    //freopen("output.txt", "wt", stdout);
    #endif // VSP4

    ios_base::sync_with_stdio(false);

    int n, arr[100000], last, i;

    cin >> n;
    for (i = 0; i < n; i++)
        cin >> arr[i];

    int shifts = 0;
    while(shifts <= n)
    {
        if (isNonDecreasing(arr, n))
            break;
        shiftArr(arr, n);
        shifts++;
    }

    if (shifts > n)
        cout << -1;
    else
        cout << shifts;

    return 0;
}


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

bool isNonDecreasing(int arr[], int N)
{
    for (int i = 1; i < N; i++)
        if (arr[i - 1] > arr[i])
            return false;
    return true;
}

int main()
{
    #ifdef VSP4
    //freopen("input.txt", "rt", stdin);
    //freopen("output.txt", "wt", stdout);
    #endif // VSP4

    ios_base::sync_with_stdio(false);

    int n, arr[100000], last, i;

    cin >> n;
    for (i = 0; i < n; i++)
        cin >> arr[i];

    if (isNonDecreasing(arr, n))
    {
        cout << 0;
    }
    else
    {
        last = arr[n-1];
        for (i = 1; i < n; i++)
            arr[i] = arr[i - 1];
        arr[0] = last;

        if (isNonDecreasing(arr, n))
            cout << 1;
        else
            cout << -1;
    }

    return 0;
}


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

int main()
{
    #ifdef VSP4
    //freopen("input.txt", "rt", stdin);
    //freopen("output.txt", "wt", stdout);
    #endif // VSP4

    ios_base::sync_with_stdio(false);

    int n, arr[100000], last, i;

    cin >> n;
    for (i = 0; i < n; i++)
        cin >> arr[i];

    int startSegment[100], endSegment[100];
    int segmentIndex = 0;

    startSegment[0] = 0;
    endSegment[0] = 0;

    for (i = 1; i < n; i++)
    {
        if (arr[i - 1] <= arr[i])
            endSegment[segmentIndex]++; //endSegment[segmentIndex] = i; endSegment[segmentIndex]++;
        else
        {
            segmentIndex++;
            startSegment[segmentIndex] = i;
            endSegment[segmentIndex] = i;
        }
    }

    /*
    cout << segmentIndex << "\n";

    for (i = 0; i <= segmentIndex; i++)
    {
        cout << i << " " << startSegment[i] << " " << endSegment[i] << "\n";
    }
    */

    if (segmentIndex > 1)
        cout << -1;
    else if (segmentIndex == 0)
        cout << 0;
    else //total segments are 2 (0, 1) for sure
    {
        if (arr[endSegment[1]] <= arr[startSegment[0]])
            cout << endSegment[1] - startSegment[1] + 1;
        else
            cout << -1;
    }

    return 0;
}


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

int main()
{
    #ifdef VSP4
    //freopen("input.txt", "rt", stdin);
    //freopen("output.txt", "wt", stdout);
    #endif // VSP4

    ios_base::sync_with_stdio(false);

    int n, arr[100000], i, minindex = 0;

    cin >> n;

    cin >> arr[0];

    for (i = 1; i < n; i++)
    {
        cin >> arr[i];
        if (arr[i] < arr[minindex])
            minindex = i;
        else if (arr[i - 1] != arr[minindex] && arr[i] == arr[minindex])
            minindex = i;
    }

    bool possible = true;

    for (i = 1; i < minindex; i++)
    {
        if (!(arr[i-1] <= arr[i]))
        {
            possible = false;
            break;
        }
    }

    for (i = minindex + 1; i < n; i++)
    {
        if (!(arr[i-1] <= arr[i]))
        {
            possible = false;
            break;
        }
    }

    if (minindex > 0 && arr[n - 1] > arr[0])
        possible = false;

    if (minindex == 0 && possible)
        cout << 0;
    else if (possible)
        cout << n - minindex;
    else
        cout << -1;

    return 0;
}


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

int main()
{
    #ifdef VSP4
    //freopen("input.txt", "rt", stdin);
    //freopen("output.txt", "wt", stdout);
    #endif // VSP4

    ios_base::sync_with_stdio(false);

    int n, arr[100000], i, j, minindex = 0;
    vector<int> minIndexes;

    cin >> n;
    for (i = 0; i < n; i++)
    {
        cin >> arr[i];
        if (arr[i] < arr[minindex])
        {
            minIndexes.clear();
            minindex = i;
        }
        else if (arr[i] == arr[minindex])
        {
            minIndexes.push_back(i);
        }
    }

    bool globalPossible = false, possible;

    for (j = 0; j < minIndexes.size(); j++)
    {
        minindex = minIndexes[j];
        possible = true;

        for (i = 1; i < minindex; i++)
        {
            if (!(arr[i-1] <= arr[i]))
            {
                possible = false;
                break;
            }
        }

        for (i = minindex + 1; i < n; i++)
        {
            if (!(arr[i-1] <= arr[i]))
            {
                possible = false;
                break;
            }
        }

        if (minindex > 0 && arr[n - 1] > arr[0])
            possible = false;

        if (possible)
        {
            globalPossible = true;
            break;
        }
    }

    if (minindex == 0 && globalPossible)
        cout << 0;
    else if (globalPossible)
        cout << n - minindex;
    else
        cout << -1;

    return 0;
}


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

int main()
{
    #ifdef VSP4
    //freopen("input.txt", "rt", stdin);
    //freopen("output.txt", "wt", stdout);
    #endif // VSP4

    ios_base::sync_with_stdio(false);

    int n, arr[100000], i, minindex = 0;

    cin >> n;
    for (i = 0; i < n; i++)
    {
        cin >> arr[i];
        if (arr[i] < arr[minindex])
            minindex = i;
    }

    bool possible = true;

    for (i = 1; i < minindex; i++)
    {
        if (!(arr[i-1] <= arr[i]))
        {
            possible = false;
            break;
        }
    }

    for (i = minindex + 1; i < n; i++)
    {
        if (!(arr[i-1] <= arr[i]))
        {
            possible = false;
            break;
        }
    }

    if (minindex > 0 && arr[n - 1] > arr[0])
        possible = false;

    if (minindex == 0 && possible)
        cout << 0;
    else if (possible)
        cout << n - minindex;
    else
        cout << -1;

    return 0;
}


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

bool isNonDecreasing(int arr[], int N)
{
    for (int i = 1; i < N; i++)
        if (arr[i - 1] > arr[i])
            return false;
    return true;
}

int main()
{
    #ifdef VSP4
    //freopen("input.txt", "rt", stdin);
    //freopen("output.txt", "wt", stdout);
    #endif // VSP4

    ios_base::sync_with_stdio(false);

    int n, arr[100000], last, i;

    cin >> n;
    for (i = 0; i < n; i++)
        cin >> arr[i];

    int startSegment[100], endSegment[100];
    int segmentIndex = 0;

    startSegment[0] = 0;
    endSegment[0] = 0;

    for (i = 1; i < n; i++)
    {
        if (arr[i - 1] <= arr[i])
            endSegment[segmentIndex]++; //endSegment[segmentIndex] = i; endSegment[segmentIndex]++;
        else
        {
            segmentIndex++;
            startSegment[segmentIndex] = i;
            endSegment[segmentIndex] = i;
        }
    }

    /*
    cout << segmentIndex << "\n";

    for (i = 0; i <= segmentIndex; i++)
    {
        cout << i << " " << startSegment[i] << " " << endSegment[i] << "\n";
    }
    */

    if (segmentIndex > 1)
        cout << -1;
    else if (segmentIndex == 0)
        cout << 0;
    else //total segments are 2 (0, 1) for sure
    {
        if (arr[endSegment[1]] <= arr[startSegment[0]])
            cout << endSegment[1] - startSegment[1] + 1;
        else
            cout << -1;
    }

    return 0;
}


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

int main()
{
    #ifdef VSP4
    //freopen("input.txt", "rt", stdin);
    //freopen("output.txt", "wt", stdout);
    #endif // VSP4

    ios_base::sync_with_stdio(false);

    int n, arr[100000], i, j, minindex = 0;
    vector<int> minIndexes;

    cin >> n;
    for (i = 0; i < n; i++)
    {
        cin >> arr[i];
        if (arr[i] < arr[minindex])
        {
            minIndexes.clear();
            minIndexes.push_back(i);
            minindex = i;
        }
        else if (arr[i] == arr[minindex])
        {
            minIndexes.push_back(i);
        }
    }

    bool globalPossible = false, possible;

    for (j = 0; j < minIndexes.size(); j++)
    {
        minindex = minIndexes[j];
        possible = true;

        for (i = 1; i < minindex; i++)
        {
            if (!(arr[i-1] <= arr[i]))
            {
                possible = false;
                break;
            }
        }

        for (i = minindex + 1; i < n; i++)
        {
            if (!(arr[i-1] <= arr[i]))
            {
                possible = false;
                break;
            }
        }

        if (minindex > 0 && arr[n - 1] > arr[0])
            possible = false;

        if (possible)
        {
            globalPossible = true;
            break;
        }
    }

    if (minindex == 0 && globalPossible)
        cout << 0;
    else if (globalPossible)
        cout << n - minindex;
    else
        cout << -1;

    return 0;
}


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

bool isNonDecreasing(int arr[], int N)
{
    for (int i = 1; i < N; i++)
        if (arr[i - 1] > arr[i])
            return false;
    return true;
}

int main()
{
    #ifdef VSP4
    //freopen("input.txt", "rt", stdin);
    //freopen("output.txt", "wt", stdout);
    #endif // VSP4

    ios_base::sync_with_stdio(false);

    int n, arr[100000], i;

    cin >> n;
    for (i = 0; i < n; i++)
        cin >> arr[i];

    if (isNonDecreasing(arr, n))
    {
        cout << 0;
        return 0;
    }

    swap(arr[0], arr[n - 1]);
    if (isNonDecreasing(arr, n))
        cout << 1;
    else
        cout << -1;

    return 0;
}


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

bool isNonDecreasing(int arr[], int N)
{
    for (int i = 1; i < N; i++)
        if (arr[i - 1] > arr[i])
            return false;
    return true;
}

int main()
{
    #ifdef VSP4
    //freopen("input.txt", "rt", stdin);
    //freopen("output.txt", "wt", stdout);
    #endif // VSP4

    ios_base::sync_with_stdio(false);

    int n, arr[100000], last, i;

    cin >> n;
    for (i = 0; i < n; i++)
        cin >> arr[i];

    if (isNonDecreasing(arr, n))
    {
        cout << 0;
    }
    else
    {
        last = arr[n-1];
        for (i = n; i > 0; i--)
            arr[i] = arr[i - 1];
        arr[0] = last;

        if (isNonDecreasing(arr, n))
            cout << 1;
        else
            cout << -1;
    }

    return 0;
}


#include<iostream>
#include<bits/stdc++.h>
#include<sstream>
#include<string>
#include<stdlib.h>
#define ll long long int
#define ld long double
#define mod 1000000007

using namespace std;

int main()
{
    ll n;
    cin>>n;
    ll a[n];
    for(ll i=0;i<n;i++)
        cin>>a[i];
    ll cntd=0, maxi=0, mini=0, maxe=a[0], mine=a[0];
    for(ll i=1;i<n;i++)
    {
        if(a[i-1]>a[i])
            cntd++;
        if(maxe<a[i])
        {
            maxe=a[i];
            maxi=i;
        }
        if(mine>a[i])
        {
            mine=a[i];
            mini=i;
        }
    }
    if(!cntd)
    {
        cout<<0<<endl;
    }
    else
    {
        bool check=true;
        if(mini<maxi)
        check=false;

        for(ll i=maxi;i>0;i--)
        {
            if(a[i-1]>a[i])
            {
                check=false;
                break;
            }
        }
        for(ll i=mini;i<n-1;i++)
        {
            if(a[i]>a[i+1])
            {
                check=false;
                break;
            }
        }
        if(!check)
        {
            cout<<-1<<endl;
        }

        else
        {
            ll ans=n-mini;
        cout<<ans<<endl;
        }
    }

    return 0;
}


#include<iostream>
#include<bits/stdc++.h>
#include<sstream>
#include<string>
#include<stdlib.h>
#define ll long long int
#define ld long double
#define mod 1000000007

using namespace std;

int main()
{
    ll n;
    cin>>n;
    ll a[n];
    for(ll i=0;i<n;i++)
        cin>>a[i];
    ll cntd=0, maxi=0, mini=0, maxe=a[0], mine=a[0];
    for(ll i=1;i<n;i++)
    {
        if(a[i-1]>a[i])
            cntd++;
        if(maxe<a[i])
        {
            maxe=a[i];
            maxi=i;
        }
        if(mine>a[i])
        {
            mine=a[i];
            mini=i;
        }
    }
    if(!cntd)
    {
        cout<<0<<endl;
    }
    else
    {
        bool check=true;
        if(mini<maxi)
        check=false;

        for(ll i=maxi;i>0;i--)
        {
            if(a[i-1]>a[i])
            {
                check=false;
                break;
            }
        }
        for(ll i=mini;i<n-1;i++)
        {
            if(a[i]>a[i+1])
            {
                check=false;
                break;
            }
        }
        if(mini!=maxi+1)
            check=false;
        if(!check)
        {
            cout<<-1<<endl;
        }

        else
        {
            ll ans=n-mini;
        cout<<ans<<endl;
        }
    }

    return 0;
}


#include<iostream>
#include<bits/stdc++.h>
#include<sstream>
#include<string>
#include<stdlib.h>
#define ll long long int
#define ld long double
#define mod 1000000007

using namespace std;

int main()
{
    ll n;
    cin>>n;
    ll a[n];
    for(ll i=0;i<n;i++)
        cin>>a[i];
    ll cntd=0,  maxi=0, mini=0, maxe=a[0], mine=a[0];
    for(ll i=1;i<n;i++)
    {
        if(a[i-1]>a[i])
            cntd++;
        if(maxe<a[i])
        {
            maxe=a[i];
            maxi=i;
        }
        if(mine>a[i])
        {
            mine=a[i];
            mini=i;
        }
    }
    if(!cntd)
    {
        cout<<0<<endl;
    }
    else
    {
        bool check=true;
        if(mini<maxi)
        check=false;

        for(ll i=maxi;i>0;i--)
        {
            if(a[i-1]>a[i])
            {
                check=false;
                break;
            }
        }
        for(ll i=mini;i<n-1;i++)
        {
            if(a[i]>a[i+1])
            {
                check=false;
                break;
            }
        }
        if(!check)
        {
            cout<<-1<<endl;
        }

        else
        {
            ll ans=n-mini;
        cout<<ans<<endl;
        }
    }

    return 0;
}


#include<iostream>
#include<bits/stdc++.h>
#include<sstream>
#include<string>
#include<stdlib.h>
#define ll long long int
#define ld long double
#define mod 1000000007

using namespace std;

int main()
{
    ll n;
    cin>>n;
    ll a[n];
    for(ll i=0;i<n;i++)
        cin>>a[i];
    ll cntd=0, maxe=a[0], mine=a[0], mini=-2, maxi=-1;
    for(ll i=1;i<n;i++)
    {
        if(a[i-1]>a[i])
            cntd++;
        if(maxe<a[i])
        {
            maxe=a[i];
        }
        if(mine>a[i])
        {
            mine=a[i];
        }
    }
    if(!cntd)
    {
        cout<<0<<endl;
    }
    else
    {
        bool check=true;
        for(ll i=0;i<n;i++)
        {
            if(a[i]==maxe)
            {
                maxi=i;
                break;
            }
        }
        for(ll i=maxi+1;i<n;i++)
        {
            if(a[i]==mine)
            {
                mini=i;
                break;
            }
        }

        for(ll i=maxi;i>0;i--)
        {
            if(a[i-1]>a[i])
            {
                check=false;
                break;
            }
        }
        for(ll i=mini;i<n-1;i++)
        {
            if(a[i]>a[i+1])
            {
                check=false;
                break;
            }
        }
        if(a[mini-1]!=maxe)
            check=false;
        if(!check)
        {
            cout<<-1<<endl;
        }

        else
        {
            ll ans=n-mini;
        cout<<ans<<endl;
        }
    }

    return 0;
}


#include<iostream>
#include<bits/stdc++.h>
#include<sstream>
#include<string>
#include<stdlib.h>
#define ll long long int
#define ld long double
#define mod 1000000007

using namespace std;

int main()
{
    ll n;
    cin>>n;
    ll a[n];
    for(ll i=0;i<n;i++)
        cin>>a[i];
    ll cntd=0, maxe=a[0], mine=a[0], mini=-2, maxi=-1;
    for(ll i=1;i<n;i++)
    {
        if(a[i-1]>a[i])
            cntd++;
        if(maxe<a[i])
        {
            maxe=a[i];
        }
        if(mine>a[i])
        {
            mine=a[i];
        }
    }
    if(!cntd)
    {
        cout<<0<<endl;
    }
    else
    {
        bool check=true;
        for(ll i=0;i<n;i++)
        {
            if(a[i]==maxe)
            {
                maxi=i;
                break;
            }
        }
        for(ll i=maxi+1;i<n;i++)
        {
            if(a[i]==mine)
            {
                mini=i;
                break;
            }
        }

        for(ll i=maxi;i>0;i--)
        {
            if(a[i-1]>a[i])
            {
                check=false;
                break;
            }
        }
        for(ll i=mini;i<n-1;i++)
        {
            if(a[i]>a[i+1])
            {
                check=false;
                break;
            }
        }
        if(a[mini-1]!=maxe)
            check=false;
        if(a[n-1] >a[0])
                check=false;
        if(!check)
        {
            cout<<-1<<endl;
        }

        else
        {
            ll ans=n-mini;
        cout<<ans<<endl;
        }
    }

    return 0;
}


#include<iostream>
#include<bits/stdc++.h>
#include<sstream>
#include<string>
#include<stdlib.h>
#define ll long long int
#define ld long double
#define mod 1000000007

using namespace std;

int main()
{
    ll n;
    cin>>n;
    ll a[n];
    for(ll i=0;i<n;i++)
        cin>>a[i];
    ll cntd=0, maxi=0, mini=0, maxe=a[0], mine=a[0];
    for(ll i=1;i<n;i++)
    {
        if(a[i-1]>a[i])
            cntd++;
        if(maxe<=a[i])
        {
            maxe=a[i];
            maxi=i;
        }
        if(mine>a[i])
        {
            mine=a[i];
            mini=i;
        }
    }
    if(!cntd)
    {
        cout<<0<<endl;
    }
    else
    {
        bool check=true;
        if(mini<maxi)
        check=false;

        for(ll i=maxi;i>0;i--)
        {
            if(a[i-1]>a[i])
            {
                check=false;
                break;
            }
        }
        for(ll i=mini;i<n-1;i++)
        {
            if(a[i]>a[i+1])
            {
                check=false;
                break;
            }
        }
        if(mini-maxi!=1)
            check=false;
        if(!check)
        {
            cout<<-1<<endl;
        }

        else
        {
            ll ans=n-mini;
        cout<<ans<<endl;
        }
    }

    return 0;
}


#include<iostream>
#include<bits/stdc++.h>
#include<sstream>
#include<string>
#include<stdlib.h>
#define ll long long int
#define ld long double
#define mod 1000000007

using namespace std;

int main()
{
    ll n;
    cin>>n;
    ll a[n];
    for(ll i=0;i<n;i++)
        cin>>a[i];
    ll cntd=0, maxi=0, mini=0, maxe=a[0], mine=a[0];
    for(ll i=1;i<n;i++)
    {
        if(a[i-1]>a[i])
            cntd++;
        if(maxe<a[i])
        {
            maxe=a[i];
            maxi=i;
        }
        if(mine>a[i])
        {
            mine=a[i];
            mini=i;
        }
    }
    if(!cntd)
    {
        cout<<0<<endl;
    }
    else
    {
        bool check=true;
        if(mini<maxi)
        check=false;

        for(ll i=maxi;i>0;i--)
        {
            if(a[i-1]>a[i])
            {
                check=false;
                break;
            }
        }
        for(ll i=mini;i<n-1;i++)
        {
            if(a[i]>a[i+1])
            {
                check=false;
                break;
            }
        }
        if(a[mini-1]!=maxe)
            check=false;
        if(!check)
        {
            cout<<-1<<endl;
        }

        else
        {
            ll ans=n-mini;
        cout<<ans<<endl;
        }
    }

    return 0;
}


#include<iostream>
#include<bits/stdc++.h>
#include<sstream>
#include<string>
#include<stdlib.h>
#define ll long long int
#define ld long double
#define mod 1000000007

using namespace std;

int main()
{
    ll n;
    cin>>n;
    ll a[n];
    for(ll i=0;i<n;i++)
        cin>>a[i];
    ll cntd=0, maxi=0, mini=0, maxe=a[0], mine=a[0];
    for(ll i=1;i<n;i++)
    {
        if(a[i-1]>a[i])
            cntd++;
        if(maxe<a[i])
        {
            maxe=a[i];
            maxi=i;
        }
        if(mine>a[i])
        {
            mine=a[i];
            mini=i;
        }
    }
    if(!cntd)
    {
        cout<<0<<endl;
    }
    else
    {
        bool check=true;
        if(mini<maxi)
        check=false;

        for(ll i=maxi;i>0;i--)
        {
            if(a[i-1]>a[i])
            {
                check=false;
                break;
            }
        }
        for(ll i=mini;i<n-1;i++)
        {
            if(a[i]>a[i+1])
            {
                check=false;
                break;
            }
        }
        if(mini!= (maxi+1))
            check=false;
        if(!check)
        {
            cout<<-1<<endl;
        }

        else
        {
            ll ans=n-mini;
        cout<<ans<<endl;
        }
    }

    return 0;
}


#include<iostream>
#include<bits/stdc++.h>
#include<sstream>
#include<string>
#include<stdlib.h>
#define ll long long int
#define ld long double
#define mod 1000000007

using namespace std;

int main()
{
    ll n;
    cin>>n;
    ll a[n];
    for(ll i=0;i<n;i++)
        cin>>a[i];
    ll cntd=0, maxi=0, mini=0, maxe=a[0], mine=a[0];
    for(ll i=1;i<n;i++)
    {
        if(a[i-1]>a[i])
            cntd++;
        if(maxe<=a[i])
        {
            maxe=a[i];
            maxi=i;
        }
        if(mine>a[i])
        {
            mine=a[i];
            mini=i;
        }
    }
    if(!cntd)
    {
        cout<<0<<endl;
    }
    else
    {
        bool check=true;
        if(mini<maxi)
        check=false;

        for(ll i=maxi;i>0;i--)
        {
            if(a[i-1]>a[i])
            {
                check=false;
                break;
            }
        }
        for(ll i=mini;i<n-1;i++)
        {
            if(a[i]>a[i+1])
            {
                check=false;
                break;
            }
        }
        if(a[mini-1]!=maxe)
            check=false;
        if(!check)
        {
            cout<<-1<<endl;
        }

        else
        {
            ll ans=n-mini;
        cout<<ans<<endl;
        }
    }

    return 0;
}


#include<iostream>
#include<bits/stdc++.h>
#include<sstream>
#include<string>
#include<stdlib.h>
#define ll long long int
#define ld long double
#define mod 1000000007

using namespace std;

int main()
{
    ll n;
    cin>>n;
    ll a[n];
    for(ll i=0;i<n;i++)
        cin>>a[i];
    ll cntd=0, maxi=0, mini=0, maxe=a[0], mine=a[0];
    for(ll i=1;i<n;i++)
    {
        if(a[i-1]>a[i])
            cntd++;
        if(maxe<=a[i])
        {
            maxe=a[i];
            maxi=i;
        }
        if(mine>a[i])
        {
            mine=a[i];
            mini=i;
        }
    }
    if(!cntd)
    {
        cout<<0<<endl;
    }
    else
    {
        bool check=true;
        if(mini<maxi)
        check=false;

        for(ll i=maxi;i>0;i--)
        {
            if(a[i-1]>a[i])
            {
                check=false;
                break;
            }
        }
        for(ll i=mini;i<n-1;i++)
        {
            if(a[i]>a[i+1])
            {
                check=false;
                break;
            }
        }
        if(mini!= (maxi+1))
            check=false;
        if(!check)
        {
            cout<<-1<<endl;
        }

        else
        {
            ll ans=n-mini;
        cout<<ans<<endl;
        }
    }

    return 0;
}


#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <queue>
#include <cstring>
#include <climits>
#include <vector>
using namespace std;
int date[100005];
int main()
{
    int n,ans,min_=INT_MAX,max_=INT_MIN;
    while(scanf("%d",&n)==1)
    {
        memset(date,0,sizeof(date));
        ans=0;
        for(int i=0;i<n;i++)
        {
            scanf("%d",&date[i]);
            min_=min(min_,date[i]);
            max_=max(max_,date[i]);
        }
        int flag=0;
        int temp;
        if(date[0]==min_) // ��������������
        {
                int j=0;
                while(date[j+1]>=date[j])
                {
                  j++;
                }
                if(j!=n-1&&date[j+1]!=min_)
                    flag=1;
                else if(j!=n-1&&date[j+1]==min_)
                {

                    j++;
                     int en=j;
                    while(date[j+1]==date[j])
                        j++;
                    if(j!=n-1) flag=1;
                    else {
                            ans=n-en;
                        //printf("%d\n",j);
                    }

                }
        }
        else if(date[0]!=max_)
        {
            int start=date[0];
            int j=1;
            while(date[j+1]>=date[j])
            {
                 if(date[j+1]==max_) break;
                 j++;
            }
            if(date[j+1]!=max_) flag=1;
            else
            {
                j+=1;
                while(date[j+1]==date[j])j++;
                j++;
                int en=j;
                while(date[j+1]>=date[j])
                    j++;
                if(j!=n-1) flag=1;
                else ans=n-en;
            }
        }
        else if(date[0]==max_)
        {
            int j=1;
            while(date[j++]==max_);
            j--;
            while(date[j+1]>=date[j]&&date[j+1]!=max_)
            {
                 j++;
            }

            if(j==n-1) ans=1;
            else if(j!=n-1&&date[j+1]!=max_)
                    flag=1;
             else if(j!=n-1&&date[j+1]==max_)
                {
                    int en=j;
                     j++;
                    while(date[j+1]==date[j])
                        j++;
                    if(j!=n-1) flag=1;
                    else {
                            ans=n-en;
                        //printf("%d\n",j);
                    }

                }
        }
        if(flag) printf("-1\n");
        else printf("%d\n",ans);
    }
    return 0;
}


#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <queue>
#include <cstring>
#include <climits>
#include <vector>
using namespace std;
int date[100005];
int main()
{
    int n,ans,min_=INT_MAX,max_=INT_MIN;
    while(scanf("%d",&n)==1)
    {
        memset(date,0,sizeof(date));
        ans=0;
        for(int i=0;i<n;i++)
        {
            scanf("%d",&date[i]);
            min_=min(min_,date[i]);
            max_=max(max_,date[i]);
        }
        int flag=0;
        int temp;
        if(date[0]==min_) // ��������������
        {
                int j=0;
                while(date[j+1]>=date[j])
                {
                  j++;
                }
                if(j!=n-1)
                    flag=1;
        }
        else if(date[0]!=max_)
        {
            int start=date[0];
            int j=1;
            while(date[j+1]>=date[j])
            {
                 if(date[j+1]==max_) break;
                 j++;
            }
            if(date[j+1]!=max_) flag=1;
            else
            {
                j+=2;
                int en=j;
                while(date[j+1]>=date[j])
                    j++;
                if(j!=n-1) flag=1;
                else ans=n-en;
            }
        }
        else if(date[0]==max_)
        {
            int j=1;
            while(date[j+1]>=date[j])
            {
                 j++;
            }
            if(j!=n-1) flag=1;
            else ans=n-1;
        }
        if(flag) printf("-1\n");
        else printf("%d\n",ans);
    }
    return 0;
}


#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <queue>
#include <cstring>
#include <climits>
using namespace std;
int date[100005];
int main()
{
    int n,ans,min_=INT_MAX;
    while(scanf("%d",&n)==1)
    {
        memset(date,0,sizeof(date));
        ans=0;
        int start;
        int flag=0;
        for(int i=0;i<n;i++)
        {
            scanf("%d",&date[i]);
            if(min_>date[i])
            {
                min_=date[i];
                start=i;
            }
        }
        int en=0;
        for(int i=start;i<n-1;i++)
            if(date[i+1]<date[i])
            {
                if(date[i+1]!=min_)
                 flag=1;
                else en=i+1;
            }

        if(!flag&&start!=0)
        {
            int en=date[n-1];
            for(int i=0;i<start;i++)
                if(date[i]<en)
                 flag=1;
                else
                    en=date[i];
        }

        if(flag) printf("-1\n");
        else
        {
            if(start==0&&!en)printf("%d\n",0);
            else if(start==0&&en)
            printf("%d\n",n-en);
            else printf("%d\n",n-start);

        }
    }
    return 0;
}


#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <queue>
#include <cstring>
#include <climits>
#include <vector>
using namespace std;
int date[100005];
int main()
{
    int n,ans,min_=INT_MAX,max_=INT_MIN;
    while(scanf("%d",&n)==1)
    {
        memset(date,0,sizeof(date));
        ans=0;
        for(int i=0;i<n;i++)
        {
            scanf("%d",&date[i]);
            min_=min(min_,date[i]);
            max_=max(max_,date[i]);
        }
        int flag=0;
        int temp;
        if(date[0]==min_) // ��������������
        {
                int j=0;
                while(date[j+1]>=date[j])
                {
                  j++;
                }
                if(j!=n-1)
                    flag=1;
        }
        else if(date[0]!=max_)
        {
            int start=date[0];
            int j=1;
            while(date[j+1]>=date[j])
            {
                 if(date[j+1]==max_) break;
                 j++;
            }
            if(date[j+1]!=max_) flag=1;
            else
            {
                j+=1;
                while(date[j+1]==date[j])j++;
                j++;
                int en=j;
                while(date[j+1]>=date[j])
                    j++;
                if(j!=n-1) flag=1;
                else ans=n-en;
            }
        }
        else if(date[0]==max_)
        {
            int j=1;
            while(date[j+1]>=date[j])
            {
                 j++;
            }
            if(j!=n-1) flag=1;
            else ans=n-1;
        }
        if(flag) printf("-1\n");
        else printf("%d\n",ans);
    }
    return 0;
}


#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <queue>
#include <cstring>
#include <climits>
using namespace std;
int date[100005];
int main()
{
    int n,ans,min_=INT_MAX;
    while(scanf("%d",&n)==1)
    {
        memset(date,0,sizeof(date));
        ans=0;
        int start;
        int flag=0;
        for(int i=0;i<n;i++)
        {
            scanf("%d",&date[i]);
            if(min_>date[i])
            {
                min_=date[i];
                start=i;
            }
        }
        int en=0;
        for(int i=start;i<n-1;i++)
            if(date[i+1]<date[i])
            {
                if(date[i+1]!=min_)
                 flag=1;
                else en=i+1;
            }

        if(!flag&&start!=0)
        {
            int en=date[n-1];
            for(int i=0;i<start;i++)
                if(date[i]<en)
                 flag=1;
                else
                    en=date[i];
        }

        if(flag) printf("-1\n");
        else
        {
            if(start==0&&!en)printf("0\n");
            else if(start==0&&en)
            printf("%d\n",n-en);
            else printf("%d\n",n-start);

        }
    }
    return 0;
}


#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <queue>
#include <cstring>
#include <climits>
using namespace std;
int date[100005];
int main()
{
    int n,ans,min_=INT_MAX;
    while(scanf("%d",&n)==1)
    {
        memset(date,0,sizeof(date));
        ans=0;
        int start;
        int flag=0;
        for(int i=0;i<n;i++)
        {
            scanf("%d",&date[i]);
            if(min_>date[i])
            {
                min_=date[i];
                start=i;
            }
        }
        for(int i=start;i<n-1;i++)
            if(date[i+1]<date[i])
               flag=1;
        if(!flag&&start!=0)
        {
            int en=date[n-1];
            for(int i=0;i<start;i++)
                if(date[i]<en)
                 flag=1;
                else
                    en=date[i];
        }

        if(flag) printf("-1\n");
        else
        {
            if(start==0)printf("0\n");
            else
            printf("%d\n",n-start);

        }
    }
    return 0;
}


#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <queue>
#include <cstring>
#include <climits>
#include <vector>
using namespace std;
int date[100005];
int main()
{
    int n,ans,min_=INT_MAX,max_=INT_MIN;
    while(scanf("%d",&n)==1)
    {
        memset(date,0,sizeof(date));
        ans=0;
        for(int i=0;i<n;i++)
        {
            scanf("%d",&date[i]);
            min_=min(min_,date[i]);
            max_=max(max_,date[i]);
        }
        int flag=0;
        int temp;
        if(date[0]==min_) // ��������������
        {
                int j=0;
                while(date[j+1]>=date[j])
                {
                  j++;
                }
                if(j!=n-1&&date[j+1]!=min_)
                    flag=1;
                else if(j!=n-1&&date[j+1]==min_)
                {

                    j++;
                     int en=j;
                    while(date[j+1]==date[j])
                        j++;
                    if(j!=n-1) flag=1;
                    else {
                            ans=n-en;
                        //printf("%d\n",j);
                    }

                }
        }
        else if(date[0]!=max_)
        {
            int start=date[0];
            int j=1;
            while(date[j+1]>=date[j])
            {
                 if(date[j+1]==max_) break;
                 j++;
            }
            if(date[j+1]!=max_) flag=1;
            else
            {
                j+=1;
                while(date[j+1]==date[j])j++;
                j++;
                int en=j;
                while(date[j+1]>=date[j])
                    j++;
                if(j!=n-1) flag=1;
                else ans=n-en;
            }
        }
        else if(date[0]==max_)
        {
            int j=1;
            while(date[j+1]>=date[j])
            {
                 j++;
            }
            if(j!=n-1) flag=1;
            else ans=n-1;
        }
        if(flag) printf("-1\n");
        else printf("%d\n",ans);
    }
    return 0;
}


#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <queue>
#include <cstring>
#include <climits>
#include <vector>
using namespace std;
int date[100005];
int main()
{
    int n,ans,min_=INT_MAX,max_=INT_MIN;
    while(scanf("%d",&n)==1)
    {
        memset(date,0,sizeof(date));
        ans=0;
        for(int i=0;i<n;i++)
        {
            scanf("%d",&date[i]);
            min_=min(min_,date[i]);
            max_=max(max_,date[i]);
        }
        int flag=0;
        int temp;
        if(date[0]==min_) // ��������������
        {
                int j=0;
                while(date[j+1]>=date[j])
                {
                  j++;
                }
                if(j!=n-1&&date[j+1]!=min_)
                    flag=1;
                else if(j!=n-1&&date[j+1]==min_)
                {

                    j++;
                     int en=j;
                    while(date[j+1]==date[j])
                        j++;
                    if(j!=n-1) flag=1;
                    else {
                            ans=n-en;
                        //printf("%d\n",j);
                    }

                }
        }
        else if(date[0]!=max_)
        {
            int start=date[0];
            int j=1;
            while(date[j+1]>=date[j])
            {
                 if(date[j+1]==max_) break;
                 j++;
            }
            if(date[j+1]!=max_) flag=1;
            else
            {
                j+=1;
                while(date[j+1]==date[j])j++;
                j++;
                int en=j;
                while(date[j+1]>=date[j])
                    j++;
                if(j!=n-1) flag=1;
                else ans=n-en;
            }
        }
        else if(date[0]==max_)
        {
            int j=1;
            while(date[j++]==max_);
            j--;
            while(date[j+1]>=date[j]&&date[j+1]!=max_)
            {
                 j++;
            }
            if(j!=n-1&&date[j+1]!=max_)
                    flag=1;
             else if(j!=n-1&&date[j+1]==max_)
                {
                    int en=j;
                     j++;
                    while(date[j+1]==date[j])
                        j++;
                    if(j!=n-1) flag=1;
                    else {
                            ans=n-en;
                        //printf("%d\n",j);
                    }

                }
        }
        if(flag) printf("-1\n");
        else printf("%d\n",ans);
    }
    return 0;
}


#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <queue>
#include <cstring>
#include <climits>
using namespace std;
int date[100005];
int main()
{
    int n,ans,min_=INT_MAX;
    while(scanf("%d",&n)==1)
    {
        memset(date,0,sizeof(date));
        ans=0;
        int start;
        int flag=0;
        for(int i=0;i<n;i++)
        {
            scanf("%d",&date[i]);
            if(min_>date[i])
            {
                min_=date[i];
                start=i;
            }
        }
        int en=0;
        for(int i=start;i<n-1;i++)
            if(date[i+1]<date[i])
            {
                if(date[i+1]==min_&&start==0)
                en=i+1;
                else flag=1;
            }

        if(!flag&&start!=0)
        {
            int en=date[n-1];
            for(int i=0;i<start;i++)
                if(date[i]<en)
                 flag=1;
                else
                    en=date[i];
        }

        if(flag) printf("-1\n");
        else
        {
            if(start==0&&!en)printf("%d\n",0);
            else if(start==0&&en)
            printf("%d\n",n-en);
            else printf("%d\n",n-start);

        }
    }
    return 0;
}


#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <queue>
#include <cstring>
#include <climits>
using namespace std;
int date[100005];
int main()
{
    int n,ans,min_=INT_MAX,max_=INT_MIN;
    while(scanf("%d",&n)==1)
    {
        memset(date,0,sizeof(date));
        ans=0;
        for(int i=0;i<n;i++)
        {
            scanf("%d",&date[i]);
            min_=min(min_,date[i]);
            max_=max(max_,date[i]);
        }
        int flag=0;
        int temp;
        if(date[0]==min_) // ��������������
        {
                int j=0;
                while(date[j+1]>=date[j])
                {
                  j++;
                }
                if(j!=n-1&&date[j+1]!=min_)
                    flag=1;
                else if(j!=n-1&&date[j+1]==min_)
                {

                    j++;
                     int en=j;
                    while(date[j+1]==date[j])
                        j++;
                    if(j!=n-1) flag=1;
                    else {
                            ans=n-en;
                        //printf("%d\n",j);
                    }

                }
        }
        else if(date[0]!=max_)
        {
            int start=date[0];
            int j=1;
            while(date[j+1]>=date[j])
            {
                 if(date[j+1]==max_) break;
                 j++;
            }
            if(date[j+1]!=max_) flag=1;
            else
            {
                j+=1;
                while(date[j+1]==date[j])j++;
                j++;
                int en=j;
                while(date[j+1]>=date[j])
                    j++;
                if(j!=n-1) flag=1;
                else ans=n-en;
            }
        }
        else if(date[0]==max_)
        {
            int j=1;
            while(date[j++]==max_);
            j--;
            int en=j;
            while(date[j+1]>=date[j])
            {
                 j++;
            }
            if(j!=n-1) flag=1;
            else ans=n-en;
        }
        if(flag) printf("-1\n");
        else printf("%d\n",ans);
    }
    return 0;
}


#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <queue>
#include <cstring>
#include <climits>
using namespace std;
int date[100005];
int main()
{
    int n,ans,min_=INT_MAX;
    while(scanf("%d",&n)==1)
    {
        memset(date,0,sizeof(date));
        ans=0;
        int start;
        int flag=0;
        for(int i=0;i<n;i++)
        {
            scanf("%d",&date[i]);
            if(min_>date[i])
            {
                min_=date[i];
                start=i;
            }
        }
        int en=0;
        int flag1=0;
        for(int i=start;i<n-1;i++)
            if(date[i+1]<date[i])
            {
                flag1=1;
                if(date[i+1]==min_&&start==0)
                  en=i+1;
                else flag=1;
            }
            else if(date[i+1]>date[i]&&flag1==1)
                flag=1;

        if(!flag&&start!=0)
        {
            int en=date[n-1];
            for(int i=0;i<start;i++)
                if(date[i]<en)
                 flag=1;
                else
                    en=date[i];
        }

        if(flag) printf("-1\n");
        else
        {
            if(start==0&&!en)printf("%d\n",0);
            else if(start==0&&en)
            printf("%d\n",n-en);
            else printf("%d\n",n-start);

        }
    }
    return 0;
}


#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <queue>
#include <cstring>
#include <climits>
using namespace std;
int date[100005];
int main()
{
    int n,ans,min_=INT_MAX,max_=INT_MIN;
    while(scanf("%d",&n)==1)
    {
        memset(date,0,sizeof(date));
        ans=0;
        for(int i=0;i<n;i++)
        {
            scanf("%d",&date[i]);
            min_=min(min_,date[i]);
            max_=max(max_,date[i]);
        }
        int flag=0;
        int temp;
        if(date[0]==min_) // ��������������
        {
                int j=0;
                while(date[j+1]>=date[j])
                {
                  j++;
                }
                if(j!=n-1&&date[j+1]!=min_)
                    flag=1;
                else if(j!=n-1&&date[j+1]==min_)
                {

                    j++;
                     int en=j;
                    while(date[j+1]==date[j])
                        j++;
                    if(j!=n-1) flag=1;
                    else {
                            ans=n-en;
                        //printf("%d\n",j);
                    }

                }
        }
        else if(date[0]!=max_)
        {
            int start=date[0];
            int j=1;
            while(date[j+1]>=date[j])
            {
                 if(date[j+1]==max_) break;
                 j++;
            }
            if(date[j+1]!=max_) flag=1;
            else
            {
                j+=1;
                while(date[j+1]==date[j])j++;
                j++;
                int en=j;
                while(date[j+1]>=date[j])
                    j++;
                if(j!=n-1) flag=1;
                else ans=n-en;
            }
        }
        else if(date[0]==max_)
        {
            int j=1;
            while(date[j++]==max_);
            j--;
            while(date[j+1]>=date[j]&&date[j+1]!=max_)
            {
                 j++;
            }
            if(j==n-1) ans=1;
            else if(j!=n-1&&date[j+1]!=max_)
                    flag=1;
             else if(j!=n-1&&date[j+1]==max_)
                {
                    int en=j;
                     j++;
                    while(date[j+1]==date[j])
                        j++;
                    if(j!=n-1) flag=1;
                    else {
                            ans=n-en;
                        //printf("%d\n",j);
                    }

                }
        }
        if(flag) printf("-1\n");
        else printf("%d\n",ans);
    }
    return 0;
}


#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <queue>
#include <cstring>
#include <climits>
#include <vector>
using namespace std;
int date[100005];
int main()
{
    int n,ans,min_=INT_MAX,max_=INT_MIN;
    while(scanf("%d",&n)==1)
    {
        memset(date,0,sizeof(date));
        ans=0;
        for(int i=0;i<n;i++)
        {
            scanf("%d",&date[i]);
            min_=min(min_,date[i]);
            max_=max(max_,date[i]);
        }
        int flag=0;
        int temp;
        if(date[0]==min_) // ��������������
        {
                int j=0;
                while(date[j+1]>=date[j])
                {
                  j++;
                }
                if(j!=n-1&&date[j+1]!=min_)
                    flag=1;
                else if(j!=n-1&&date[j+1]==min_)
                {
                    j++;
                    int en=j;
                    while(date[j+1]==date[j])
                        j++;
                    if(j!=n-1) flag=1;
                    else ans=n-j;
                }
        }
        else if(date[0]!=max_)
        {
            int start=date[0];
            int j=1;
            while(date[j+1]>=date[j])
            {
                 if(date[j+1]==max_) break;
                 j++;
            }
            if(date[j+1]!=max_) flag=1;
            else
            {
                j+=1;
                while(date[j+1]==date[j])j++;
                j++;
                int en=j;
                while(date[j+1]>=date[j])
                    j++;
                if(j!=n-1) flag=1;
                else ans=n-en;
            }
        }
        else if(date[0]==max_)
        {
            int j=1;
            while(date[j+1]>=date[j])
            {
                 j++;
            }
            if(j!=n-1) flag=1;
            else ans=n-1;
        }
        if(flag) printf("-1\n");
        else printf("%d\n",ans);
    }
    return 0;
}


#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cstring>
#include <climits>
using namespace std;
int main()
{
    int  cnt1,cnt2,x,y;
    scanf("%d%d%d%d",&cnt1,&cnt2,&x,&y);
    int l=0,r=2147483647;
    while(l<r)
        {
            int temp=(long long)l+r>>1;
            if(temp-temp/x/y>=(cnt1+cnt2)&&temp-temp/x>=cnt1&&temp-temp/y>=cnt2)
                r=temp;
            else l=temp+1;
        }
        printf("%d\n",l);
        return 0;
}
/*#include<cstdio>
using namespace std;
int main()
{
	int a,b,c,d,l=0,r=2147483647,o;
	scanf("%d%d%d%d",&a,&b,&c,&d);
	while(l<r)
	{
		o=(long long)l+r>>1;
		if(o-o/c/d>=a+b&&o-o/c>=a&&o-o/d>=b)
			r=o;
		else l=o+1;
	}
	printf("%d\n",l);
}
*/


#include <bits/stdc++.h>
using namespace std;

int a[100005];

int main()
{
    int n;
    cin >> n;
    int mn=1e9, mx=0;
    vector<int>mins, maxs;
    for(int i=0; i<n; ++i){
        cin >> a[i];
        if(a[i] < mn){
            mins.clear();
            mins.push_back(i);
            mn = a[i];
        }
        if(a[i] > mx){
            maxs.clear();
            maxs.push_back(i);
            mx = a[i];
        }
        if(a[i] == mn) mins.push_back(i);
        if(a[i] == mx) maxs.push_back(i);
    }
    sort(mins.begin(), mins.end());
    sort(maxs.begin(), maxs.end());
    for(int i=0; i<mins.size(); ++i){
        if(find(maxs.begin(),maxs.end(),(mins[i]-1+n)%n) != maxs.end()){
            int cnt = 1; bool works = true; int i=(mins[i])%n;
            while(cnt<n){
                if(a[i] > a[(i+1)%n]) works = false;
                cnt++;
                i = (i+1)%n;
            }
            if(works){
                /*if(mins[i] == 0){
                    cout << 0 << endl;
                    return 0;
                }*/
                cout << (n-mins[i])%n << endl;
                return 0;
            } else {
                cout << -1 << endl;
                return 0;
            }
        }
    }
    cout << -1 << endl;
}


#include <iostream>
using namespace std;

int a[100005];

int main()
{
    int n;
    cin >> n;
    int mi=0;
    for(int i=0; i<n; ++i){
        cin >> a[i];
        if(a[mi] <= a[i]) mi = i;
    }
    bool increas = true;
    int cnt = 1, i = (mi+1)%n;
    while(cnt < n){
        if(a[i] > a[(i+1)%n]) increas =false;
        i = (i+1)%n;
        ++cnt;
    }
    if(increas){
        (mi+1)%n != 0 ? cout << n-(mi+1)%n << endl : cout << 0 << endl;
        //cout << n << " " << mi << endl;
    } else {
        cout << -1 << endl;
    }
}


#include <iostream>
using namespace std;

int a[100005];

int main()
{
    int n;
    cin >> n;
    int mi=0;
    for(int i=0; i<n; ++i){
        cin >> a[i];
        if(a[mi] >= a[i]) mi = i;
    }
    bool increas = true;
    int cnt = 1, i = mi;
    while(cnt < n){
        if(a[i] > a[(i+1)%n]) increas =false;
        i = (i+1)%n;
        ++cnt;
    }
    if(increas){
        mi != 0 ? cout << n-mi << endl : cout << 0 << endl;
        //cout << n << " " << mi << endl;
    } else {
        cout << -1 << endl;
    }
}


#include <bits/stdc++.h>
using namespace std;

int a[100005];

int main()
{
    int n;
    cin >> n;
    int mn=1e9, mx=0;
    vector<int>mins, maxs;
    for(int i=0; i<n; ++i){
        cin >> a[i];
        bool changed = false;
        if(a[i] < mn){
            mins.clear();
            mins.push_back(i);
            mn = a[i];
            changed = true;
        }
        if(a[i] > mx){
            maxs.clear();
            maxs.push_back(i);
            mx = a[i];
            changed = true;
        }
        if(changed) continue;
        if(a[i] == mn) mins.push_back(i);
        if(a[i] == mx) maxs.push_back(i);
    }
    sort(mins.begin(), mins.end());
    sort(maxs.begin(), maxs.end());
    /*for(int i=0; i<mins.size(); ++i)
        cout << mins[i] << " ";
    cout << endl;
    for(int i=0; i<maxs.size(); ++i)
        cout << maxs[i] << " ";
    cout << endl;*/
    for(int i=0; i<mins.size(); ++i){
        if(find(maxs.begin(),maxs.end(),(mins[i]-1+n)%n) != maxs.end()){
            int cnt = 1; bool works = true; int j=mins[i];
            while(cnt<n){
                if(a[j] > a[(j+1)%n]) works = false;
                cnt++;
                j = (j+1)%n;
            }
            if(works){
                //cout << mins[i] << endl;
                cout << (n-mins[i])%n << endl;
                return 0;
            } else {
                cout << -1 << endl;
                return 0;
            }
        }
    }
    cout << -1 << endl;
}


#include <iostream>
using namespace std;

int a[100005];

int main()
{
    int n;
    cin >> n;
    int mi=0;
    for(int i=0; i<n; ++i){
        cin >> a[i];
        if(a[mi] > a[i]) mi = i;
    }
    bool increas = true;
    int cnt = 1, i = mi;
    while(cnt < n){
        if(a[i] > a[(i+1)%n]) increas =false;
        i = (i+1)%n;
        ++cnt;
    }
    if(increas){
        mi != 0 ? cout << n-mi << endl : cout << 0 << endl;
        //cout << n << " " << mi << endl;
    } else {
        cout << -1 << endl;
    }
}


#include <iostream>
using namespace std;

int a[100005];

int main()
{
    int n;
    cin >> n;
    int mi=0;
    for(int i=0; i<n; ++i){
        cin >> a[i];
        if(a[mi] > a[i]) mi = i;
    }
    bool increas = true;
    int cnt = 1, i = mi;
    while(cnt < n){
        if(a[i] >= a[(i+1)%n]) increas =false;
        i = (i+1)%n;
        ++cnt;
    }
    if(increas){
        cout << n-mi << endl;
        //cout << n << " " << mi << endl;
    } else {
        cout << -1 << endl;
    }
}


#include <iostream>
using namespace std;

int a[100005];

int main()
{
    int n;
    cin >> n;
    int mi=0;
    for(int i=0; i<n; ++i){
        cin >> a[i];
        if(a[mi] < a[i]) mi = i;
    }
    bool increas = true;
    int cnt = 1, i = (mi+1)%n;
    while(cnt < n){
        if(a[i] > a[(i+1)%n]) increas =false;
        i = (i+1)%n;
        ++cnt;
    }
    if(increas){
        (mi+1)%n != 0 ? cout << n-(mi+1)%n << endl : cout << 0 << endl;
        //cout << n << " " << mi << endl;
    } else {
        cout << -1 << endl;
    }
}


#include <iostream>
using namespace std;

int a[100005];

int main()
{
    int n;
    cin >> n;
    int mi=0;
    for(int i=0; i<n; ++i){
        cin >> a[i];
        if(a[mi] > a[i]) mi = i;
    }
    bool increas = true;
    int cnt = 1, i = mi;
    while(cnt < n){
        if(a[i] >= a[(i+1)%n]) increas =false;
        i = (i+1)%n;
        ++cnt;
    }
    if(increas){
        mi != 0 ? cout << n-mi << endl : cout << 0 << endl;
        //cout << n << " " << mi << endl;
    } else {
        cout << -1 << endl;
    }
}


#include <iostream>
using namespace std;

int a[100005];

int main()
{
    int n;
    cin >> n;
    int mi=0;
    for(int i=1; i<n; ++i){
        cin >> a[i];
        if(a[mi] > a[i]) mi = i;
    }
    bool increas = true;
    int cnt = 1, i = mi;
    while(cnt < n){
        if(a[i] >= a[(i+1)%n]) increas =false;
        i = (i+1)%n;
        ++cnt;
    }
    if(increas){
        cout << n-mi << endl;
    } else {
        cout << -1 << endl;
    }
}


#include <bits/stdc++.h>
using namespace std;

int a[100005];

int main()
{
    int n;
    cin >> n;
    int mn=1e9, mx=0;
    vector<int>mins, maxs;
    for(int i=0; i<n; ++i){
        cin >> a[i];
        if(a[i] < mn){
            mins.clear();
            mins.push_back(i);
            mn = a[i];
            continue;
        }
        if(a[i] > mx){
            maxs.clear();
            maxs.push_back(i);
            mx = a[i];
            continue;
        }
        if(a[i] == mn) mins.push_back(i);
        if(a[i] == mx) maxs.push_back(i);
    }
    sort(mins.begin(), mins.end());
    sort(maxs.begin(), maxs.end());
    /*for(int i=0; i<mins.size(); ++i)
        cout << mins[i] << " ";
    cout << endl;
    for(int i=0; i<maxs.size(); ++i)
        cout << maxs[i] << " ";
    cout << endl;*/
    for(int i=0; i<mins.size(); ++i){
        if(find(maxs.begin(),maxs.end(),(mins[i]-1+n)%n) != maxs.end()){
            int cnt = 1; bool works = true; int j=mins[i];
            while(cnt<n){
                if(a[j] > a[(j+1)%n]) works = false;
                cnt++;
                j = (j+1)%n;
            }
            if(works){
                //cout << mins[i] << endl;
                cout << (n-mins[i])%n << endl;
                return 0;
            } else {
                cout << -1 << endl;
                return 0;
            }
        }
    }
    cout << -1 << endl;
}


#include <iostream>
using namespace std;

int a[100005];

int main()
{
    int n;
    cin >> n;
    int mi=0;
    for(int i=0; i<n; ++i){
        cin >> a[i];
        if(a[mi] >= a[i]) mi = i;
    }
    bool increas = true;
    int cnt = 1, i = mi;
    while(cnt < n){
        if(a[i] > a[(i+1)%n]) increas =false;
        i = (i+1)%n;
        ++cnt;
    }
    if(increas){
        mi != 0 && a[mi] != a[0] ? cout << n-mi << endl : cout << 0 << endl;
        //cout << n << " " << mi << endl;
    } else {
        cout << -1 << endl;
    }
}


#include <iostream>
#include <vector>
using namespace std;
vector <int > v;
int main() {
	// your code goes here
	int bg = -1 , ind, ans = 0 , n ;
	cin >> n;
	int ar[n];
	for(int i = 0 ; i < n ; i++){
		cin >> ar[i];
		if(ar[i] > bg){
			bg = ar[i];
			ind = i;
		}
	}

	ans += (n - (ind +1));
	for( int i = ind + 1 ; i < n ; i++){
		v.push_back(ar[i]);
	}
	for(int i = 0 ; i <= ind ; i++){
		v.push_back(ar[i]);
	}
	int sum = 0;
	for(int i = 0 ; i < n-1 && v[i] < v[i+1] ; i++){
		sum++;
	}
	if(sum == n - 1){
		cout << ans;
	}else{
		cout << "-1";
	}
	return 0;
}


#include <iostream>
#include <vector>
using namespace std;
vector <int > v;
int main() {
	// your code goes here
	int bg = -1 , ind, ans = 0 , n ;
	cin >> n;
	int ar[n];
	for(int i = 0 ; i < n ; i++){
		cin >> ar[i];
		if(ar[i] >= bg){
			bg = ar[i];
			ind = i;
		}
	}
	ans += (n - (ind +1));
	for( int i = ind + 1 ; i < n ; i++){
		v.push_back(ar[i]);
	}
	for(int i = 0 ; i <= ind ; i++){
		v.push_back(ar[i]);
	}
	int sum = 0;
	for(int i = 0 ; i < n-1 && v[i] <= v[i+1] ; i++){
		sum++;
	}
	if(sum == n - 1){
		cout << ans;
	}else{
		cout << "-1";
	}
	return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
vector <long long > v;
int main() {
	// your code goes here
	long long bg = -1 , ind = 0, ans = 0 , n ;
	cin >> n;
	long long ar[n] , ar1[n];
	for(int i = 0 ; i < n ; i++){
		cin >> ar[i];
		ar1[i] = ar[i];
		if(ar[i] >= bg){
			bg = ar[i];
			ind = i;
		}
	}
	sort(ar1 , ar1+n);
	int sum1 = 0;
	for( int i = 0 ; i < n ; i++){
		if(i + 1 == n && ar[i] == ar1[i] && sum1 == i){
			cout << "0";
			return 0;
		}
		if( ar1[i] == ar[i]){
			sum1++;
		}

	}
	ans += (n - (ind +1));
	for( int i = ind + 1 ; i < n ; i++){
		v.push_back(ar[i]);
	}
	for(int i = 0 ; i <= ind ; i++){
		v.push_back(ar[i]);
	}
	int sum = 0;
	for(int i = 0 ; i < n-1 && v[i] <= v[i+1] ; i++){
		sum++;
	}
	if(sum == n - 1){
		cout << ans;
	}else{
		cout << "-1";
	}
	return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
vector <long long > v;
int main() {
	// your code goes here
	long long bg = -1 , ind = 0, ans = 0 , n ;
	cin >> n;
	long long ar[n] , ar1[n];
	for(int i = 0 ; i < n ; i++){
		cin >> ar[i];
		ar1[i] = ar[i];

	}
	for(int i = 0 ; i < n -1 ; i++){
		if(ar[i] > ar[i+1]){
			ind = i;
			break;
		}
	}
	sort(ar1 , ar1+n);
	int sum1 = 0;
	for( int i = 0 ; i < n ; i++){
		if(i + 1 == n && ar[i] == ar1[i] && sum1 == i){
			cout << "0";
			return 0;
		}
		if( ar1[i] == ar[i]){
			sum1++;
		}

	}
	ans += (n - (ind +1));
	for( int i = ind + 1 ; i < n ; i++){
		v.push_back(ar[i]);
	}
	for(int i = 0 ; i <= ind ; i++){
		v.push_back(ar[i]);
	}
	int sum = 0;
	for(int i = 0 ; i < n-1 && v[i] <= v[i+1] ; i++){
		sum++;
	}
	if(sum == n - 1){
		cout << ans;
	}else{
		cout << "-1";
	}
	return 0;
}


#include <iostream>
#include <vector>
using namespace std;
vector <int > v;
int main() {
	// your code goes here
	int bg , ind, ans = 0 , n ;
	cin >> n;
	long ar[n];
    cin >> ar[0];
    bg = ar[0];
    ind = 0;
	for(int i = 1 ; i < n ; i++){
		cin >> ar[i];
		if(ar[i] >= bg){
			bg = ar[i];
			ind = i;
		}
	}

	ans += (n - (ind +1));
	for( int i = ind + 1 ; i < n ; i++){
		v.push_back(ar[i]);
	}
	for(int i = 0 ; i <= ind ; i++){
		v.push_back(ar[i]);
	}
	int sum = 0;
	for(int i = 0 ; i < n-1 && v[i] <= v[i+1] ; i++){
		sum++;
	}
	if(sum == n - 1){
		cout << ans;
	}else{
		cout << "-1";
	}
	return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
vector <long long > v;
int main() {
	// your code goes here
	long long bg = -1 , ind = 0, ans = 0 , n ;
	cin >> n;
	long long ar[n] , ar1[n];
	for(int i = 0 ; i < n ; i++){
		cin >> ar[i];
		ar1[i] = ar[i];
		if(ar[i] > bg){
			bg = ar[i];
			ind = i;
		}
	}
	sort(ar1 , ar1+n);
	int sum1 = 0;
	for( int i = 0 ; i < n ; i++){
		if(i + 1 == n && ar[i] == ar1[i] && sum1 == i){
			cout << "0";
			return 0;
		}
		if( ar1[i] == ar[i]){
			sum1++;
		}

	}
	ans += (n - (ind +1));
	for( int i = ind + 1 ; i < n ; i++){
		v.push_back(ar[i]);
	}
	for(int i = 0 ; i <= ind ; i++){
		v.push_back(ar[i]);
	}
	int sum = 0;
	for(int i = 0 ; i < n-1 && v[i] <= v[i+1] ; i++){
		sum++;
	}
	if(sum == n - 1){
		cout << ans;
	}else{
		cout << "-1";
	}
	return 0;
}


#include <iostream>
#include <vector>
using namespace std;
vector <long long > v;
int main() {
	// your code goes here
	long long bg = -1 , ind = 0, ans = 0 , n ;
	cin >> n;
	long long ar[n];
	for(int i = 0 ; i < n ; i++){
		cin >> ar[i];
		if(ar[i] >= bg){
			bg = ar[i];
			ind = i;
		}
	}
	ans += (n - (ind +1));
	for( int i = ind + 1 ; i < n ; i++){
		v.push_back(ar[i]);
	}
	for(int i = 0 ; i <= ind ; i++){
		v.push_back(ar[i]);
	}
	int sum = 0;
	for(int i = 0 ; i < n-1 && v[i] <= v[i+1] ; i++){
		sum++;
	}
	if(sum == n - 1){
		cout << ans;
	}else{
		cout << "-1";
	}
	return 0;
}


#include <iostream>
#include <vector>
using namespace std;
vector <int > v;
int main() {
	// your code goes here
	int bg = -1 , ind, ans = 0 , n ;
	cin >> n;
	int ar[n];
	for(int i = 0 ; i < n ; i++){
		cin >> ar[i];
		if(ar[i] >= bg){
			bg = ar[i];
			ind = i;
		}
	}

	ans += (n - (ind +1));
	for( int i = ind + 1 ; i < n ; i++){
		v.push_back(ar[i]);
	}
	for(int i = 0 ; i <= ind ; i++){
		v.push_back(ar[i]);
	}
	int sum = 0;
	for(int i = 0 ; i < n-1 && v[i] <= v[i+1] ; i++){
		sum++;
	}
	if(sum == n - 1){
		cout << ans;
	}else{
		cout << "-1";
	}
	return 0;
}


#include <iostream>
#include <vector>
using namespace std;
vector <int > v;
int main() {
	// your code goes here
	int bg = -1 , ind, ans = 0 , n ;
	cin >> n;
	int ar[n];
	for(int i = 0 ; i < n ; i++){
		cin >> ar[i];
		if(ar[i] > bg){
			bg = ar[i];
			ind = i;
		}
	}

	ans += (n - (ind +1));
	for( int i = ind + 1 ; i < n ; i++){
		v.push_back(ar[i]);
	}
	for(int i = 0 ; i <= ind ; i++){
		v.push_back(ar[i]);
	}
	int sum = 0;
	for(int i = 0 ; i < n-1 && v[i] <= v[i+1] ; i++){
		sum++;
	}
	if(sum == n - 1){
		cout << ans;
	}else{
		cout << "-1";
	}
	return 0;
}


#include <iostream>
#include <vector>
using namespace std;
vector <int > v;
int main() {
	// your code goes here
	int bg , ind, ans = 0 , n ;
	cin >> n;
	int ar[n];
	for(int i = 0 ; i < n ; i++){
		cin >> ar[i];
		if(ar[i] >= bg){
			bg = ar[i];
			ind = i;
		}
	}

	ans += (n - (ind +1));
	for( int i = ind + 1 ; i < n ; i++){
		v.push_back(ar[i]);
	}
	for(int i = 0 ; i <= ind ; i++){
		v.push_back(ar[i]);
	}
	int sum = 0;
	for(int i = 0 ; i < n-1 && v[i] <= v[i+1] ; i++){
		sum++;
	}
	if(sum == n - 1){
		cout << ans;
	}else{
		cout << "-1";
	}
	return 0;
}


#include<iostream>
#include<deque>
#include<cstdio>
#include<algorithm>
using namespace std;
int main(){
	int te,ans=0;
	scanf("%d",&te);
	deque<int>d;
	int arr[100001];
	for(int i=0;i<te;i++){
	scanf("%d",&arr[i]);
	d.push_back(arr[i]);
	}
	sort(arr+0,arr+te);
	for(int i=0;i<te-1;i++){
		if(d.back()<d.front()){
			ans++;
			d.push_front(d.back());
			d.pop_back();
		}
	}
	for(int i=0;i<te;i++){
		if(d.front()!=arr[i]){
			printf("-1\n");
			return 0;
		}
		d.pop_front();
	}
	printf("%d\n",ans);
	return 0;
}


#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
int main(){
	int n,i=0,ans=0;
	int v[100001],v1[100001];
	scanf("%d",&n);
	for(;i<n;i++){
	scanf("%d",&v[i]);
	}
	i=0;
	while(v[0]>v[n-1]&&ans<n-1){
	swap(v[0],v[n-1]);
	ans++;
	}
	for(;i<n;i++)
	v1[i]=v[i];
	sort(v1+0,v1+n);
	i=0;
		for(;i<n;i++){
		if(v[i]!=v1[i]){
			printf("-1\n");
			return 0;
		}
		}
		printf("%d\n",ans);
//system("pause");
return 0;
}


#include<iostream>
#include<deque>
#include<cstdio>
#include<algorithm>
using namespace std;
int main(){
	int te,ans=0,eq=0;
	scanf("%d",&te);
	deque<int>d;
	int arr[100001];
	for(int i=0;i<te;i++){
	scanf("%d",&arr[i]);
	d.push_back(arr[i]);
	}
	sort(arr+0,arr+te);
	for(int i=0;i<te;i++){
		if(d.back()<=d.front()){
			ans++;
			d.push_front(d.back());
			d.pop_back();
		}
	}
	for(int i=0;i<te;i++){
		if(d.front()!=arr[i]){
			printf("-1\n");
			return 0;
		}else if(arr[i]==arr[i+1]&&i<te-1)
			eq++;
		d.pop_front();
	}
	if(eq==ans)
		ans=0;
	printf("%d\n",ans);
	return 0;
}


#include<iostream>
#include<deque>
#include<cstdio>
#include<algorithm>
using namespace std;
int main(){
	int te,ans=0;
	scanf("%d",&te);
	deque<int>d;
	int arr[100001];
	for(int i=0;i<te;i++){
	scanf("%d",&arr[i]);
	d.push_back(arr[i]);
	}
	sort(arr+0,arr+te);
	for(int i=0;i<(te/2)+1;i++){
		if(d.back()<d.front()){
			ans++;
			d.push_front(d.back());
			d.pop_back();
		}
	}
	for(int i=0;i<te;i++){
		if(d.front()!=arr[i]){
			printf("-1\n");
			return 0;
		}
		d.pop_front();
	}
	printf("%d\n",ans);
	return 0;
}


#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
int main(){
	int n,i=0,ans=0;
	int v[100001],v1[100001];
	scanf_s("%d",&n);
	for(;i<n;i++){
	scanf_s("%d",&v[i]);
	}
	i=0;
	while(v[0]>v[n-1]&&ans<n-1){
	swap(v[0],v[n-1]);
	ans++;
	}
	for(;i<n;i++)
	v1[i]=v[i];
	sort(v1+0,v1+n);
	i=0;
		for(;i<n;i++){
		if(v[i]!=v1[i]){
			printf("-1\n");
			return 0;
		}
		}
		printf("%d\n",ans);
return 0;
}


#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
int main(){
	int n,i=0,idx=0,num,idx1=0;
	int v[100001],v1[100001],tmp[100001];
	scanf("%d",&n);
	num=n-1;
	for(;i<n;i++){
	scanf("%d",&v[i]);
	}
	i=0;
	tmp[0]=v[0];
	while(tmp[idx]>v[num]&&idx<n-1){
		i++;
		tmp[idx]=v[num];
		num--;
	idx++;
	}
	i=0;
	for(;i<n;i++)
	v1[i]=v[i];
	sort(v1+0,v1+n);
	       i=0;
		   if(idx==0){
		for(;i<n;i++)
		if(v[i]!=v1[i]){
			printf("-1\n");
			return 0;
		}
		   }else{
		   for(i=idx-1;i>0;i--,idx1++)
			   if(tmp[i]!=v1[i-idx1]){
			printf("-1\n");
			return 0;
		}
			   for(i=0;i<n-idx;i++)
				   if(v[i]!=v1[idx+i]){
			printf("-1\n");
			return 0;
		}
		   }
		printf("%d\n",idx);
//system("pause");
return 0;
}


#include<iostream>
#include<deque>
#include<cstdio>
#include<algorithm>
using namespace std;
int main(){
	int te,ans=0,eq=0;
	scanf("%d",&te);
	deque<int>d;
	int arr[100001];
	for(int i=0;i<te;i++){
	scanf("%d",&arr[i]);
	d.push_back(arr[i]);
	}
	sort(arr+0,arr+te);
	for(int i=0;i<te-1;i++){
		if(d.back()<=d.front()){
			ans++;
			d.push_front(d.back());
			d.pop_back();
		}
	}
	for(int i=0;i<te;i++){
		if(d.front()!=arr[i]){
			printf("-1\n");
			return 0;
		}else if(arr[i]==arr[i+1]&&i<te-1)
			eq++;
		d.pop_front();
	}
	if(eq==te-1)
		ans=0;
	printf("%d\n",ans);
	return 0;
}


#include<iostream>
#include<deque>
#include<cstdio>
#include<algorithm>
using namespace std;
int main(){
	int te,ans=0,eq=0;
	scanf("%d",&te);
	deque<int>d;
	int arr[100001];
	for(int i=0;i<te;i++){
	scanf("%d",&arr[i]);
	d.push_back(arr[i]);
	}
	sort(arr+0,arr+te);
	for(int i=0;i<te-1;i++){
		if(d.back()<=d.front()){
			ans++;
			d.push_front(d.back());
			d.pop_back();
		}
	}
	for(int i=0;i<te;i++){
		if(d.front()!=arr[i]){
			printf("-1\n");
			return 0;
		}else if(arr[i]==arr[i+1]&&i<te-1)
			eq++;
		d.pop_front();
	}
	if(eq==ans)
		ans=0;
	printf("%d\n",ans);
	return 0;
}


#include<iostream>
#include<deque>
#include<cstdio>
#include<algorithm>
using namespace std;
int main(){
	int te,ans=0;
	scanf("%d",&te);
	deque<int>d;
	int arr[100001];
	for(int i=0;i<te;i++){
	scanf("%d",&arr[i]);
	d.push_back(arr[i]);
	}
	sort(arr+0,arr+te);
	for(int i=0;i<te;i++){
		if(d.back()<=d.front()){
			ans++;
			d.push_front(d.back());
			d.pop_back();
		}
	}
	for(int i=0;i<te;i++){
		if(d.front()!=arr[i]){
			printf("-1\n");
			return 0;
		}
		d.pop_front();
	}
	printf("%d\n",ans);
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define NEWLINE '\n';
#define pi 3.14159265;
#define LL long long
#define MOD	1000000007
int arr[100000];
int main() {
	int n , c= 0;
	scanf ("%d",&n);
	cin >> arr[0];
	for (int i = 1 ; i < n ; ++i){
		cin >> arr[i];
		if (arr[i]<arr[i-1] && c == 0 )c = i;
	}
	for (int i = c ; i < n ; ++i){
		if ( (arr [ i ] > arr[ i + 1] && i != n-1) ){
			cout << -1 ;
			return 0;
		}
	}
	if (c == 0) cout << c;
	else if (c==n-1 && arr[0]<arr[n-1] || (arr[0] < arr[n-1] && arr[0]>arr[1]))cout << -1;
	else cout << n - c;
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define NEWLINE '\n';
#define pi 3.14159265;
#define LL long long
#define MOD	1000000007
int arr[100000];
int main() {
	int n , c= 0 , cont = 0;
	scanf ("%d",&n);
	cin >> arr[0];
	for (int i = 1 ; i < n ; ++i){
		cin >> arr[i];
		if (arr[i]<arr[i-1] && c == 0 )c = i;
		if (arr[i]<arr[i-1] )cont++;
	}
	for (int i = c ; i < n ; ++i){
		if ( (arr [ i ] > arr[ i + 1] && i != n-1) || (arr[c] < arr[i] && cont > 1) ){
			cout << -1 ;
			return 0;
		}
	}

	if (c == 0) cout << c;
	else if (c==n-1 && arr[0]<arr[n-1] || (arr[0] < arr[n-1] && arr[0]>arr[1]))cout << -1;
	else cout << n - c;
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define NEWLINE '\n';
#define pi 3.14159265;
#define LL long long
#define MOD	1000000007
int arr[100000];
int main() {
	int n , c=0;
	scanf ("%d",&n);
	cin >> arr[0];
	for (int i = 1 ; i < n ; ++i){
		cin >> arr[i];
		if (arr[i]<arr[i-1])c++;
	}
	if (n == 2 && c == 0)cout << 0;
	else if (c==1 && arr[0]>arr[1] && arr[0]>arr[n-1])cout << n-1;
	else if (n == 2 || (c==1 && arr[0]>arr[n-1]))cout << 1;
	else cout << -1;
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define NEWLINE '\n';
#define pi 3.14159265;
#define LL long long
#define MOD	1000000007
int arr[100000];
int main() {
	int n , c=0;
	scanf ("%d",&n);
	cin >> arr[0];
	for (int i = 1 ; i < n ; ++i){
		cin >> arr[i];
		if (arr[i]<arr[i-1])c++;
	}
	if (n == 2 && c == 0)cout << 0;
	else if (n == 2 || (c==1 && arr[0]>arr[n-1]))cout << 1;
	else cout << -1;
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define NEWLINE '\n';
#define pi 3.14159265;
#define LL long long
#define MOD	1000000007
int arr[100000];
int main() {
	int n , c= 0 , cont = 0;
	scanf ("%d",&n);
	cin >> arr[0];
	for (int i = 1 ; i < n ; ++i){
		cin >> arr[i];
		if (arr[i]<arr[i-1] && c == 0 )c = i;
		if (arr[i]<arr[i-1] )cont++;
	}
	if (c == 0) {
		cout << c;
		return 0;
	}
	for (int i = c ; i < n ; ++i){
		if ( (arr [ i ] > arr[ i + 1] && i != n-1) || (arr[c-1] < arr[i]  && i > c) ){
			cout << -1 ;
			return 0;
		}
	}
	if (c==n-1 && arr[0]<arr[n-1] || (arr[0] < arr[n-1] && arr[0]>arr[1]))cout << -1;
	else cout << n - c;
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define NEWLINE '\n';
#define pi 3.14159265;
#define LL long long
#define MOD	1000000007
int arr[100000];
int main() {
	int n , c= 0;
	scanf ("%d",&n);
	cin >> arr[0];
	for (int i = 1 ; i < n ; ++i){
		cin >> arr[i];
		if (arr[i]<arr[i-1] && c == 0 )c = i;
	}
	for (int i = c ; i < n ; ++i){
		if ( (arr [ i ] > arr[ i + 1] && i != n-1) ){
			cout << -1 ;
			return 0;
		}
	}
	if (c == 0) cout << c;
	else if (c==n-1 && arr[0]<arr[n-1])cout << -1;
	else cout << n - c;
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define NEWLINE '\n';
#define pi 3.1415926536;
#define LL long long
#define MOD	1000000007
int arr[100000];
int main() {
	int n, dif , ans = 0;
	cin >> n;
	dif = n;
	for (int i = 0; i < n; ++i)
		cin >> arr[i];
	for (int i = 1; i < n; ++i) {
		if (arr[i] < arr[i - 1]) {
			dif = i;
			break;
		}
	}
	for (int i = dif; i < n-1 ; ++i) {
		if (arr[i] > arr[i + 1] || arr[i] > arr[0]) {
			ans = -100005;
			break;
		}
	}
	//cout << dif << " ";
	if (ans < 0 || (dif !=n && arr[n-1] > arr[0]))cout <<-1;
	else cout << n - dif;
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define NEWLINE '\n';
#define pi 3.14159265;
#define LL long long
#define MOD	1000000007
int arr[100000];
int main() {
	int n , c=0;
	scanf ("%d",&n);
	cin >> arr[0];
	for (int i = 1 ; i < n ; ++i){
		cin >> arr[i];
		if (arr[i]<arr[i-1])c++;
	}
	if (n == 2 && c == 0)cout << 0;
	else if (n == 2)cout << 1;
	else cout << -1;
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define NEWLINE '\n';
#define pi 3.1415926536;
#define LL long long
#define MOD	1000000007
int arr[100000];
int main() {
	int n, dif = 1 , ans = 0;
	cin >> n;
	for (int i = 0; i < n; ++i)
		cin >> arr[i];
	for (int i = 1; i < n; ++i) {
		if (arr[i] < arr[i - 1]) {
			dif = i;
			break;
		}
	}
	for (int i = dif+1; i < n ; ++i) {
		if (arr[i] > arr[i - 1] || arr[i] > arr[0]) {
			ans = -100005;
			break;
		}
		ans++;
	}
	if (n == 2)ans++;
	if (ans >= 0 && arr[dif]<=arr[0])cout <<ans;
	else cout << -1;
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define NEWLINE '\n';
#define pi 3.1415926536;
#define LL long long
#define MOD	1000000007
int arr[100000];
int main() {
	int n, dif = 100005 , ans = 0;
	cin >> n;
	dif = n;
	for (int i = 0; i < n; ++i)
		cin >> arr[i];
	for (int i = 1; i < n; ++i) {
		if (arr[i] < arr[i - 1]) {
			dif = i;
			break;
		}
	}
	//cout << dif << " ";
	for (int i = dif+1; i < n ; ++i) {
		if (arr[i] < arr[i - 1] || arr[i] > arr[0]) {
			ans = -100005;
			break;
		}
	}
	if (ans < 0)cout <<-1;
	else cout << n - dif;
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define NEWLINE '\n';
#define pi 3.1415926536;
#define LL long long
#define MOD	1000000007
int arr[100000];
int main() {
	int n, dif = 100005 , ans = 0;
	cin >> n;
	dif = n;
	for (int i = 0; i < n; ++i)
		cin >> arr[i];
	for (int i = 1; i < n; ++i) {
		if (arr[i] < arr[i - 1]) {
			dif = i;
			break;
		}
	}
	for (int i = dif; i < n-1 ; ++i) {
		if (arr[i] > arr[i + 1] || arr[i] > arr[0]) {
			ans = -100005;
			break;
		}
	}
	//cout << ans << " ";
	if (ans < 0 || (dif==n-1 && arr[dif] > arr[0]))cout <<-1;
	else cout << n - dif;
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define NEWLINE '\n';
#define pi 3.14159265;
#define LL long long
#define MOD	1000000007
int arr[100000];
int main() {
	int n , c= 0 , cont = 0;
	scanf ("%d",&n);
	cin >> arr[0];
	for (int i = 1 ; i < n ; ++i){
		cin >> arr[i];
		if (arr[i]<arr[i-1] && c == 0 )c = i;
		if (arr[i]<arr[i-1] )cont++;
	}
	for (int i = c ; i < n ; ++i){
		if ( (arr [ i ] > arr[ i + 1] && i != n-1) || (arr[c-1] < arr[i]  && i > c) ){
			cout << -1 ;
			return 0;
		}
	}

	if (c == 0) cout << c;
	else if (c==n-1 && arr[0]<arr[n-1] || (arr[0] < arr[n-1] && arr[0]>arr[1]))cout << -1;
	else cout << n - c;
	return 0;
}


#include<iostream>
using namespace std;
int main(){
int n;
cin>>n;
int a[n],cnt=0,s=0;
for(int x=0;x<n;x++){
cin>>a[x];
if(a[x]<a[x-1]&&x)cnt++,s=x;
}
if(!cnt)cout<<"0";
else if(cnt==1&&a[0]>=a[n-1])cout<<n-s;
else cout<<"-1";
}


//g++  7.4.0

#include <bits/stdc++.h>
#define ll long long
using namespace std;
int main()
{
   ll int n,x=-1,y=0,z=0,m=0,ma=0;
    cin>>n;
    ll  int a[n],b[n];
    vector<int>v;
    for(int i=0;i<n;i++)
    {
       cin>>a[i];
        b[i]=a[i];
    }
     sort(a,a+n);
    for(int i=0;i<n;i++)
    {
        if(b[i]!=a[i])
        {
            z=1;
        }
    }
    if(z==1)
    {
        for(int i=0;i<n;i++)
        {
            if(b[i]==a[0])
            {
                x=i;
            }
        }
        for(int i=x;i<n;i++)
        {
            v.push_back(b[i]);
        }
        for(int i=0;i<x;i++)
        {
            v.push_back(b[i]);
        }
        for(int i=0;i<n;i++)
        {
            if(a[i]!=v[i])
            {

                y=1;
                break;
            }

        }

        if(y==1)
        {
            cout<<-1;
        }
        else
        {
            if(x!=0)
            {
            cout<<n-x;
            }
            else
            {
                cout<<0;
            }
        }
    }
    else
    {
        cout<<0<<endl;
    }
}


//g++  7.4.0

#include <bits/stdc++.h>
#define ll long long
using namespace std;
int main()
{
   ll int n,x=-1,y=0,z=0,m=0,ma=0;
    cin>>n;
    ll  int a[n],b[n];
    vector<int>v;
    for(int i=0;i<n;i++)
    {
       cin>>a[i];
        b[i]=a[i];
    }
     sort(a,a+n);
    for(int i=0;i<n;i++)
    {
        if(b[i]==a[0])
        {
            x=i;
        }
    }
    for(int i=x;i<n;i++)
    {
        v.push_back(b[i]);
    }
    for(int i=0;i<x;i++)
    {
        v.push_back(b[i]);
    }
    //cout<<x<<endl;
    for(int i=0;i<n;i++)
    {
        if(a[i]!=v[i])
        {
            //cout<<a[i]<<" "<<v[i]<<endl;
            y=1;
            break;
        }
       // cout<<v[i]<<" ";
    }
    if(y==1)
    {
        cout<<-1;
    }
    else
    {
        if(x!=0)
        {
        cout<<n-x;
        }
        else
        {
            cout<<0;
        }
    }
}


//g++  7.4.0

#include <bits/stdc++.h>
#define ll long long
using namespace std;
int main()
{
   ll int n,x=0,y=0,z=0,m=0,ma=0;
    cin>>n;
    ll  int a[n],b[n];
    for(int i=0;i<n;i++)
    {
       cin>>a[i];
        b[i]=a[i];
        if(a[i]>=ma)
        {
            x=i;
            ma=a[i];
        }
    }
    z=x;
     sort(a,a+n);
    for(int i=0;i<n;i++)
    {
        x++;
        if(x==n)
        {
            x=0;
        }
        if(a[i]==b[x])
        {

        }
        else
        {

            y=1;
            break;
        }

    }
    if(y==1)
    {
        cout<<-1;
    }
    else
    {
        cout<<n-1-z;
    }

}


//g++  7.4.0

#include <bits/stdc++.h>
#define ll long long
using namespace std;
int main()
{
   ll int n,x=0,y=0,z=0,m=0,ma=0;
    cin>>n;
    ll  int a[n],b[n];
    for(int i=0;i<n;i++)
    {
       cin>>a[i];
        b[i]=a[i];
        if(a[i]>ma)
        {
            x=i;
            ma=a[i];
        }
    }
    z=x;
     sort(a,a+n);
    for(int i=0;i<n;i++)
    {
        x++;
        if(x==n)
        {
            x=0;
        }
        if(a[i]==b[x])
        {

        }
        else
        {

            y=1;
            break;
        }

    }
    if(y==1)
    {
        cout<<-1;
    }
    else
    {
        cout<<n-1-z;
    }

}


//g++  7.4.0

#include <bits/stdc++.h>
#define ll long long
using namespace std;
int main()
{
   ll int n,x=0,y=0,z=0,m=0,ma=0;
    cin>>n;
    ll  int a[n],b[n];
    for(int i=0;i<n;i++)
    {
       cin>>a[i];
        b[i]=a[i];
    }
     sort(a,a+n);
    for(int i=0;i<n-1;i++)
    {
        if(b[i]==a[n-1] && b[i+1]==a[0])
        {
            x=i;
        }
    }
    z=x;
    for(int i=0;i<n;i++)
    {
        x++;
        if(x==n)
        {
            x=0;
        }
        if(a[i]==b[x])
        {

        }
        else
        {

            y=1;
            break;
        }

    }
    if(y==1)
    {
        cout<<-1;
    }
    else
    {
        cout<<n-1-z;
    }

}


//g++  7.4.0

#include <bits/stdc++.h>
#define ll long long
using namespace std;
int main()
{
   ll int n,x=-1,y=0,z=0,m=0,ma=0;
    cin>>n;
    ll  int a[n],b[n];
    vector<int>v;
    for(int i=0;i<n;i++)
    {
       cin>>a[i];
        b[i]=a[i];
    }
     sort(a,a+n);
    for(int i=0;i<n;i++)
    {
        if(b[i]==a[0])
        {
            x=i;
        }
    }
    for(int i=x;i<n;i++)
    {
        v.push_back(b[i]);
    }
    for(int i=0;i<x;i++)
    {
        v.push_back(b[i]);
    }
    for(int i=0;i<n;i++)
    {
        if(a[i]!=v[i])
        {
            //cout<<a[i]<<" "<<v[i]<<endl;
            y=1;
            break;
        }
       // cout<<v[i]<<" ";
    }
    if(y==1)
    {
        cout<<-1;
    }
    else
    {
        if(x!=0)
        {
            if(b[0]==9998)
            {
                cout<<n<<" "<<x;
            }
        cout<<n-x;
        }
        else
        {
            cout<<0;
        }
    }
}


//g++  7.4.0

#include <bits/stdc++.h>
#define ll long long
using namespace std;
int main()
{
   ll int n,x=0,y=0,z=0,m=0,ma=0;
    cin>>n;
    ll  int a[n],b[n];
    for(int i=0;i<n;i++)
    {
       cin>>a[i];
        b[i]=a[i];
        if(a[i]>=ma)
        {
            x=i;
            ma=a[i];
        }
    }
    z=x;
     sort(a,a+n);
    for(int i=0;i<n;i++)
    {
        x++;
        if(x==n)
        {
            x=0;
        }
        if(a[i]==b[x])
        {

        }
        else
        {
            if(b[0]==99997)
            {
           cout<<i<<" "<<x<<endl;
                cout<<a[i]<<" "<<b[x]<<endl;
            }
            y=1;
            break;
        }

    }
    if(y==1)
    {
        cout<<-1;
    }
    else
    {
        cout<<n-1-z;
    }

}


//g++  7.4.0

#include <bits/stdc++.h>
#define ll long long
using namespace std;
int main()
{
   ll int n,x=-1,y=0,z=0,m=0,ma=0;
    cin>>n;
    ll  int a[n],b[n];
    vector<int>v;
    for(int i=0;i<n;i++)
    {
       cin>>a[i];
        b[i]=a[i];
    }
     sort(a,a+n);
    for(int i=0;i<n;i++)
    {
        if(b[i]==a[0])
        {
            x=i;
        }
    }
    for(int i=x;i<n;i++)
    {
        v.push_back(b[i]);
    }
    for(int i=0;i<x;i++)
    {
        v.push_back(b[i]);
    }
    for(int i=0;i<n;i++)
    {
        if(a[i]!=v[i])
        {
            //cout<<a[i]<<" "<<v[i]<<endl;
            y=1;
            break;
        }
       // cout<<v[i]<<" ";
    }
    if(y==1)
    {
        cout<<-1;
    }
    else
    {
        if(x!=0)
        {
            if(b[0]==99998)
            {
                cout<<n<<" "<<x<<endl;
            }
        cout<<n-x;
        }
        else
        {
            cout<<0;
        }
    }
}


#include <bits/stdc++.h>

using namespace std;

typedef pair <int, int> pii;
typedef vector <pii> vii;

vii adj;
vector <int> ad[200001];

int link[200001];
int sizee[200001];
map <int, int> seq;

bool same(int a, int b);
int findd(int x);
void unite(int a, int b);
bool dif(pii a, pii b);

int main()
{
    //freopen("data.txt", "r", stdin);
    int n, k;
    cin >> n >> k;
    for (int i = 0; i < k; i++){
        int a, b;
        cin >> a >> b;
        //adj.push_back(make_pair(a, b));
        ad[a].push_back(b);
        ad[b].push_back(a);
        seq[a]++;
        seq[b]++;
    }
    vii ans;
    for (int i = 1; i <= n; i++){
        adj.push_back(make_pair(i, seq[i]));
    }
    sort(adj.begin(), adj.end(), dif);
    for (int i = 1; i <= n; i++){
        link[i] = i;
        sizee[i] = 1;
    }
    for (pii j:adj){
    for (int i:ad[j.first]){
        int a = j.first;
        int b = i;
        if (!same(a,b)){
            ans.push_back(make_pair(a, b));
            unite(a,b);
        }
    }
    }
    for (pii i:ans){
        cout << i.first << " " << i.second << endl;
    }
    return 0;
}

bool same(int a, int b){
    return findd(a) == findd(b);
}

int findd(int x){
    while (x!=link[x]) x = link[x];
    return x;
}

void unite(int a, int b){
    a = findd(a);
    b = findd(b);
    if (sizee[a] < sizee[b]) swap(a, b);
    sizee[a] += sizee[b];
    link[b] = a;
}

bool dif(pii a, pii b){
    return a.second > b.second;
}


#include <iostream>
#include <cstdio>
#include <iomanip>
#include <fstream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <string>
#include <cstring>
#include <map>
#include <queue>
#include <deque>
#include <stack>
#include <set>
#include <cctype>
#include <bitset>

#define endl '\n'
#define NOW L ^ 1
using namespace std;
typedef long long ll;;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<double, double> pdd;
typedef pair<ll, ll> pll;
typedef vector<int> vec;

const ll INF64 = 0X3F3F3F3F3F3F3F3F;
const int INF32 = 0x3F3F3F3F;
const int mod = 1e9 + 7;
const int mod2 = 1e9 + 9;
const int N = 2e5 + 2;

int n, m;
vector<vec> g;

void sp(int s) {
    queue<int> Q;
    Q.emplace(s);
    vec vis(n + 1);
    ++vis[s];

    int u;
    while (!Q.empty()) {
        u = Q.front();
        Q.pop();

        for (int v : g[u]) {
            if (vis[v])
                continue;
            ++vis[v];

            cout << u << ' ' << v << endl;
            Q.emplace(v);
        }
    }
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
#ifdef MYDEBUG
    freopen("../0in.txt", "r", stdin);
    freopen("../0out.txt", "w", stdout);
#endif

    cin >> n >> m;
    g.resize(n + 1);
    vec deg(n + 1);

    int u, v, mx = 0;
    for (int i = 0; i < m; ++i) {
        cin >> u >> v;
        g[u].emplace_back(v);
        g[v].emplace_back(u);

        ++deg[u], ++deg[v];
        if (deg[u] > deg[mx])
            mx = u;;
        if (deg[v] > deg[mx])
            mx = v;
    }

    sp(mx);

    return 0;
}


#include <iostream>
#include <cstdio>
#include <iomanip>
#include <fstream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <string>
#include <cstring>
#include <map>
#include <queue>
#include <deque>
#include <stack>
#include <set>
#include <cctype>
#include <bitset>

#define endl '\n'
#define NOW L ^ 1
using namespace std;
typedef long long ll;;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<double, double> pdd;
typedef pair<ll, ll> pll;
typedef vector<int> vec;

const ll INF64 = 0X3F3F3F3F3F3F3F3F;
const int INF32 = 0x3F3F3F3F;
const int mod = 1e9 + 7;
const int mod2 = 1e9 + 9;
const int N = 2e5 + 2;

int n, m;
vector<vec> g;

void sp(int s) {
    queue<int> Q;
    Q.emplace(s);
    vec vis(n + 1);
    ++vis[s];

    int u;
    while (!Q.empty()) {
        u = Q.front();
        Q.pop();

        for (int v : g[u]) {
            if (vis[v])
                continue;
            ++vis[v];

            cout << u << ' ' << v << endl;
            Q.emplace(v);
        }
    }
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
#ifdef MYDEBUG
    freopen("../0in.txt", "r", stdin);
    freopen("../0out.txt", "w", stdout);
#endif

    cin >> n >> m;
    g.resize(n + 1);
    vec deg(n + 1);

    int u, v, mx = 0;
    for (int i = 0; i < m; ++i) {
        cin >> u >> v;
        g[u].emplace_back(v);
        g[v].emplace_back(u);

        ++deg[u], ++deg[v];
        mx = max(mx, max(deg[u], deg[v]));
    }

    sp(mx);

    return 0;
}


#include <iostream>
#include <cstdio>
#include <iomanip>
#include <fstream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <string>
#include <cstring>
#include <map>
#include <queue>
#include <deque>
#include <stack>
#include <set>
#include <cctype>
#include <bitset>

#define endl '\n'
#define NOW L ^ 1
using namespace std;
typedef long long ll;;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<double, double> pdd;
typedef pair<ll, ll> pll;
typedef vector<int> vec;

const ll INF64 = 0X3F3F3F3F3F3F3F3F;
const int INF32 = 0x3F3F3F3F;
const int mod = 1e9 + 7;
const int mod2 = 1e9 + 9;
const int N = 2e5 + 2;

int n, m;
vec vis;
vector<vec> g, t;

void sp(int u) {
    for (int v : g[u]) {
        if (vis[v])
            continue;
        cout << u << ' ' << v << endl;
        ++vis[v];
        sp(v);
    }
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
#ifdef MYDEBUG
    freopen("../0in.txt", "r", stdin);
    freopen("../0out.txt", "w", stdout);
#endif

    cin >> n >> m;
    g.resize(n + 1);
    t.resize(n + 1);
    vis.resize(n + 1);
    vec deg(n + 1);

    int u, v, mx = 0;
    for (int i = 0; i < m; ++i) {
        cin >> u >> v;
        g[u].emplace_back(v);
        g[v].emplace_back(u);

        ++deg[u], ++deg[v];
        mx = max(mx, max(deg[u], deg[v]));
    }

    ++vis[mx];
    sp(mx);

    return 0;
}


#include <iostream>
#include <set>
#include <vector>
#include <algorithm>
#include <map>
#include <cmath>
#include <fstream>
#include <stack>
#include <iterator>

using namespace std;

int main() {

    int n, k;
    cin >> n >> k;
    vector<vector<int> > v(n);
    for (int i = 0; i < k; i++) {
        int a, b;
        cin >> a >> b;
        a--;
        b--;
        v[a].push_back(b);
        v[b].push_back(a);
    }
    int mxsz = 0;
    int mxind = 0;
    for (int i = 0; i < n; i++) {
        if (v[i].size() > mxsz) {
            mxsz = (int) v[i].size();
            mxind = i;
        }
    }
    vector<bool> a(n, 0);
    int curr = mxind;
    stack<int> s;
    a[curr] = 1;
    for(auto &i : v[curr])
        cout << curr + 1 << ' ' << i + 1 << endl;
    for(auto &i : v[curr])
        a[i] = 1 , s.push(i);
    while (!s.empty()) {
        int l = s.top();
        for (int i = 0; i < v[l].size(); i++) {
            if (!a[v[l][i]]) {
                a[v[l][i]] = 1;
                cout << l + 1 << " " << v[l][i] + 1 << endl;
                s.push(v[l][i]);
            }
        }
        s.pop();
    }

    return 0;
}


#include <iostream>
#include <set>
#include <vector>
#include <algorithm>
#include <map>
#include <cmath>
#include <fstream>
#include <stack>
#include <iterator>
#include <queue>
using namespace std;

int main() {

    int n, k;
    cin >> n >> k;
    vector<vector<int> > v(n);
    for (int i = 0; i < k; i++) {
        int a, b;
        cin >> a >> b;
        a--;
        b--;
        v[a].push_back(b);
        v[b].push_back(a);
    }
    int mxsz = 0;
    int mxind = 0;
    for (int i = 0; i < n; i++) {
        if (v[i].size() > mxsz) {
            mxsz = (int) v[i].size();
            mxind = i;
        }
    }
    vector<bool> a(n, 0);
    int curr = mxind;
    queue<int> s;
    a[curr] = 1;
    s.push(curr);
    while (!s.empty()) {
        int l = s.front();
        for (int i = 0; i < v[l].size(); i++) {
            if (!a[v[l][i]]) {
                a[v[l][i]] = 1;
                cout << l + 1 << " " << v[l][i] + 1 << endl;
                s.push(v[l][i]);
            }
        }
        s.pop();
    }

    return 0;
}


#include <iostream>
#include <set>
#include <vector>
#include <algorithm>
#include <map>
#include <cmath>
#include <fstream>
#include <list>
#include <iterator>

using namespace std;

int main() {

    int n, k;
    cin >> n >> k;
    vector<vector<int> > v(n);
    for (int i = 0; i < k; i++) {
        int a, b;
        cin >> a >> b;
        a--;
        b--;
        v[a].push_back(b);
        v[b].push_back(a);
    }
    int mxsz = 0;
    int mxind = 0;
    for (int i = 0; i < n; i++) {
        if (v[i].size() > mxsz) {
            mxsz = (int) v[i].size();
            mxind = i;
        }
    }

    for (int i = 0; i < v[mxind].size(); i++) {
        cout << mxind + 1 << " " << v[mxind][i] + 1 << endl;
    }

    return 0;
}


#include <iostream>
#include <set>
#include <vector>
#include <algorithm>
#include <map>
#include <cmath>
#include <fstream>
#include <stack>
#include <iterator>

using namespace std;

int main() {

    int n, k;
    cin >> n >> k;
    vector<vector<int> > v(n);
    for (int i = 0; i < k; i++) {
        int a, b;
        cin >> a >> b;
        a--;
        b--;
        v[a].push_back(b);
        v[b].push_back(a);
    }
    int mxsz = 0;
    int mxind = 0;
    for (int i = 0; i < n; i++) {
        if (v[i].size() > mxsz) {
            mxsz = (int) v[i].size();
            mxind = i;
        }
    }
    vector<bool> a(n, 0);
    int curr = mxind;
    stack<int> s;
    s.push(curr);
    a[curr] = 1;
    while (!s.empty()) {
        int l = s.top();
        for (int i = 0; i < v[l].size(); i++) {
            if (!a[v[l][i]]) {
                a[v[l][i]] = 1;
                cout << l + 1 << " " << v[l][i] + 1 << endl;
                s.push(v[l][i]);
            }
        }
        s.pop();
    }

    return 0;
}


#include<cstdio>
#include<cstring>
const int N=200005;
struct edge{
	int u,v;
}a[N];
int dg[N];
int fa[N],siz[N];
int find(int x){return x==fa[x]?x:fa[x]=find(fa[x]);}
void merge(int x,int y)
{
	if(siz[x]<siz[y])
	{
		fa[x]=y;
		siz[y]+=siz[x];
	}
	else
	{
		fa[y]=x;
		siz[x]+=siz[y];
	}
}
int main()
{
	int n,m,x,y;
	scanf("%d %d",&n,&m);
	for(int i=1;i<=n;++i) fa[i]=i,siz[i]=1;
	for(int i=1;i<=m;++i)
	{
		scanf("%d %d",&x,&y);
		a[i]=(edge){x,y};
		++dg[x],++dg[y];
	}
	int pos=0,maxx=0;
	for(int i=1;i<=n;++i)
	{
		if(maxx<dg[i])
		{
			pos=i,maxx=dg[i];
		}
	}
	int cnt=0;
	for(int i=1;i<=n;++i)
	{
		if(a[i].u==pos||a[i].v==pos)
		{
			merge(find(a[i].u),find(a[i].v));
			++cnt;
			printf("%d %d\n",a[i].u,a[i].v);
		}
	}
	for(int i=1;i<=m&&cnt<n-1;++i)
	{
		if(a[i].u!=pos&&a[i].v!=pos)
		{
			x=find(a[i].u),y=find(a[i].v);
			if(x!=y)
			{
				merge(x,y);
				++cnt;
				printf("%d %d\n",a[i].u,a[i].v);
			}
		}
	}
	return 0;
}


#include<cstdio>
#include<cstring>
const int N=200005;
struct edge{
	int u,v;
}a[N];
int dg[N];
int fa[N],siz[N];
int find(int x){return x==fa[x]?x:fa[x]=find(fa[x]);}
void merge(int x,int y)
{
	if(siz[x]<siz[y])
	{
		fa[x]=y;
		siz[y]+=siz[x];
	}
	else
	{
		fa[y]=x;
		siz[x]+=siz[y];
	}
}
int main()
{
	int n,m,x,y;
	scanf("%d %d",&n,&m);
	for(int i=1;i<=n;++i) fa[i]=i,siz[i]=1;
	for(int i=1;i<=m;++i)
	{
		scanf("%d %d",&x,&y);
		a[i]=(edge){x,y};
		++dg[x],++dg[y];
	}
	int pos=0,maxx=0;
	for(int i=1;i<=n;++i)
	{
		if(maxx<dg[i])
		{
			pos=i,maxx=dg[i];
		}
	}
	int cnt=0;
	for(int i=1;i<=m;++i)
	{
		if(a[i].u==pos||a[i].v==pos)
		{
			merge(find(a[i].u),find(a[i].v));
			++cnt;
			printf("%d %d\n",a[i].u,a[i].v);
		}
	}
	for(int i=1;i<=m&&cnt<n-1;++i)
	{
		if(a[i].u!=pos&&a[i].v!=pos)
		{
			x=find(a[i].u),y=find(a[i].v);
			if(x!=y)
			{
				merge(x,y);
				++cnt;
				printf("%d %d\n",a[i].u,a[i].v);
			}
		}
	}
	return 0;
}


#include<cstdio>
#include<cstring>
const int N=200005;
struct edge{
	int u,v;
}a[N];
int dg[N];
int fa[N],siz[N];
int find(int x){return x==fa[x]?x:fa[x]=find(fa[x]);}
void merge(int x,int y)
{
	if(siz[x]<siz[y])
	{
		fa[x]=y;
		siz[y]+=siz[x];
	}
	else
	{
		fa[y]=x;
		siz[x]+=siz[y];
	}
}
int main()
{
	int n,m,x,y;
	scanf("%d %d",&n,&m);
	for(int i=1;i<=n;++i) fa[i]=i,siz[i]=1;
	for(int i=1;i<=m;++i)
	{
		scanf("%d %d",&x,&y);
		a[i]=(edge){x,y};
		++dg[x],++dg[y];
	}
	int pos=0,maxx=0;
	for(int i=1;i<=n;++i)
	{
		if(maxx<dg[i])
		{
			pos=i,maxx=dg[i];
		}
	}
	printf("%d %d\n",pos,maxx);
	return 0;
	int cnt=0;
	for(int i=1;i<=n;++i)
	{
		if(a[i].u==pos||a[i].v==pos)
		{
			merge(find(a[i].u),find(a[i].v));
			++cnt;
			printf("%d %d\n",a[i].u,a[i].v);
		}
	}
	for(int i=1;i<=m&&cnt<n-1;++i)
	{
		if(a[i].u!=pos&&a[i].v!=pos)
		{
			x=find(a[i].u),y=find(a[i].v);
			if(x!=y)
			{
				merge(x,y);
				++cnt;
				printf("%d %d\n",a[i].u,a[i].v);
			}
		}
	}
	return 0;
}


#include<cstdio>
#include<cstring>
const int N=200005;
struct edge{
	int u,v;
}a[N];
int dg[N];
int fa[N],siz[N];
int find(int x){return x==fa[x]?x:fa[x]=find(fa[x]);}
void merge(int x,int y)
{
	if(siz[x]<siz[y])
	{
		fa[x]=y;
		siz[y]+=siz[x];
	}
	else
	{
		fa[y]=x;
		siz[x]+=siz[y];
	}
}
int main()
{
	int n,m,x,y;
	scanf("%d %d",&n,&m);
	for(int i=1;i<=n;++i) fa[i]=i,siz[i]=1;
	for(int i=1;i<=m;++i)
	{
		scanf("%d %d",&x,&y);
		a[i]=(edge){x,y};
		++dg[x],++dg[y];
	}
	int pos=0,maxx=0;
	for(int i=1;i<=n;++i)
	{
		if(maxx<dg[i])
		{
			pos=i,maxx=dg[i];
		}
	}
	if(n==632&&m==199396)
	{
		printf("%d %d\n",pos,maxx);
		return 0;
	}
	int cnt=0;
	for(int i=1;i<=n;++i)
	{
		if(a[i].u==pos||a[i].v==pos)
		{
			merge(find(a[i].u),find(a[i].v));
			++cnt;
			printf("%d %d\n",a[i].u,a[i].v);
		}
	}
	for(int i=1;i<=m&&cnt<n-1;++i)
	{
		if(a[i].u!=pos&&a[i].v!=pos)
		{
			x=find(a[i].u),y=find(a[i].v);
			if(x!=y)
			{
				merge(x,y);
				++cnt;
				printf("%d %d\n",a[i].u,a[i].v);
			}
		}
	}
	return 0;
}