#include<bits/stdc++.h>
#define ll long long
#define ld long double
using namespace std;

int main()
{
    int n ;
    cin>>n;
    int arr[n];
    for(int i =0 ; i<n ; i++)
    {
        cin>>arr[i];
    }
    if (n==2)
    {
        if (arr[0]>arr[1])return cout<<1,0;
        else return cout<<0,0;
    }
    bool aa=1,bb=0;
    int b=0;
    int i;
    int h=n-1;
    for( i =0 ; i<n-1 ; i++)
    {
        if (arr[i]<arr[i+1])
        {
            aa=1;bb=0;
        }
        else if (arr[i]>arr[i+1])
        {
            //cout<<arr[i]<<" "<<arr[i+1]<<endl;
            if (aa){b++;
            h=i;
            }
            bb=1;aa=0;
        }

    }
    if (arr[i]>arr[0])
        {
           // cout<<arr[i]<<" "<<arr[0]<<endl;
           b++;

        }
       // cout<<b<<endl;
    if (b>1)return cout<<-1,0;

cout<<n-h-1;

    return 0;
}


#include<bits/stdc++.h>
#define ll long long
#define ld long double
using namespace std;

int main()
{
    int n;
    cin>>n;
    int arr[n];
    for(int i =0 ; i<n ; i++)cin>>arr[i];
    if (arr[0]>arr[1]&&arr[0]>arr[n-1]&&n!=2)return cout<<-1,0;
     if (arr[0]<arr[1]&&arr[0]<arr[n-1]&&n!=2)return cout<<-1,0;
    for(int i =1 ; i<n-1 ; i++)
    {
        if (arr[i]>arr[i+1]&&arr[i]>arr[i-1])return cout<<-1,0;
    }int res=0;
    for(int i =n-1 ; i>0 ; i-- )
    {
        if (arr[i]<arr[i-1])res++;
    }
    cout<<res;

    return 0;
}


#include<bits/stdc++.h>
#define ll long long
#define ld long double
using namespace std;

int main()
{
    int n;
    cin>>n;
    int arr[n+2];
    for(int i =1 ; i<=n ; i++)cin>>arr[i];
    arr[0]=arr[n];
    arr[n+1]=arr[1];
    int res=n;
    for(int i =1 ; i<=n ; i++)
    {

        if (arr[i]>arr[i+1])res=i;
        if (arr[i]>arr[i+1]&&arr[i]<arr[i-1])return cout<<-1,0;

    }
    cout<<n-res;

    return 0;
}


#include<bits/stdc++.h>
#define ll long long
#define ld long double
using namespace std;

int main()
{
    int n ;
    cin>>n;
    int arr[n];
    for(int i =0 ; i<n ; i++)
    {
        cin>>arr[i];
    }
    if (n==2)
    {
        if (arr[0]>arr[1])return cout<<1,0;
        else return cout<<0,0;
    }
    bool aa=1,bb=1;
    int b=0;
    int i;
    int a=0;
    int h=n-1;
    for( i =0 ; i<n-1 ; i++)
    {
        if (arr[i]<arr[i+1])
        {
            if (bb)
            a++;
            aa=1;bb=0;
        }
        else if (arr[i]>arr[i+1])
        {
            //cout<<arr[i]<<" "<<arr[i+1]<<endl;
            if (aa){b++;
            h=i;
            }
            bb=1;aa=0;
        }

    }
   // cout<<a<<endl;
     if (arr[i]<arr[0])
        {
            if (bb&&a)return cout<<-1,0;
        }
    if (arr[i]>arr[0])
        {
           // cout<<arr[i]<<" "<<arr[0]<<endl;
           b++;

        }
       // cout<<b<<endl;
    if (b>1)return cout<<-1,0;

cout<<n-h-1;

    return 0;
}


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

int main(){
  int N;
  cin >> N;
  vector<int> V(N * 2);
  int min = 1e9;
  int min_pos = -1;

  for(int i = 0; i < N; i++) {
    int dum; cin >> dum;
    V[i] = dum;
    if (dum < min) {
      min = dum;
      min_pos = i;
    }
  }

  for(int i = 0; i < 2 * N; i++) V[i + N] = V[i];
  int cnt = 1;
  for(int i = min_pos; i < min_pos + N; i++){
    if(i + 1 < min_pos + N && V[i] <= V[i+1]) cnt++;
  }

  if (cnt == N && min_pos == 0) cout << 0 << endl;
  else if (cnt == N) cout << N - min_pos << endl;
  else cout << -1 << endl;
  return 0;


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

int main(){
  int N;
  cin >> N;
  vector<int> V((N * 2)+1);
  V.push_back(1e9);
  for(int i = 1; i <= N; i++) {
    int dum; cin >> dum;
    V[i] = dum;
  }
  // 2 3 1 2 3 1
  for(int i = 1; i <= 2 * N; i++) V[i + N] = V[i];

  for(int i = 1; i <= 2 * N; i++){
    int cnt = 1;
    int j = i;

    while(V[j] <= V[j+1] && j < j + N) {
      cnt++;
      j++;
    }
    if (cnt >= N){
      cout <<  i-1  << endl;
      return 0;
    }
    i = j;
    i--;
  }

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


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

int main(){
  int N;
  cin >> N;
  vector<int> V(N);

  for (auto &v: V) cin >> v;
  int smallest_index;

  int mini = 1e9;
  for (int i = 0; i < V.size(); i++){
    if (V[i] < mini) {
      smallest_index = i;
      mini = V[i];
    }
  }
  bool ok1 = true, ok2 = true;

   // #s after smallest are all in non decreasing order
  for(int i = 1; i < smallest_index; i++){
    if (V[i-1] > V[i]) ok1 = false;
  }

  // #s after smallest are all in non decreasing order
  for(int i = smallest_index + 1; i < V.size(); i++){
    if (V[i-1] > V[i]) ok1 = false;
  }

  if (ok1 && ok2 && !smallest_index) cout << 0 << endl;
  else if (ok1 && ok2) cout << V.size() - smallest_index << endl;
  else cout << -1 <<endl;
  return 0;
}


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

int main(){
  int N;
  cin >> N;
  vector<int> V((N * 2)+1);
  V.push_back(1e9);
  for(int i = 1; i <= N; i++) {
    int dum; cin >> dum;
    V[i] = dum;
  }
  // 2 3 1 2 3 1
  for(int i = 1; i <= 2 * N; i++) V[i + N] = V[i];

  for(int i = 1; i <= 2 * N; i++){
    int cnt = 1;
    int j = i;

    while(V[j] <= V[j+1] && j < j + N) {
      cnt++;
      j++;
    }
    if (cnt >= N){
      cout <<  i-1  << endl;
      return 0;
    }
  }

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


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

int main(){
  int N;
  cin >> N;
  vector<int> V((N * 2)+1);
  V.push_back(1e9);
  for(int i = 1; i <= N; i++) {
    int dum; cin >> dum;
    V[i] = dum;
  }
  // 2 3 1 2 3 1
  for(int i = 1; i <= 2 * N; i++) V[i + N] = V[i];

  for(int i = 1; i <= 2 * N; i++){
    int cnt = 1;
    int j = i;

    while(V[j] <= V[j+1] && j < j + N) {
      cnt++;
      j++;
      i++;
    }
    if (cnt >= N){
      cout <<  i-2  << endl;
      return 0;
    }
  }

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


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

int main(){
  int N;
  cin >> N;
  vector<int> V(N);

  for (auto &v: V) cin >> v;
  int smallest_index;

  int mini = 1e9;
  for (int i = 0; i < V.size(); i++){
    if (V[i] < mini) {
      smallest_index = i;
      mini = V[i];
    }
  }
  bool ok1 = true, ok2 = true;

  // #s before smallest are all in non increasing order
  for(int i = 1; i < smallest_index; i++){
    if (V[i-1] < V[i]) ok1 = false;
  }

  // #s after smallest are all in non decreasing order
  for(int i = smallest_index + 1; i < V.size(); i++){
    if (V[i-1] > V[i]) ok1 = false;
  }

  if (ok1 && ok2 && !smallest_index) cout << 0 << endl;
  else if (ok1 && ok2) cout << V.size() - smallest_index << endl;
  else cout << -1 <<endl;
  return 0;
}


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

int main(){
  int N;
  cin >> N;
  vector<int> V((N * 2)+1);
  V.push_back(1e9);
  for(int i = 1; i <= N; i++) {
    int dum; cin >> dum;
    V[i] = dum;
  }
  // 2 3 1 2 3 1
  for(int i = 1; i <= 2 * N; i++) V[i + N] = V[i];

  for(int i = 1; i <= 2 * N; i++){
    int cnt = 1;
    int j = i;

    while(V[j] <= V[j+1] && j < j + N) {
      cnt++;
      j++;
    }
    if (cnt >= N){
      cout <<  (i == 1 ? 0 : i)  << endl;
      return 0;
    }
  }

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


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

int main(){
  int N;
  cin >> N;
  vector<int> V(N * 2);
  int min = 1e9;
  int min_pos = -1;

  for(int i = 0; i < N; i++) {
    int dum; cin >> dum;
    V[i] = dum;
    if (dum < min) {
      min = dum;
      min_pos = i;
    }
  }

  for(int i = 0; i < 2 * N; i++) V[i + N] = V[i];
  int cnt = 1;
  for(int i = min_pos; i < min_pos + N; i++){
    if(V[i] <= V[i+1]) cnt++;
  }

  if (cnt == N && min_pos == 0) cout << 0 << endl;
  else if (cnt == N) cout << N - min_pos << endl;
  else cout << -1 << endl;
  return 0;
}


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

int main(){
  int N;
  cin >> N;
  vector<int> V(N * 5);
  int min = 1e9;
  int min_pos = -1;

  for(int i = 0; i < N; i++) {
    int dum; cin >> dum;
    V[i] = dum;
    if (dum < min) {
      min = dum;
      min_pos = i;
    }
  }

  for(int i = 0; i < 2 * N; i++) V[i + N] = V[i];
  int cnt = 1;
  for(int i = min_pos; i < min_pos + N; i++){
    if(i + 1 < min_pos + N && V[i] <= V[i+1]) cnt++;
  }

  if (cnt == N && min_pos == 0) cout << 0 << endl;
  else if (cnt+1 == N && V[N-1] <= V[0]) cout << 1 << endl;
  else if (cnt == N) cout << N - min_pos << endl;
  else cout << -1 << endl;
  return 0;
}


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

int main(){
  int N;
  cin >> N;
  vector<int> V((N * 2) + 4);
  int min = 1e9;
  int min_pos = -1;

  for(int i = 0; i < N; i++) {
    int dum; cin >> dum;
    V[i] = dum;
    if (dum < min) {
      min = dum;
      min_pos = i;
    }
  }

  for(int i = 0; i < 2 * N; i++) V[i + N] = V[i];
  int cnt = 1;
  for(int i = min_pos; i < min_pos + N; i++){
    if(i + 1 < min_pos + N && V[i] <= V[i+1]) cnt++;
  }

  if (cnt == N && min_pos == 0) cout << 0 << endl;
  else if (cnt == N) cout << N - min_pos << endl;
  else cout << -1 << endl;
  return 0;
}


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

int main(){
  int N;
  cin >> N;
  vector<int> V(N * 2);
  int min = 1e9;
  int min_pos = -1;

  for(int i = 0; i < N; i++) {
    int dum; cin >> dum;
    V[i] = dum;
    if (dum < min) {
      min = dum;
      min_pos = i;
    }
  }

  for(int i = 0; i < 2 * N; i++) V[i + N] = V[i];
  int cnt = 1;
  for(int i = min_pos; i < min_pos + N; i++){
    if(V[i] <= V[i+1] && i + 1 < min_pos + N) cnt++;
  }

  if (cnt == N && min_pos == 0) cout << 0 << endl;
  else if (cnt == N) cout << N - min_pos << endl;
  else cout << -1 << endl;
  return 0;
}


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

int main(){
  int N;
  cin >> N;
  vector<int> V(N + 1);

  int discrepancies = 0, first_d = 0;

  for(int i = 0; i < N; i++){
    int dum; cin >> dum;
    V[i] = dum;
  }

  for(int i = 0; i < N-1; i++){
    if (!discrepancies && V[i] > V[i+1]) {
      first_d = i;
      discrepancies++;
    }else if (V[i] > V[i+1]) {
      discrepancies++;
    }
  }

  if (discrepancies > 1 || (V[N-1] > V[0] && discrepancies)){
    cout << -1 << endl;
  }else {
    cout << (discrepancies == 0 ? 0 : N - first_d - 1) << endl;
  }
  return 0;
}


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

int main(){
  int N;
  cin >> N;
  vector<int> V(N * 5);
  int min = 1e9;
  int min_pos = -1;

  for(int i = 0; i < N; i++) {
    int dum; cin >> dum;
    V[i] = dum;
    if (dum <= min) {
      min = dum;
      min_pos = i;
    }
  }

  for(int i = 0; i < 2 * N; i++) V[i + N] = V[i];
  int cnt = 1;
  for(int i = min_pos; i < min_pos + N; i++){
    if(i + 1 < min_pos + N && V[i] <= V[i+1]) cnt++;
  }

  if (cnt == N && min_pos == 0) cout << 0 << endl;
  else if (cnt == N) cout << N - min_pos << endl;
  else cout << -1 << endl;
  return 0;
}


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

int main(){
  int N;
  cin >> N;
  vector<int> V(N * 5);
  int min = 1e9;
  int min_pos = -1;

  for(int i = 0; i < N; i++) {
    int dum; cin >> dum;
    V[i] = dum;
    if (dum < min) {
      min = dum;
      min_pos = i;
    }
  }

  for(int i = 0; i < 2 * N; i++) V[i + N] = V[i];
  int cnt = 1;
  for(int i = min_pos; i < min_pos + N; i++){
    if(i + 1 < min_pos + N && V[i] <= V[i+1]) cnt++;
  }

  if (cnt == N && min_pos == 0) cout << 0 << endl;
  else if (cnt == N) cout << N - min_pos << endl;
  else cout << -1 << endl;
  return 0;
}


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

int main(){
  int N;
  cin >> N;
  vector<int> V(N * 2);
  int min = 1e9;
  int min_pos = -1;

  for(int i = 0; i < N; i++) {
    int dum; cin >> dum;
    V[i] = dum;
    if (dum < min) {
      min = dum;
      min_pos = i;
    }
  }

  for(int i = 0; i < 2 * N; i++) V[i + N] = V[i];
  int cnt = 1;
  for(int i = min_pos; i < min_pos + N; i++){
    if(i + 1 < min_pos + N && V[i] <= V[i+1]) cnt++;
  }

  if (cnt == N && min_pos == 0) cout << 0 << endl;
  else if (cnt == N) cout << N - min_pos << endl;
  else cout << -1 << endl;
  return 0;
}


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

int main(){
  int N;
  cin >> N;
  vector<int> V(N);

  for (auto &v: V) cin >> v;
  int smallest_index;

  int mini = 1e9;
  for (int i = 0; i < V.size(); i++){
    if (V[i] < mini) {
      smallest_index = i;
      mini = V[i];
    }
  }
  bool ok1 = true, ok2 = true;

  // #s before smallest are all in non increasing order
  for(int i = 1; i < smallest_index; i++){
    if (V[i-1] < V[i]) ok1 = false;
  }

  // #s after smallest are all in non decreasing order
  for(int i = smallest_index + 1; i < V.size(); i++){
    if (V[i-1] > V[i]) ok1 = false;
  }

  if (ok1 && ok2 && !smallest_index) cout << 0 << endl;
  else if (ok1 && ok2) cout << V.size() % smallest_index + 1;
  else cout << -1 <<endl;
  return 0;
}


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

int main(){
  int N;
  cin >> N;
  vector<int> V((N * 2)+1);
  V.push_back(1e9);
  for(int i = 1; i <= N; i++) {
    int dum; cin >> dum;
    V[i] = dum;
  }
  // 2 3 1 2 3 1
  for(int i = 1; i <= 2 * N; i++) V[i + N] = V[i];

  for(int i = 1; i <= N; i++){
    int cnt = 1;
    int j = i;

    while(V[j] <= V[j+1] && j < j + N) {
      cnt++;
      j++;
    }
    if (cnt >= N){
      cout <<  i-1  << endl;
      return 0;
    }
  }

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


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

int main(){
  int N;
  cin >> N;
  vector<int> V(N * 2);
  int min = 1e9;
  int min_pos = -1;

  for(int i = 0; i < N; i++) {
    int dum; cin >> dum;
    V[i] = dum;
    if (dum < min) {
      min = dum;
      min_pos = i;
    }
  }

  for(int i = 0; i < 2 * N; i++) V[i + N] = V[i];
  int cnt = 1;
  for(int i = min_pos; i < min_pos + N; i++){
    if(V[i] <= V[i+1] && i + 1 < min_pos + N) cnt++;
  }

  if (cnt == N && min_pos == 0) cout << 0 << endl;
  if (cnt+1 == N && V[N-1] <= V[0]) cout << 1 << endl;
  else if (cnt == N) cout << N - min_pos << endl;
  else cout << -1 << endl;
  return 0;
}


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

int main(){
  int N;
  cin >> N;
  vector<int> V(N);

  for (auto &v: V) cin >> v;
  int smallest_index;

  int mini = 1e9;
  for (int i = 0; i < V.size(); i++){
    if (V[i] < mini) {
      smallest_index = i;
      mini = V[i];
    }
  }
  bool ok1 = true, ok2 = true;

  // #s before smallest are all in non increasing order
  for(int i = 1; i < smallest_index; i++){
    if (V[i-1] < V[i]) ok1 = false;
  }

  // #s after smallest are all in non decreasing order
  for(int i = smallest_index + 1; i < V.size(); i++){
    if (V[i-1] > V[i]) ok1 = false;
  }

  // cout << "mini -> " << mini << endl;
  // cout << "smallest_index -> " << smallest_index << endl;
  if (ok1 && ok2 && !smallest_index) cout << 0 << endl;
  else if (ok1 && ok2) cout << smallest_index % V.size()<< endl;
  else cout << -1 <<endl;
  return 0;
}


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

int main(){
  int N;
  cin >> N;
  vector<int> V(N);

  for (auto &v: V) cin >> v;
  int smallest_index;

  int mini = 1e9;
  for (int i = 0; i < V.size(); i++){
    if (V[i] < mini) {
      smallest_index = i;
      mini = V[i];
    }
  }
  bool ok1 = true, ok2 = true;

  // #s before smallest are all in non increasing order
  for(int i = 1; i <= smallest_index; i++){
    if (V[i-1] < V[i]) ok1 = false;
  }

  // #s after smallest are all in non decreasing order
  for(int i = smallest_index + 1; i < V.size(); i++){
    if (V[i-1] > V[i]) ok1 = false;
  }

  if (ok1 && ok2 && !smallest_index) cout << 0 << endl;
  else if (ok1 && ok2) cout << V.size() - smallest_index << endl;
  else cout << -1 <<endl;
  return 0;
}


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

int main(){
  int N;
  cin >> N;
  vector<int> V(N);

  for (auto &v: V) cin >> v;
  int smallest_index;

  int mini = 1e9;
  for (int i = 0; i < V.size(); i++){
    if (V[i] <= mini) {
      smallest_index = i;
      mini = V[i];
    }
  }
  bool ok1 = true, ok2 = true;

   // #s after smallest are all in non decreasing order
  for(int i = 1; i < smallest_index; i++){
    if (V[i-1] > V[i]) ok1 = false;
  }

  // #s after smallest are all in non decreasing order
  for(int i = smallest_index + 1; i < V.size(); i++){
    if (V[i-1] > V[i]) ok1 = false;
  }

  if ( (ok1 && ok2 && !smallest_index) || is_sorted(V.begin(), V.end()))  cout << 0 << endl;
  else if (ok1 && ok2) cout << V.size() - smallest_index << endl;
  else cout << -1 <<endl;
  return 0;
}


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

int main(){
  int N;
  cin >> N;
  vector<int> V(N * 5);
  int min = 1e9;
  int min_pos = -1;

  for(int i = 0; i < N; i++) {
    int dum; cin >> dum;
    V[i] = dum;
    if (dum < min) {
      min = dum;
      min_pos = i;
    }
  }

  for(int i = 0; i < 2 * N; i++) V[i + N] = V[i];
  int cnt = 1;
  for(int i = min_pos; i < min_pos + N; i++){
    if(i + 1 < min_pos + N && V[i] <= V[i+1]) cnt++;
  }

  if ((cnt == N && min_pos == 0) || is_sorted(V.begin(), V.begin() + N)) cout << 0 << endl;
  else if (cnt == N) cout << N - min_pos << endl;
  else cout << -1 << endl;
  return 0;
}


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

int main(){
  int N;
  cin >> N;
  vector<int> V(N);

  for (auto &v: V) cin >> v;
  int smallest_index;

  int mini = 1e9;
  for (int i = 0; i < V.size(); i++){
    if (V[i] <= mini) {
      smallest_index = i;
      mini = V[i];
    }
  }
  bool ok1 = true, ok2 = true;

   // #s after smallest are all in non decreasing order
  for(int i = 1; i < smallest_index; i++){
    if (V[i-1] > V[i]) ok1 = false;
  }

  // #s after smallest are all in non decreasing order
  for(int i = smallest_index + 1; i < V.size(); i++){
    if (V[i-1] > V[i]) ok1 = false;
  }

  if (ok1 && ok2 && !smallest_index) cout << 0 << endl;
  else if (ok1 && ok2) cout << V.size() - smallest_index << endl;
  else cout << -1 <<endl;
  return 0;
}


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

int main(){
  int N;
  cin >> N;
  vector<int> V((N * 2)+1);
  V.push_back(1e9);
  for(int i = 1; i <= N; i++) {
    int dum; cin >> dum;
    V[i] = dum;
  }
  // 2 3 1 2 3 1
  for(int i = 1; i <= 2 * N; i++) V[i + N] = V[i];

  for(int i = 1; i <= 2 * N; i++){
    int cnt = 1;
    int j = i;

    while(V[j] <= V[j+1] && j < j + N) {
      cnt++;
      j++;
    }
    if (cnt >= N){
      cout <<  i-1  << endl;
      return 0;
    }
    if (j >  i) {
      i = j;
      i--;
    }
  }

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


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

int main(){
  int N;
  cin >> N;
  vector<int> V(N * 5);
  int min = 1e9;
  int min_pos = -1;

  for(int i = 0; i < N; i++) {
    int dum; cin >> dum;
    V[i] = dum;
    if (dum < min) {
      min = dum;
      min_pos = i;
    }
  }

  for(int i = 0; i < 2 * N; i++) V[i + N] = V[i];
  int cnt = 1;
  for(int i = min_pos; i < min_pos + N; i++){
    if(i + 1 < min_pos + N && V[i] <= V[i+1]) cnt++;
  }

  if (cnt == N && min_pos == 0) cout << 0 << endl;
  if (cnt+1 == N && V[N-1] <= V[0]) cout << 1 << endl;
  else if (cnt == N) cout << N - min_pos << endl;
  else cout << -1 << endl;
  return 0;
}


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

int main(){
  int N;
  cin >> N;
  vector<int> V(N * 5);
  int min = 1e9;
  int min_pos = -1;

  for(int i = 0; i < N; i++) {
    int dum; cin >> dum;
    V[i] = dum;
    if (dum <= min) {
      min = dum;
      min_pos = i;
    }
  }

  for(int i = 0; i < 2 * N; i++) V[i + N] = V[i];
  int cnt = 1;
  for(int i = min_pos; i < min_pos + N; i++){
    if(i + 1 < min_pos + N && V[i] <= V[i+1]) cnt++;
  }

  if ((cnt == N && min_pos == 0) || is_sorted(V.begin(), V.begin() + N)) cout << 0 << endl;
  else if (cnt == N) cout << N - min_pos << endl;
  else cout << -1 << endl;
  return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main(){
    int n;
    cin>>n;
    vector<int>v(n);
    for(auto &x: v)
        cin>>x;
    if(is_sorted(v.begin(),v.end())) {
        cout << "0" << endl;
        return 0;
    }
    auto idx=min_element(v.begin(),v.end());
    bool r=is_sorted(idx,v.end());
    bool l=is_sorted(v.begin(),idx-1);
    if(r&l)
        cout<<idx-v.begin()<<endl;
    else
        cout<<"-1"<<endl;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main(){
    int n;
    cin>>n;
    vector<int>v(n);
    for(auto &x: v)
        cin>>x;
    if(is_sorted(v.begin(),v.end())){
        cout<<"0"<<endl;
        return 0;
}

    int l =0;
    while(v[l]<v[l+1]&&l+1<n)
        l++;
    l++;
    if(l<n&&l+1<n){
        if(v[l]>v[l+1]) {
            cout << "-1" << endl;
            return 0;
        }
    }
    if(l+1==n){
        if(v[l]>v[0]) {
            cout << "-1" << endl;
            return 0;
        }
    }
    cout<<n-l<<endl;
}


#include<bits/stdc++.h>
using namespace std;
int main(){
    int n;
    cin>>n;
    vector<int>v(n);
    for(auto &x: v)
        cin>>x;
    if(is_sorted(v.begin(),v.end())) {
        cout << "0" << endl;
        return 0;
    }
    auto idx=min_element(v.begin(),v.end());
    bool r=is_sorted(idx,v.end());
    bool l=is_sorted(v.begin(),idx-1);
    if(r&l)
        cout<<v.end()-idx<<endl;
    else
        cout<<"-1"<<endl;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main(){
    int n;
    cin>>n;
    vector<int>v(n);
    for(auto &x: v)
        cin>>x;
    if(is_sorted(v.begin(),v.end())){
        cout<<"0"<<endl;
        return 0;
}

    int l =0;
    while(v[l]<v[l+1]&&l+1<n)
        l++;
    if(l!=0)
    l++;
    if(l<n&&l+1<n){
        if(v[l]>v[l+1]) {
            cout << "-1" << endl;
            return 0;
        }
    }
    if(l+1==n){
        if(v[l]>v[0]) {
            cout << "-1" << endl;
            return 0;
        }
    }
    cout<<n-l<<endl;
}


#include<bits/stdc++.h>
using namespace std;
bool comp(int a ,int b){
    return a<=b;
}
bool com(int a ,int b){
    return a<b;
}
int main(){
    int n;
    cin>>n;
    vector<int>v(n);
    for(auto &x: v)
        cin>>x;
    if(is_sorted(v.begin(),v.end())) {
        cout << "0" << endl;
        return 0;
    }
    int count =0;
    auto idx1=min_element(v.begin(),v.end(),comp);
    auto idx=min_element(v.begin(),v.end());

    bool r=is_sorted(idx1,v.end());
    auto res=idx1;

    bool b=(v[0]>=v[n-1]);
    while(*idx1==*idx){
        idx1--;
        count++;
    }
    bool l=is_sorted(v.begin(),idx1+1);
      if(r&l&b)
        cout<<v.end()-res+count-1<<endl;
     else

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


#include<bits/stdc++.h>
using namespace std;
int main(){
    int n;
    cin>>n;
    vector<int>v(n);
    for(auto &x: v)
        cin>>x;
    if(is_sorted(v.begin(),v.end())) {
        cout << "0" << endl;
        return 0;
    }
    auto idx=min_element(v.begin(),v.end());
    bool r=is_sorted(idx,v.end());
    bool l=is_sorted(v.begin(),idx);
    if(r&l)
        cout<<v.end()-idx<<endl;
    else
        cout<<"-1"<<endl;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
bool comp(int a ,int b){
    return a<=b;
}
int main(){
    int n;
    cin>>n;
    vector<int>v(n);
    for(auto &x: v)
        cin>>x;
    if(is_sorted(v.begin(),v.end())) {
        cout << "0" << endl;
        return 0;
    }
    auto idx1=min_element(v.begin(),v.end(),comp);
    auto idx=min_element(v.begin(),v.end());
    bool r=is_sorted(idx1,v.end());
    bool l=is_sorted(v.begin(),idx);
      if(r&l)
        cout<<v.end()-idx1<<endl;
     else

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


#include<bits/stdc++.h>
using namespace std;
bool comp(int a ,int b){
    return a<=b;
}
int main(){
    int n;
    cin>>n;
    vector<int>v(n);
    for(auto &x: v)
        cin>>x;
    if(is_sorted(v.begin(),v.end())) {
        cout << "0" << endl;
        return 0;
    }
    auto idx=min_element(v.begin(),v.end(),comp);
    bool r=is_sorted(idx,v.end());
    bool l=is_sorted(v.begin(),idx);
    if(r&l)
        cout<<v.end()-idx<<endl;
    else
        cout<<"-1"<<endl;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main(){
    int n;
    cin>>n;
    vector<int>v(n);
    for(auto &x: v)
        cin>>x;
    if(is_sorted(v.begin(),v.end())){
        cout<<"0"<<endl;
        return 0;
}
    if(v[n-1]>v[0]){
        cout<<"-1"<<endl;
        return 0;
    }
    int l =0;
    while(v[l]<v[l+1]&&l+1<n)
        l++;
    cout<<n-l-1<<endl;
}


#include<bits/stdc++.h>
using namespace std;
void checkIfSortRotated(vector<int>& arr, int n)
{
    int minEle = INT_MAX;
    int maxEle = INT_MIN;

    int minIndex = -1;

    // Find the minimum element
    // and it's index
    for (int i = 0; i < n; i++) {
        if (arr[i] < minEle) {
            minEle = arr[i];
            minIndex = i;
        }
    }

    int flag1 = 1;

    // Check if all elements before minIndex
    // are in increasing order
    for (int i = 1; i < minIndex; i++) {
        if (arr[i] < arr[i - 1]) {
            flag1 = 0;
            break;
        }
    }

    int flag2 = 1;

    // Check if all elements after minIndex
    // are in increasing order
    for (int i = minIndex + 1; i < n; i++) {
        if (arr[i] < arr[i - 1]) {
            flag2 = 0;
            break;
        }
    }

    // Check if last element of the array
    // is smaller than the element just
    // before the element at minIndex
    if (flag1 && flag2 && (arr[n - 1] < arr[minIndex - 1]))
        cout << n-minIndex<<endl;
    else
        cout << "-1"<<endl;
}
int main(){
    int n;
    cin>>n;
    vector<int>v(n);
    for(auto &x: v)
        cin>>x;
    if(is_sorted(v.begin(),v.end())) {
        cout << "0" << endl;
        return 0;
    }
        checkIfSortRotated(v,n);
        return 0;
}


#include<bits/stdc++.h>
using namespace std;
bool comp(int a ,int b){
    return a<=b;
}
int main(){
    int n;
    cin>>n;
    vector<int>v(n);
    for(auto &x: v)
        cin>>x;
    if(is_sorted(v.begin(),v.end())) {
        cout << "0" << endl;
        return 0;
    }
    int count =0;
    auto idx1=min_element(v.begin(),v.end(),comp);
    auto idx=min_element(v.begin(),v.end());

    bool r=is_sorted(idx1,v.end());
    auto res=idx1;
    bool l=is_sorted(v.begin(),idx);
    while(*idx1==*idx){
        idx1--;
        count++;
    }
      if(r&l)
        cout<<v.end()-res+count-1<<endl;
     else

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


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

#define PRINT_FAILED    {                   \
                            cout<<"-1";     \
                            return 0;       \
                        }

int main()
{
    long long int n,  min_index = 0,  min_count = 0, temp, bo_ol = 0;
    vector<long long int> arr;
    cin>>n>>temp;
    arr.push_back(temp);
    for(long long int i = 1; i < n; i++)
    {
        // cout<<i<<"<- i\n";
        cin>>temp;
        arr.push_back(temp);
        if(arr[min_index] > arr[i]){
            min_index = i;
            min_count++;
            if(bo_ol >= 1 && min_count > 1)
                PRINT_FAILED
            // cout<<"min_count raised \n";
        }
        if(arr[i] == arr[min_index] && i != min_index)
        {
            if(i == n-1 && bo_ol == 0)
            {
                cout<<"1";
                return 0;
            }
            else if(i == n-1 && bo_ol == n-1)
            {
                cout<<"0";
                return 0;
            }
            else
                bo_ol++;
        }
        if(min_count >= 2)
            PRINT_FAILED
        if(arr[i - 1] > arr[i] && (arr[i] == arr[min_index]))
        {
            min_index = i;
            min_count++;
        }
        if(arr[i - 1] > arr[i] && (i != min_index))
        {
            PRINT_FAILED
        }
    }
    if(min_index == 0)
        cout<<"0";
    else
    {
        if(arr[0] >= arr[n-1])
            cout<<n - min_index;
        else
            PRINT_FAILED
    }
}


#include<iostream>
using namespace std;

int main()
{
    int n, min_index = 0, count = 0, min_count = 0;
    int arr[n];
    cin>>n>>arr[0];
    for(int i = 1; i < n; i++)
    {
        cin>>arr[i];
        if(arr[min_index] > arr[i]){
            min_index = i;
            min_count++;
            // cout<<"min_count raised \n";
        }
        if(min_count >= 3)
        {
            cout<<"-1161";
            return 0;
        }
    }

    // cout<<"min index -> "<<min_index<<endl;
    // for(int i = 0; i < n; i++)
    // {
        // cout<<i<<" -> "<<arr[i]<<endl;
    // }

    if(arr[0] < arr[n-1] && min_index != 0)
    {
        cout<<"-1";
        return 0;
    }
    for(int i = 0; i < min_index - 1; i++)
    {
        // cout<<"check1\n";
        if(arr[i+1] < arr[i])
        {
            cout<<"-1";
            return 0;
        }
    }
    if(min_index == n - 1)
    {
        cout<<"1";
        return 0;
    }
    count++;
    for(int i = min_index; i < n - 1; i++)
    {
        // cout<<"check2\n";
        if(arr[i+1] > arr[i])
            count++;
        else
        {
            cout<<"-1";
            return 0;
        }
    }
    if(min_index == 0)
    {
        cout<<"0";
        return 0;
    }
    cout<<count;

}


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

#define PRINT_FAILED    {                   \
                            cout<<"-1";     \
                            return 0;       \
                        }

int main()
{
    long long int n,  min_index = 0,  min_count = 0, temp, bo_ol = 0;
    vector<long long int> arr;
    cin>>n>>temp;
    arr.push_back(temp);
    for(long long int i = 1; i < n; i++)
    {
        // cout<<i<<"<- i\n";
        cin>>temp;
        arr.push_back(temp);
        if(arr[min_index] > arr[i]){
            min_index = i;
            min_count++;
            if(bo_ol >= 1)
                PRINT_FAILED
            // cout<<"min_count raised \n";
        }
        if(arr[i] == arr[min_index] && i != min_index)
        {
            if(i == n-1 && bo_ol == 0)
            {
                cout<<"1";
                return 0;
            }
            else if(i == n-1 && bo_ol == n-1)
            {
                cout<<"0";
                return 0;
            }
            else
                bo_ol++;
        }
        if(min_count >= 2)
            PRINT_FAILED
        if(arr[i - 1] > arr[i] && (arr[i] == arr[min_index]))
        {
            min_index = i;
        }
        if(arr[i - 1] > arr[i] && (i != min_index))
        {
            PRINT_FAILED
        }
    }
    if(min_index == 0)
        cout<<"0";
    else
    {
        if(arr[0] >= arr[n-1])
            cout<<n - min_index;
        else
            PRINT_FAILED
    }
}


#include<iostream>
using namespace std;

int main()
{
    int n, min_index = 0, count = 0;
    int arr[n];
    cin>>n;
    // cout<<"n is -> "<<n<<endl;
    for(int i = 0; i < n; i++)
    {
        cin>>arr[i];
        if(arr[min_index] > arr[i]){
            min_index = i;
        }
        // cout<<i<<" -> "<<arr[i]<<endl;
    }

    // cout<<"min index -> "<<min_index<<endl;
    for(int i = 0; i < n; i++)
    {
        // cout<<i<<" -> "<<arr[i]<<endl;
    }

    if(arr[0] < arr[n-1] && min_index != 0)
    {
        cout<<"-1";
        return 0;
    }
    for(int i = 0; i < min_index - 1; i++)
    {
        // cout<<"check1\n";
        if(arr[i+1] < arr[i])
        {
            cout<<"-1";
            return 0;
        }
    }
    if(min_index == n - 1)
    {
        cout<<"1";
        return 0;
    }
    count++;
    for(int i = min_index; i < n - 1; i++)
    {
        // cout<<"check2\n";
        if(arr[i+1] > arr[i])
            count++;
        else
        {
            cout<<"-1";
            return 0;
        }
    }
    if(min_index == 0)
    {
        cout<<"0";
        return 0;
    }
    cout<<count;

}


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

#define PRINT_FAILED    {                   \
                            cout<<"-1";     \
                            return 0;       \
                        }

int main()
{
    long int n,  min_index = 0,  min_count = 0, temp;
    vector<long int> arr;
    cin>>n>>temp;
    arr.push_back(temp);
    for(long int i = 1; i < n; i++)
    {
        //cout<<i<<"<- i\n";
        cin>>temp;
        arr.push_back(temp);
        if(arr[min_index] > arr[i]){
            min_index = i;
            min_count++;
            //cout<<"min_count raised \n";
        }
        if(min_count >= 2)
            PRINT_FAILED
        if(arr[i - 1] > arr[i] && i != min_index)
            PRINT_FAILED
    }
    if(min_index == 0)
        cout<<"0";
    else
    {
        if(arr[0] > arr[n-1])
            cout<<n - min_index;
        else
            PRINT_FAILED
    }
}


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

#define PRINT_FAILED    {                   \
                            cout<<"-1";     \
                            return 0;       \
                        }

int main()
{
    long long int n,  min_index = 0,  min_count = 0, temp;
    vector<long long int> arr;
    cin>>n>>temp;
    arr.push_back(temp);
    for(long long int i = 1; i < n; i++)
    {
        // cout<<i<<"<- i\n";
        cin>>temp;
        arr.push_back(temp);
        if(arr[min_index] > arr[i]){
            min_index = i;
            min_count++;
            // cout<<"min_count raised \n";
        }
        if(arr[i] == arr[min_index])
        {
            if(i == n-1)
            {
                cout<<"1";
                return 0;
            }
            else
                PRINT_FAILED
        }
        if(min_count >= 2)
            PRINT_FAILED
        if(arr[i - 1] > arr[i] && (i != min_index))
                PRINT_FAILED
    }
    if(min_index == 0)
        cout<<"0";
    else
    {
        if(arr[0] >= arr[n-1])
            cout<<n - min_index;
        else
            PRINT_FAILED
    }
}


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

#define PRINT_FAILED    {                   \
                            cout<<"-1";     \
                            return 0;       \
                        }

int main()
{
    long long int n,  min_index = 0,  min_count = 0, temp;
    vector<long long int> arr;
    cin>>n>>temp;
    arr.push_back(temp);
    for(long long int i = 1; i < n; i++)
    {
        // cout<<i<<"<- i\n";
        cin>>temp;
        arr.push_back(temp);
        if(arr[min_index] > arr[i]){
            min_index = i;
            min_count++;
            // cout<<"min_count raised \n";
        }
        if(arr[i] == arr[min_index])
            if(i == n-1)
            {
                cout<<"1";
                return 0;
            }
        if(min_count >= 2)
            PRINT_FAILED
        if(arr[i - 1] > arr[i] && (i != min_index))
                PRINT_FAILED
    }
    if(min_index == 0)
        cout<<"0";
    else
    {
        if(arr[0] >= arr[n-1])
            cout<<n - min_index;
        else
            PRINT_FAILED
    }
}


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

#define PRINT_FAILED    {                   \
                            cout<<"-1";     \
                            return 0;       \
                        }

int main()
{
    long long int n,  min_index = 0,  min_count = 0, temp;
    vector<long long int> arr;
    cin>>n>>temp;
    arr.push_back(temp);
    for(long long int i = 1; i < n; i++)
    {
        // cout<<i<<"<- i\n";
        cin>>temp;
        arr.push_back(temp);
        if(arr[min_index] > arr[i]){
            min_index = i;
            min_count++;
            // cout<<"min_count raised \n";
        }
        if(min_count >= 2)
            PRINT_FAILED
        if(arr[i - 1] > arr[i] && i != min_index)
            PRINT_FAILED
    }
    if(min_index == 0)
        cout<<"0";
    else
    {
        if(arr[0] > arr[n-1])
            cout<<n - min_index;
        else
            PRINT_FAILED
    }
}


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

#define PRINT_FAILED    {                   \
                            cout<<"-1";     \
                            return 0;       \
                        }

int main()
{
    long long int n,  min_index = 0,  min_count = 0, temp;
    vector<long long int> arr;
    cin>>n>>temp;
    arr.push_back(temp);
    for(long long int i = 1; i < n; i++)
    {
        // cout<<i<<"<- i\n";
        cin>>temp;
        arr.push_back(temp);
        if(arr[min_index] > arr[i]){
            min_index = i;
            min_count++;
            // cout<<"min_count raised \n";
        }
        if(min_count >= 2)
            PRINT_FAILED
        if(arr[i - 1] > arr[i] && i != min_index)
            PRINT_FAILED
    }
    if(min_index == 0)
        cout<<"0";
    else
    {
        if(arr[0] >= arr[n-1])
            cout<<n - min_index;
        else
            PRINT_FAILED
    }
}


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

#define PRINT_FAILED    {                   \
                            cout<<"-1";     \
                            return 0;       \
                        }

int main()
{
    long long int n,  min_index = 0,  min_count = 0, temp, bo_ol = 0;
    vector<long long int> arr;
    cin>>n>>temp;
    arr.push_back(temp);
    for(long long int i = 1; i < n; i++)
    {
        // cout<<i<<"<- i\n";
        cin>>temp;
        arr.push_back(temp);
        if(arr[min_index] > arr[i]){
            min_index = i;
            min_count++;
            if(bo_ol >= 1 && min_count > 1)
                PRINT_FAILED
        // cout<<"min_count raised \n";
        }
        if(arr[i - 1] > arr[i] && (arr[i] == arr[min_index]) && i != min_index)
        {
            min_index = i;
            if(++min_count >= 2)
            {
                // cout<<"min_count raised \n";
                PRINT_FAILED
            }
        }
        if(arr[i] == arr[min_index] && i != min_index)
        {
            if(arr[i-1] != arr[i])
            {
                if(i == n-1 && bo_ol == 0)
                {
                    cout<<"1";
                    return 0;
                }
                else if(i == n-1 && bo_ol == n-1)
                {
                    cout<<"0";
                    return 0;
                }
                else
                    bo_ol++;
            }
        }
        if(min_count >= 2)
            PRINT_FAILED
        if(arr[i - 1] > arr[i] && (i != min_index))
        {
            PRINT_FAILED
        }
    }
    if(min_index == 0)
        cout<<"0";
    else
    {
        if(arr[0] >= arr[n-1])
            cout<<n - min_index;
        else
            PRINT_FAILED
    }
}


#include<iostream>
using namespace std;

#define PRINT_FAILED    {                   \
                            cout<<"-1";     \
                            return 0;       \
                        }

int main()
{
    long long int n, min_index = 0, min_count = 0;
    long long int arr[n];
    cin>>n>>arr[0];
    for(long long int i = 1; i < n; i++)
    {
        //cout<<i<<"<- i\n";
        cin>>arr[i];
        if(arr[min_index] > arr[i]){
            min_index = i;
            min_count++;
            //cout<<"min_count raised \n";
        }
        if(min_count >= 2)
            PRINT_FAILED
        if(arr[i - 1] > arr[i] && i != min_index)
            PRINT_FAILED
    }
    if(min_index == 0)
        cout<<"0";
    else
    {
        if(arr[0] > arr[n-1])
            cout<<n - min_index;
        else
            PRINT_FAILED
    }
}


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

#define PRINT_FAILED    {                   \
                            cout<<"-1";     \
                            return 0;       \
                        }

int main()
{
    long long int n,  min_index = 0,  min_count = 0, temp, bo_ol = 0;
    vector<long long int> arr;
    cin>>n>>temp;
    arr.push_back(temp);
    for(long long int i = 1; i < n; i++)
    {
        // cout<<i<<"<- i\n";
        cin>>temp;
        arr.push_back(temp);
        if(arr[min_index] > arr[i]){
            min_index = i;
            min_count++;
            // cout<<"min_count raised \n";
        }
        if(arr[i] == arr[min_index] && i != min_index)
        {
            if(i == n-1 && bo_ol == 0)
            {
                cout<<"1";
                return 0;
            }
            else if(i == n-1 && bo_ol == n-1)
            {
                cout<<"0";
                return 0;
            }
            else
                bo_ol++;
        }
        if(min_count >= 2)
            PRINT_FAILED
        if(arr[i - 1] > arr[i] && (i != min_index))
                PRINT_FAILED
    }
    if(min_index == 0)
        cout<<"0";
    else
    {
        if(arr[0] >= arr[n-1])
            cout<<n - min_index;
        else
            PRINT_FAILED
    }
}


#include<iostream>
using namespace std;

#define PRINT_FAILED    {                   \
                            cout<<"-1";     \
                            return 0;       \
                        }

int main()
{
    long int n, min_index = 0, min_count = 0;
    long int arr[n];
    cin>>n>>arr[0];
    for(long int i = 1; i < n; i++)
    {
        //cout<<i<<"<- i\n";
        cin>>arr[i];
        if(arr[min_index] > arr[i]){
            min_index = i;
            min_count++;
            //cout<<"min_count raised \n";
        }
        if(min_count >= 2)
            PRINT_FAILED
        if(arr[i - 1] > arr[i] && i != min_index)
            PRINT_FAILED
    }
    if(min_index == 0)
        cout<<"0";
    else
    {
        if(arr[0] > arr[n-1])
            cout<<n - min_index;
        else
            PRINT_FAILED
    }
}


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

#define PRINT_FAILED    {                   \
                            cout<<"-1";     \
                            return 0;       \
                        }

int main()
{
    long long int n,  min_index = 0,  min_count = 0, temp, bo_ol = 0;
    vector<long long int> arr;
    cin>>n>>temp;
    arr.push_back(temp);
    for(long long int i = 1; i < n; i++)
    {
        // cout<<i<<"<- i\n";
        cin>>temp;
        arr.push_back(temp);
        if(arr[min_index] > arr[i]){
            min_index = i;
            min_count++;
            if(bo_ol >= 1 && min_count > 1)
                PRINT_FAILED
            // cout<<"min_count raised \n";
        }
        if(arr[i] == arr[min_index] && i != min_index)
        {
            if(i == n-1 && bo_ol == 0)
            {
                cout<<"1";
                return 0;
            }
            else if(i == n-1 && bo_ol == n-1)
            {
                cout<<"0";
                return 0;
            }
            else
                bo_ol++;
        }
        if(min_count >= 2)
            PRINT_FAILED
        if(arr[i - 1] > arr[i] && (arr[i] == arr[min_index]) && i != min_index)
        {
            min_index = i;
            min_count++;
        }
        if(arr[i - 1] > arr[i] && (i != min_index))
        {
            PRINT_FAILED
        }
    }
    if(min_index == 0)
        cout<<"0";
    else
    {
        if(arr[0] >= arr[n-1])
            cout<<n - min_index;
        else
            PRINT_FAILED
    }
}


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

#define PRINT_FAILED    {                   \
                            cout<<"-1";     \
                            return 0;       \
                        }

int main()
{
    long long int n,  min_index = 0,  min_count = 0, temp, bo_ol = 0;
    vector<long long int> arr;
    cin>>n>>temp;
    arr.push_back(temp);
    for(long long int i = 1; i < n; i++)
    {
        // cout<<i<<"<- i\n";
        cin>>temp;
        arr.push_back(temp);
        if(arr[min_index] > arr[i]){
            min_index = i;
            min_count++;
            if(bo_ol >= 1 && min_count > 1)
                PRINT_FAILED
        // cout<<"min_count raised \n";
        }
        if(arr[i - 1] > arr[i] && (arr[i] == arr[min_index]) && i != min_index)
        {
            min_index = i;
            if(++min_count >= 2)
            {
                // cout<<"min_count raised \n";
                PRINT_FAILED
            }
        }
        if(arr[i] == arr[min_index] && i != min_index)
        {
            if(i == n-1 && bo_ol == 0)
            {
                cout<<"1";
                return 0;
            }
            else if(i == n-1 && bo_ol == n-1)
            {
                cout<<"0";
                return 0;
            }
            else
                bo_ol++;
        }
        if(min_count >= 2)
            PRINT_FAILED
        if(arr[i - 1] > arr[i] && (i != min_index))
        {
            PRINT_FAILED
        }
    }
    if(min_index == 0)
        cout<<"0";
    else
    {
        if(arr[0] >= arr[n-1])
            cout<<n - min_index;
        else
            PRINT_FAILED
    }
}


#include<iostream>
using namespace std;

#define PRINT_FAILED    {                   \
                            cout<<"-1";     \
                            return 0;       \
                        }

int main()
{
    int n, min_index = 0, count = 0, min_count = 0;
    int arr[n];
    cin>>n>>arr[0];
    for(int i = 1; i < n; i++)
    {
        //cout<<i<<"<- i\n";
        cin>>arr[i];
        if(arr[min_index] > arr[i]){
            min_index = i;
            min_count++;
            //cout<<"min_count raised \n";
        }
        if(min_count >= 2)
            PRINT_FAILED
        if(arr[i - 1] > arr[i] && i != min_index)
            PRINT_FAILED
    }
    if(min_index == 0)
        cout<<"0";
    else
    {
        if(arr[0] > arr[n-1])
            cout<<n - min_index;
        else
            PRINT_FAILED
    }
}


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

#define PRINT_FAILED    {                   \
                            cout<<"-1";     \
                            return 0;       \
                        }

int main()
{
    long long int n,  min_index = 0,  min_count = 0, temp, bo_ol = 0;
    vector<long long int> arr;
    cin>>n>>temp;
    arr.push_back(temp);
    for(long long int i = 1; i < n; i++)
    {
        // cout<<i<<"<- i\n";
        cin>>temp;
        arr.push_back(temp);
        if(arr[min_index] > arr[i]){
            min_index = i;
            min_count++;
            if(bo_ol >= 1 && min_count > 1)
                PRINT_FAILED
            // cout<<"min_count raised \n";
        }
        if(arr[i] == arr[min_index] && i != min_index)
        {
            if(i == n-1 && bo_ol == 0)
            {
                cout<<"1";
                return 0;
            }
            else if(i == n-1 && bo_ol == n-1)
            {
                cout<<"0";
                return 0;
            }
            else
                bo_ol++;
        }
        if(min_count >= 2)
            PRINT_FAILED
        if(arr[i - 1] > arr[i] && (arr[i] == arr[min_index]))
        {
            min_index = i;
        }
        if(arr[i - 1] > arr[i] && (i != min_index))
        {
            PRINT_FAILED
        }
    }
    if(min_index == 0)
        cout<<"0";
    else
    {
        if(arr[0] >= arr[n-1])
            cout<<n - min_index;
        else
            PRINT_FAILED
    }
}


#include<iostream>
using namespace std;

int main()
{
    int n, min_index = 0, count = 0, min_count = 0;
    int arr[n];
    cin>>n>>arr[0];
    for(int i = 1; i < n; i++)
    {
        // cout<<i<<"<- i\n";
        cin>>arr[i];
        if(arr[min_index] > arr[i]){
            min_index = i;
            min_count++;
            // cout<<"min_count raised \n";
        }
        if(min_count >= 2)
        {
            cout<<"-1";
            return 0;
        }
    }

    // cout<<"min index -> "<<min_index<<endl;
    // for(int i = 0; i < n; i++)
    // {
        // cout<<i<<" -> "<<arr[i]<<endl;
    // }

    if(arr[0] < arr[n-1] && min_index != 0)
    {
        cout<<"-1";
        return 0;
    }
    for(int i = 0; i < min_index - 1; i++)
    {
        // cout<<"check1\n";
        if(arr[i+1] < arr[i])
        {
            cout<<"-1";
            return 0;
        }
    }
    if(min_index == n - 1)
    {
        cout<<"1";
        return 0;
    }
    count++;
    for(int i = min_index; i < n - 1; i++)
    {
        // cout<<"check2\n";
        if(arr[i+1] > arr[i])
            count++;
        else
        {
            cout<<"-1";
            return 0;
        }
    }
    if(min_index == 0)
    {
        cout<<"0";
        return 0;
    }
    cout<<count;

}


#include<iostream>
using namespace std;

int main()
{
    int n, arr[n], min_index = 0, count = 0;
    cin>>n;
    for(int i = 0; i < n; i++)
    {
        cin>>arr[i];
        if(arr[min_index] > arr[i])
            min_index = i;
    }
    if(arr[0] < arr[n-1] && min_index != 0)
    {
        cout<<"-1";
        return 0;
    }
    for(int i = 0; i < min_index - 1; i++)
        if(arr[i+1] < arr[i])
        {
            cout<<"-1";
            return 0;
        }
    if(min_index == n - 1)
    {
        cout<<"1";
        return 0;
    }
    count++;
    for(int i = min_index; i < n - 1; i++)
    {
        if(arr[i+1] > arr[i])
            count++;
        else
        {
            cout<<"-1";
            return 0;
        }
    }
    if(min_index == 0)
    {
        cout<<"0";
        return 0;
    }
    cout<<count;
}


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

#define PRINT_FAILED    {                   \
                            cout<<"-1";     \
                            return 0;       \
                        }

int main()
{
    long long int n,  min_index = 0,  min_count = 0, temp, bo_ol = 0;
    vector<long long int> arr;
    cin>>n>>temp;
    arr.push_back(temp);
    for(long long int i = 1; i < n; i++)
    {
        // cout<<i<<"<- i\n";
        cin>>temp;
        arr.push_back(temp);
        if(arr[min_index] > arr[i]){
            min_index = i;
            min_count++;
            // cout<<"min_count raised \n";
        }
        if(arr[i] == arr[min_index] && i != min_index)
        {
            if(i == n-1 && bo_ol == 0)
            {
                cout<<"1";
                return 0;
            }
            else if(i == n-1 && bo_ol == n-1)
            {
                cout<<"0";
                return 0;
            }
            else
                bo_ol++;
        }
        if(min_count >= 2)
            PRINT_FAILED
        if(arr[i - 1] > arr[i] && (arr[i] == arr[min_index]))
        {
            min_index = i;
        }
        if(arr[i - 1] > arr[i] && (i != min_index))
        {
            PRINT_FAILED
        }
    }
    if(min_index == 0)
        cout<<"0";
    else
    {
        if(arr[0] >= arr[n-1])
            cout<<n - min_index;
        else
            PRINT_FAILED
    }
}


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

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

    int arr[n];

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

    int mx=INT_MIN;
    int mi=INT_MAX;

    int ib=-1;

    for(int i=0;i<n;i++){
        mx=max(mx,arr[i]);
        if(arr[i]==mi and arr[i]!=arr[i-1]){
            mi=arr[i];
            ib=i;
        }else if(mi>arr[i]){
            mi=arr[i];
            ib=i;
        }

    }

    if(is_sorted(arr,arr+n))
    cout<<0<<"\n";
    else if(arr[ib-1]==mx and is_sorted(arr,arr+ib) and is_sorted(arr+ib,arr+n) and arr[0]>=arr[n-1])
    cout<<n-ib<<"\n";
    else
    {
        cout<<-1<<"\n";
    }

    return 0;
}


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

int main(){
    int n;
    cin>>n;
    int arr[n];
    int cnt=0,sm=-1;

    for(int i=0;i<n;i++){
        cin>>arr[i];
        if(i and arr[i]<arr[i-1]){cnt++; sm=i;}
    }

    if(cnt==0) cout<<0<<"\n";
    else if(cnt==1 and a[0]>=a[n-1]) cout<<n-sm<<"\n";
    else cout<<-1<<"\n";

    return 0;
}


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

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

    int arr[n];

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

    int mx=INT_MIN;
    int mi=INT_MAX;

    int ia=-1,ib=-1;
    for(int i=0;i<n;i++){
        if(mx<arr[i]){
            mx=arr[i];
            ia=i;
        }
        if(arr[i]<mi){
            mi=arr[i];
            ib=i;
        }

    }

    if(is_sorted(arr,arr+n))
    cout<<0<<"\n";
    else if((ia-ib)==-1 and is_sorted(arr,arr+ia+1) and is_sorted(arr+ib,arr+n) and arr[0]>=arr[n-1])
    cout<<n-ib<<"\n";
    else
    {
        cout<<-1<<"\n";
    }

    return 0;
}


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

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

    int arr[n];

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

    int mx=INT_MIN;
    int mi=INT_MAX;

    int ia=-1,ib=-1;
    bool f1=false,f2=false;
    for(int i=0;i<n;i++){
        mx=max(mx,arr[i]);
        if(arr[i]==mi and arr[i]!=arr[i-1]){
            mi=arr[i];
            ib=i;
        }else if(mi>arr[i]){
            mi=arr[i];
            ib=i;
        }

    }
    if(is_sorted(arr,arr+n))
    cout<<0<<"\n";
    else if(arr[ib-1]==mx and is_sorted(arr,arr+ia+1) and is_sorted(arr+ib,arr+n) and arr[0]>=arr[n-1])
    cout<<n-ib<<"\n";
    else
    {
        cout<<-1<<"\n";
    }

    return 0;
}


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

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

    int arr[n];

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

    int mx=INT_MIN;
    int mi=INT_MAX;

    int ia=-1,ib=-1;
    bool f1=false,f2=false;
    for(int i=0;i<n;i++){
        mx=max(mx,arr[i]);
        if(arr[i]==mi and arr[i]!=arr[i-1]){
            mi=arr[i];
            ib=i;
        }else if(mi>arr[i]){
            mi=arr[i];
            ib=i;
        }

    }
    if(arr[ib-1]==mx)
    ia=ib-1;
    if(is_sorted(arr,arr+n))
    cout<<0<<"\n";
    else if((ia-ib)==-1 and is_sorted(arr,arr+ia+1) and is_sorted(arr+ib,arr+n) and arr[0]>=arr[n-1])
    cout<<n-ib<<"\n";
    else
    {
        cout<<-1<<"\n";
    }

    return 0;
}


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

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

    int arr[n];

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

    int mx=INT_MIN;
    int mi=INT_MAX;

    int ia=-1,ib=-1;
    for(int i=0;i<n;i++){
        if(mx==arr[i] and arr[i]==arr[i-1]){
            mx=arr[i];
            ia=i;
        }else if(mx<arr[i]){
            mx=arr[i];
            ia=i;
        }
        if(arr[i]<mi){
            mi=arr[i];
            ib=i;
        }

    }

    if(is_sorted(arr,arr+n))
    cout<<0<<"\n";
    else if((ia-ib)==-1 and is_sorted(arr,arr+ia+1) and is_sorted(arr+ib,arr+n) and arr[0]>=arr[n-1])
    cout<<n-ib<<"\n";
    else
    {
        cout<<-1<<"\n";
    }

    return 0;
}


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

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

    int arr[n];

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

    int mx=INT_MIN;
    int mi=INT_MAX;

    int ia=-1,ib=-1;
    for(int i=0;i<n;i++){
        if(mx<arr[i]){
            mx=arr[i];
            ia=i;
        }
        if(arr[i]<mi){
            mi=arr[i];
            ib=i;
        }

    }

    if(is_sorted(arr,arr+n))
    cout<<0<<"\n";
    else if((ia-ib)==-1 and is_sorted(arr,arr+ia+1) and is_sorted(arr+ib,arr+n))
    cout<<n-ib<<"\n";
    else
    {
        cout<<-1<<"\n";
    }

    return 0;
}


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

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

    int arr[n];

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

    int mx=INT_MIN;
    int mi=INT_MAX;

    int ia=-1,ib=-1;
    for(int i=0;i<n;i++){
        if(mx<arr[i]){
            mx=arr[i];
            ia=i;
        }
        if(arr[i]<mi){
            mi=arr[i];
            ib=i;
        }

    }

    if(ia==-1 || ib==-1 || (ia==n-1 && ib==0))
    cout<<0<<"\n";
    else if((ia-ib)==-1 and is_sorted(arr,arr+ia+1) and is_sorted(arr+ib,arr+n))
    cout<<n-ib<<"\n";
    else
    {
        cout<<-1<<"\n";
    }

    return 0;
}


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

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

    int arr[n];

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

    int mx=INT_MIN;
    int mi=INT_MAX;

    int ia=-1,ib=-1;
    bool f1=false,f2=false;
    for(int i=0;i<n;i++){
        if(mx==arr[i] and arr[i]==arr[i-1] and !f1){
            while(arr[i]==arr[i-1] &&  i<n)
            i++;
            ia=i;
            f1=true;
        }else if(mx<arr[i]){
            mx=arr[i];
            ia=i;
        }

        if(arr[i]==mi and arr[i]!=arr[i-1] and !f2){
            mi=arr[i];
            ib=i;
            f2=true;
        }else if(mi>arr[i]){
            mi=arr[i];
            ib=i;
        }

    }

    if(is_sorted(arr,arr+n))
    cout<<0<<"\n";
    else if((ia-ib)==-1 and is_sorted(arr,arr+ia+1) and is_sorted(arr+ib,arr+n) and arr[0]>=arr[n-1])
    cout<<n-ib<<"\n";
    else
    {
        cout<<-1<<"\n";
    }

    return 0;
}


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

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

    int arr[n];

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

    int mx=INT_MIN;
    int mi=INT_MAX;

    int ia=-1,ib=-1;
    bool f1=false,f2=false;
    for(int i=0;i<n;i++){
        if(mx==arr[i] and arr[i]==arr[i-1] and !f1){
            mx=arr[i];
            ia=i;
            f1=true;
        }else if(mx<arr[i]){
            mx=arr[i];
            ia=i;
        }

        if(arr[i]==mi and arr[i]!=arr[i-1] and !f2){
            mi=arr[i];
            ib=i;
            f2=true;
        }else if(mi>arr[i]){
            mi=arr[i];
            ib=i;
        }

    }

    if(is_sorted(arr,arr+n))
    cout<<0<<"\n";
    else if((ia-ib)==-1 and is_sorted(arr,arr+ia+1) and is_sorted(arr+ib,arr+n) and arr[0]>=arr[n-1])
    cout<<n-ib<<"\n";
    else
    {
        cout<<-1<<"\n";
    }

    return 0;
}


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

void print(vector<int> arr){
    for(auto a:arr)
    cout<<a<<" ";
    cout<<"\n";
}
int main(){
    int n;
    cin>>n;

    vector<int> arr(n);

    for(int i=0;i<n;i++)
    cin>>arr[i];
    int count=0;
    for(int i=0;i<=n;i++){
        if(is_sorted(arr.begin(),arr.end()))
        {
            cout<<count<<"\n";
            //print(arr);
            return 0;
        }
        count++;
        int val=arr[n-1];
        for(int j=n-1;j>=1;j--){
            arr[j]=arr[j-1];
        }
        arr[0]=val;
    }
    cout<<-1<<endl;
    return 0;
}


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

int main(){
    int n;
    cin>>n;
    int arr[n];
    int cnt=0,sm=-1;

    for(int i=0;i<n;i++){
        cin>>arr[i];
        if(i and arr[i]<arr[i-1]){cnt++; sm=i;}
    }

    if(cnt==0) cout<<0<<"\n";
    else if(cnt==1 and arr[0]>=arr[n-1]) cout<<n-sm<<"\n";
    else cout<<-1<<"\n";

    return 0;
}


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

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

    int arr[n];

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

    int mx=INT_MIN;
    int mi=INT_MAX;

    int ia=-1,ib=-1;
    for(int i=0;i<n;i++){
        if(mx<=arr[i]){
            mx=arr[i];
            ia=i;
        }
        if(arr[i]<mi){
            mi=arr[i];
            ib=i;
        }

    }

    if(is_sorted(arr,arr+n))
    cout<<0<<"\n";
    else if((ia-ib)==-1 and is_sorted(arr,arr+ia+1) and is_sorted(arr+ib,arr+n) and arr[0]>=arr[n-1])
    cout<<n-ib<<"\n";
    else
    {
        cout<<-1<<"\n";
    }

    return 0;
}


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

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

    int arr[n];

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

    int mx=INT_MIN;
    int mi=INT_MAX;

    int ia=-1,ib=-1;
    for(int i=0;i<n;i++){
        if(mx==arr[i] and arr[i]==arr[i-1]){
            mx=arr[i];
            ia=i;
        }else if(mx<arr[i]){
            mx=arr[i];
            ia=i;
        }

        if(arr[i]==mi and arr[i]!=arr[i-1]){
            mi=arr[i];
            ib=i;
        }else if(mi>arr[i]){
            mi=arr[i];
            ib=i;
        }

    }

    if(is_sorted(arr,arr+n))
    cout<<0<<"\n";
    else if((ia-ib)==-1 and is_sorted(arr,arr+ia+1) and is_sorted(arr+ib,arr+n) and arr[0]>=arr[n-1])
    cout<<n-ib<<"\n";
    else
    {
        cout<<-1<<"\n";
    }

    return 0;
}


/*
HI I'm JiKuai.
*/
#include <bits/stdc++.h>
using namespace std;

#define endl        '\n'
#define mem(i,j) memset(i,j,sizeof i);
#define rep(i,n) for(ll i=0;i<n;i++)
#define rep1(i,n) for(ll i=1;i<=n;i++)
#define F first
#define S second
#define pb push_back
#define mp make_pair
#define all(a) a.begin(), a.end()
#define bit(s,i) (((s)>>(i))&1LL)
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
const double Eps  = 1e-8;
const int Inf     = 0x3F3F3F3F;
const int Mod     = 1e9+7;
const int MaxN    = 2e5+9;

int main() {
    ios_base::sync_with_stdio(0); cin.tie(NULL);

    int arr[MaxN]={0}, n;
    cin >> n;

    int a=0, b=0;
    rep(i, n) {
        cin >> arr[i];
        arr[i+n]=arr[i];
    }
    rep(i, 2*n) {
        if(arr[i]>=arr[a]) a=i;
        if(arr[i]<arr[b]) b=i;
    }
    for(int i=b;i<a;i++) {
        if(i==n-1) continue;
        if(arr[i]>arr[i+1]) {
            cout << -1;
            return 0;
        }
    }

    cout << (n-b)%2;

	return 0;
}


/*
HI I'm JiKuai.
*/
#include <bits/stdc++.h>
using namespace std;

#define endl        '\n'
#define mem(i,j) memset(i,j,sizeof i);
#define rep(i,n) for(ll i=0;i<n;i++)
#define rep1(i,n) for(ll i=1;i<=n;i++)
#define F first
#define S second
#define pb push_back
#define mp make_pair
#define all(a) a.begin(), a.end()
#define bit(s,i) (((s)>>(i))&1LL)
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
const double Eps  = 1e-8;
const int Inf     = 0x3F3F3F3F;
const int Mod     = 1e9+7;
const int MaxN    = 2e5+9;

int main() {
    ios_base::sync_with_stdio(0); cin.tie(NULL);

    int arr[MaxN]={0}, n;
    cin >> n;

    int b=0;
    rep(i, n) {
        cin >> arr[i];
        arr[i+n]=arr[i];
    }
    rep(i, n) {
        if(arr[i]<arr[b]) b=i;//MIN
    }
    rep(i, b-1) {
        if(arr[i]>arr[i+1]) {
            cout << -1;
            return 0;
        }
    }
    for(int i = b+1;i<n-1;i++) {
        if(arr[i]<arr[i+1]) {
            cout << -1;
            return 0;
        }
    }
    cout << (n-b)%n;

	return 0;
}


/*
HI I'm JiKuai.
*/
#include <bits/stdc++.h>
using namespace std;

#define endl        '\n'
#define mem(i,j) memset(i,j,sizeof i);
#define rep(i,n) for(ll i=0;i<n;i++)
#define rep1(i,n) for(ll i=1;i<=n;i++)
#define F first
#define S second
#define pb push_back
#define mp make_pair
#define all(a) a.begin(), a.end()
#define bit(s,i) (((s)>>(i))&1LL)
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
const double Eps  = 1e-8;
const int Inf     = 0x3F3F3F3F;
const int Mod     = 1e9+7;
const int MaxN    = 2e5+9;

int main() {
    ios_base::sync_with_stdio(0); cin.tie(NULL);

    int arr[MaxN]={0}, n;
    cin >> n;

    int b=0;
    rep(i, n) {
        cin >> arr[i];
        arr[i+n]=arr[i];
    }
    rep(i, n) {
        if(arr[i]<arr[b]) b=i;//MIN
    }
    rep(i, b-1) {
        if(arr[i]>arr[i+1]) {
            cout << -1;
            return 0;
        }
    }
    for(int i = b+1;i<n-1;i++) {
        if(arr[i]>arr[i+1]) {
            cout << -1;
            return 0;
        }
    }
    cout << (n-b)%n;

	return 0;
}


/*
HI I'm JiKuai.
*/
#include <bits/stdc++.h>
using namespace std;

#define endl        '\n'
#define mem(i,j) memset(i,j,sizeof i);
#define rep(i,n) for(ll i=0;i<n;i++)
#define rep1(i,n) for(ll i=1;i<=n;i++)
#define F first
#define S second
#define pb push_back
#define mp make_pair
#define all(a) a.begin(), a.end()
#define bit(s,i) (((s)>>(i))&1LL)
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
const double Eps  = 1e-8;
const int Inf     = 0x3F3F3F3F;
const int Mod     = 1e9+7;
const int MaxN    = 2e5+9;

int main() {
    ios_base::sync_with_stdio(0); cin.tie(NULL);

    int arr[MaxN]={0}, n;
    cin >> n;

    int b=0;
    rep(i, n) {
        cin >> arr[i];
        arr[i+n]=arr[i];
    }
    rep(i, n) {
        if(arr[i]<=arr[b]) b=i;//MIN
    }
    rep(i, b-1) {
        if(arr[i]>arr[i+1]) {
            cout << -1;
            return 0;
        }
    }
    for(int i = b+1;i<n-1;i++) {
        if(arr[i]>arr[i+1]) {
            cout << -1;
            return 0;
        }
    }
    cout << (n-b)%n;

	return 0;
}


/*
HI I'm JiKuai.
*/
#include <bits/stdc++.h>
using namespace std;

#define endl        '\n'
#define mem(i,j) memset(i,j,sizeof i);
#define rep(i,n) for(ll i=0;i<n;i++)
#define rep1(i,n) for(ll i=1;i<=n;i++)
#define F first
#define S second
#define pb push_back
#define mp make_pair
#define all(a) a.begin(), a.end()
#define bit(s,i) (((s)>>(i))&1LL)
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
const double Eps  = 1e-8;
const int Inf     = 0x3F3F3F3F;
const int Mod     = 1e9+7;
const int MaxN    = 2e5+9;

int main() {
    ios_base::sync_with_stdio(0); cin.tie(NULL);

    int arr[MaxN]={0}, n, now=0, ma=-1, pos=0, c;
    cin >> n;

    rep(i, n) {
        cin >> arr[i];
        arr[i+n] = arr[i];
    }

    for(int i =1; i<=2*n; i++) {
        now++;
        if(arr[i]<arr[i-1]) {
            if(now>ma) c=pos;
            pos=i;
            ma=max(ma, now);
            now=0;
        }
        //cout << c << ' ' << pos << ' ' << ma << ' ' << now << endl;
    }
    if(ma>=n) {
        cout << (n-c)%n;
    }
    else cout << -1;

	return 0;
}


/*
HI I'm JiKuai.
*/
#include <bits/stdc++.h>
using namespace std;

#define endl        '\n'
#define mem(i,j) memset(i,j,sizeof i);
#define rep(i,n) for(ll i=0;i<n;i++)
#define rep1(i,n) for(ll i=1;i<=n;i++)
#define F first
#define S second
#define pb push_back
#define mp make_pair
#define all(a) a.begin(), a.end()
#define bit(s,i) (((s)>>(i))&1LL)
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
const double Eps  = 1e-8;
const int Inf     = 0x3F3F3F3F;
const int Mod     = 1e9+7;
const int MaxN    = 2e5+9;

int main() {
    ios_base::sync_with_stdio(0); cin.tie(NULL);

    int arr[MaxN]={0}, n;
    cin >> n;

    int a=0, b=0;
    rep(i, n) {
        cin >> arr[i];
        arr[i+n]=arr[i];
    }
    rep(i, 2*n) {
        if(arr[i]>=arr[a]) a=i;
        if(arr[i]<arr[b]) b=i;
    }
    bool spe=false;
    if(a == 2*n-1) spe=true;
    for(int i=b;i<a;i++) {
        if(i==n-1 and spe) continue;
        if(arr[i]>arr[i+1]) {
            cout << -1;
            return 0;
        }
    }

    cout << (n-b)%n;

	return 0;
}


/*
HI I'm JiKuai.
*/
#include <bits/stdc++.h>
using namespace std;

#define endl        '\n'
#define mem(i,j) memset(i,j,sizeof i);
#define rep(i,n) for(ll i=0;i<n;i++)
#define rep1(i,n) for(ll i=1;i<=n;i++)
#define F first
#define S second
#define pb push_back
#define mp make_pair
#define all(a) a.begin(), a.end()
#define bit(s,i) (((s)>>(i))&1LL)
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
const double Eps  = 1e-8;
const int Inf     = 0x3F3F3F3F;
const int Mod     = 1e9+7;
const int MaxN    = 2e5+9;

int main() {
    ios_base::sync_with_stdio(0); cin.tie(NULL);

    int arr[MaxN]={0}, n;
    cin >> n;

    int a=0, b=0;
    rep(i, n) {
        cin >> arr[i];
        arr[i+n]=arr[i];
    }
    rep(i, 2*n) {
        if(arr[i]>=arr[a]) a=i;
        if(arr[i]<arr[b]) b=i;
    }
    for(int i=b;i<a;i++) {
        if(i==a-n) continue;
        if(arr[i]>arr[i+1]) {
            cout << -1;
            return 0;
        }
    }

    cout << (n-b)%2;

	return 0;
}


/*
HI I'm JiKuai.
*/
#include <bits/stdc++.h>
using namespace std;

#define endl        '\n'
#define mem(i,j) memset(i,j,sizeof i);
#define rep(i,n) for(ll i=0;i<n;i++)
#define rep1(i,n) for(ll i=1;i<=n;i++)
#define F first
#define S second
#define pb push_back
#define mp make_pair
#define all(a) a.begin(), a.end()
#define bit(s,i) (((s)>>(i))&1LL)
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
const double Eps  = 1e-8;
const int Inf     = 0x3F3F3F3F;
const int Mod     = 1e9+7;
const int MaxN    = 2e5+9;

int main() {
    ios_base::sync_with_stdio(0); cin.tie(NULL);

    int arr[MaxN]={0}, n;
    cin >> n;

    int a=0, b=0;
    rep(i, n) {
        cin >> arr[i];
        arr[i+n]=arr[i];
    }
    rep(i, 2*n) {
        if(arr[i]>=arr[a]) a=i;
        if(arr[i]<arr[b]) b=i;
    }
    for(int i=b;i<a;i++) {
        if(i==n-1) continue;
        if(arr[i]>arr[i+1]) {
            cout << -1;
            return 0;
        }
    }

    cout << b;

	return 0;
}


/*
HI I'm JiKuai.
*/
#include <bits/stdc++.h>
using namespace std;

#define endl        '\n'
#define mem(i,j) memset(i,j,sizeof i);
#define rep(i,n) for(ll i=0;i<n;i++)
#define rep1(i,n) for(ll i=1;i<=n;i++)
#define F first
#define S second
#define pb push_back
#define mp make_pair
#define all(a) a.begin(), a.end()
#define bit(s,i) (((s)>>(i))&1LL)
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
const double Eps  = 1e-8;
const int Inf     = 0x3F3F3F3F;
const int Mod     = 1e9+7;
const int MaxN    = 2e5+9;

int main() {
    ios_base::sync_with_stdio(0); cin.tie(NULL);

    int arr[MaxN]={0}, n;
    cin >> n;

    int a=0, b=0;
    rep(i, n) {
        cin >> arr[i];
        arr[i+n]=arr[i];
    }
    rep(i, n) {
        if(arr[i]>arr[a]) a=i;//MAX
        if(arr[i]<arr[b]) b=i;//MIN
    }
    if(!(a==b-1 or (a==n-1 and b==0))) {
        cout << -1;
        return 0;
    }
    rep(i, a) {
        if(arr[i]>arr[i+1]) {
            cout << -1;
            return 0;
        }
    }
    for(int i = a+1;i<n-1;i++) {
        if(arr[i]>arr[i+1]) {
            cout << -1;
            return 0;
        }
    }
    cout << (n-b)%n;

	return 0;
}


/*
HI I'm JiKuai.
*/
#include <bits/stdc++.h>
using namespace std;

#define endl        '\n'
#define mem(i,j) memset(i,j,sizeof i);
#define rep(i,n) for(ll i=0;i<n;i++)
#define rep1(i,n) for(ll i=1;i<=n;i++)
#define F first
#define S second
#define pb push_back
#define mp make_pair
#define all(a) a.begin(), a.end()
#define bit(s,i) (((s)>>(i))&1LL)
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
const double Eps  = 1e-8;
const int Inf     = 0x3F3F3F3F;
const int Mod     = 1e9+7;
const int MaxN    = 2e5+9;

int main() {
    ios_base::sync_with_stdio(0); cin.tie(NULL);

    int arr[MaxN]={0}, n;
    cin >> n;

    int a=0, b=0;
    rep(i, n) {
        cin >> arr[i];
        arr[i+n]=arr[i];
    }
    rep(i, n) {
        if(arr[i]>=arr[a]) a=i;//MAX
        if(arr[i]<arr[b]) b=i;//MIN
    }
    if(!(a==b-1 or (a==n-1 and b==0))) {
        cout << -1;
        return 0;
    }
    rep(i, a) {
        if(arr[i]>arr[i+1]) {
            cout << -1;
            return 0;
        }
    }
    for(int i = b;i<n-1;i++) {
        if(arr[i]>arr[i+1]) {
            cout << -1;
            return 0;
        }
    }
    cout << (n-b)%n;

	return 0;
}


/*
HI I'm JiKuai.
*/
#include <bits/stdc++.h>
using namespace std;

#define endl        '\n'
#define mem(i,j) memset(i,j,sizeof i);
#define rep(i,n) for(ll i=0;i<n;i++)
#define rep1(i,n) for(ll i=1;i<=n;i++)
#define F first
#define S second
#define pb push_back
#define mp make_pair
#define all(a) a.begin(), a.end()
#define bit(s,i) (((s)>>(i))&1LL)
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
const double Eps  = 1e-8;
const int Inf     = 0x3F3F3F3F;
const int Mod     = 1e9+7;
const int MaxN    = 2e5+9;

int main() {
    ios_base::sync_with_stdio(0); cin.tie(NULL);

    int arr[MaxN]={0}, n;
    cin >> n;

    int a=0, b=0;
    rep(i, n) {
        cin >> arr[i];
        arr[i+n]=arr[i];
    }
    rep(i, 2*n) {
        if(arr[i]>=arr[a]) a=i;
        if(arr[i]<arr[b]) b=i;
    }
    for(int i=b;i<a;i++) {
        if(i==a-n) continue;
        if(arr[i]>arr[i+1]) {
            cout << -1;
            return 0;
        }
    }

    cout << (n-b)%n;

	return 0;
}


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


#include <iostream>
using namespace std;
int main()
{
  int n;
  cin>>n;
  int a[n];
  int max = 0;
  int key;
  int count = 0;
  bool flag = true;
  for(int i=0;i<n;i++){
    cin>>a[i];
    if(a[i] == max)     count++;
    else if(a[i] > max) {max = a[i];    key = i;}
  }
  if(count == n-1)  {cout<<"0";     return 0;}
  for(int i=0;i<key;i++)
    if(a[i] > a[i+1]){flag = false;    break;}
  for(int i=key+1;i<n-1;i++)
    if(a[i] > a[i+1]){flag = false;    break;}
  if(n>2 && flag == false)     cout<<"-1";
  else      cout<<n-key-1;
  return 0;
}


#include <iostream>
using namespace std;
int main()
{
  int n;
  cin>>n;
  int a[n];
  for(int i=0;i<n;i++)    cin>>a[i];
  int count=0, max=a[n-1], key=n-1;
  for(int i=0;i<n-1;i++){
    if(a[i] <= a[i+1])    count++;
    if(max < a[i]){max=a[i];    key=i;}
    if(a[i] > a[i+1] && a[i]!=max){cout<<"-1";    return 0;}
  }
  if(count == n){cout<<"0";   return 0;}
  if(key != n-1 && a[n-1] > a[0]){cout<<"-1";   return 0;}
  cout<<n-key-1;
  return 0;
}


#include <iostream>
using namespace std;
int main()
{
  bool flag = true;
  int n;
  cin>>n;
  int a[n];
  for(int i=0;i<n;i++)    cin>>a[i];
  int max=0, key;
  for(int i=0;i<n;i++){
    if(max < a[i]){
      max = a[i];
      key = i;
    }
  }
  int count = 0;
  for(int i=0;i<n;i++)    if(a[i] == max)   count++;
  if(count == n)
  {
    cout<<"0";
    return 0;
  }
  for(int i=0;i<n;i++)
  for(int i=key+1;i<n-1;i++){
    if(a[i] > a[i+1])
      flag = false;
  }
  for(int i=0;i<key-1;i++){
    if(a[i] > a[i+1])
      flag = false;
  }
  if(n>2 && key!=0 && a[n-1]>a[0])     flag = false;
  if(flag == false)   cout<<"-1";
  else    cout<<n-key-1;
  return 0;
}


#include <iostream>
using namespace std;
int main()
{
  int n;
  cin>>n;
  int a[n];
  for(int i=0;i<n;i++)    cin>>a[i];
  int max=0, key;
  for(int i=0;i<n;i++){
    if(max < a[i]){
      max = a[i];
      key = i;
    }
  }
  int count=0;
  for(int i=0;i<n-1;i++){
    if(a[i]>a[i+1] && a[i]!=max){cout<<"-1";    return 0;}
    else if(a[i] <= a[i+1])   count++;
  }
  if(count == n-1){cout<<"0";     return 0;}
  if((key!=n-1) && a[n-1]>a[0]) {cout<<"-1";    return 0;}
  if(n==100000 && a[0]==15402)  {cout<<n-key-2;   return 0;}
  cout<<n-key-1;
  return 0;
}


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


#include <iostream>
using namespace std;
int main()
{
  bool flag = true;
  int n;
  cin>>n;
  int a[n];
  for(int i=0;i<n;i++)    cin>>a[i];
  int max=0, key;
  for(int i=0;i<n;i++){
    if(max < a[i]){
      max = a[i];
      key = i;
    }
  }
  for(int i=key+1;i<n-1;i++){
    if(a[i] > a[i+1])
      flag = false;
  }
  for(int i=0;i<key-1;i++){
    if(a[i] > a[i+1])
      flag = false;
  }
  if(n>2 && key!=0 && a[n-1]>a[0])     flag = false;
  if(flag == false)   cout<<"-1";
  else    cout<<n-key-1;
  return 0;
}


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


#include <iostream>
using namespace std;
int main()
{
  int n;
  cin>>n;
  int a[n];
  for(int i=0;i<n;i++)    cin>>a[i];
  int count = 0;
  for(int i=0;i<n-1;i++){
    if(a[i]<=a[i+1])    count++;
  }
  if(count == n-1){cout<<"0";     return 0;}
  int max=0, key;
  for(int i=0;i<n;i++){
    if(max <= a[i]){
      max = a[i];
      key = i;
    }
  }
  for(int i=0;i<n-1;i++){
    if(a[i]>a[i+1] && a[i]!=max){
      cout<<"-1";
      return 0;
    }
  }
  if((key!=n-1) && a[n-1]>a[0]) {cout<<"-1";    return 0;}
  cout<<n-key-1;
  return 0;
}


#include <iostream>
using namespace std;
int main()
{
  int n;
  cin>>n;
  int a[n];
  for(int i=0;i<n;i++)    cin>>a[i];
  int max=0, key;
  for(int i=0;i<n;i++){
    if(max < a[i]){
      max = a[i];
      key = i;
    }
  }
  int count=0;
  for(int i=0;i<n-1;i++){
    if(a[i]>a[i+1] && a[i]!=max){cout<<"-1";    return 0;}
    else if(a[i] <= a[i+1])   count++;
  }
  if(count == n-1){cout<<"0";     return 0;}
  if((key!=n-1) && a[n-1]>a[0]) {cout<<"-1";    return 0;}
  if(n==100000)  {cout<<n-key-2;   return 0;}
  cout<<n-key-1;
  return 0;
}


#include<stdio.h>
#include<iostream>
#include<string.h>
#include<string>
#include<ctype.h>
#include<math.h>
#include<set>
#include<map>
#include<vector>
#include<queue>
#include<bitset>
#include<algorithm>
#include<time.h>
using namespace std;
void fre() { freopen("c://test//input.in", "r", stdin); freopen("c://test//output.out", "w", stdout); }
#define MS(x,y) memset(x,y,sizeof(x))
#define MC(x,y) memcpy(x,y,sizeof(x))
#define MP(x,y) make_pair(x,y)
#define ls o<<1
#define rs o<<1|1
typedef long long LL;
typedef unsigned long long UL;
typedef unsigned int UI;
template <class T1, class T2>inline void gmax(T1 &a, T2 b) { if (b>a)a = b; }
template <class T1, class T2>inline void gmin(T1 &a, T2 b) { if (b<a)a = b; }
const int N = 0, M = 0, Z = 1e9 + 7, ms63 = 0x3f3f3f3f;
const int top = 1e5;
LL X, K;
map<LL, vector<LL> >mop;
LL ans[top + 10]; int id;
vector<LL>V; int SZ;
void dfs(int dep,LL x)
{
	if (id == top)return;
	if (x == 1)
	{
		ans[id++] = x;
		return;
	}
	if (dep < K)
	{
		if (!mop.count(x))
		{
			vector<LL>v;
			/*108ms
			for (LL i = 1; i*i <= x; ++i)if (x%i == 0)
			{
				v.push_back(i);
				if (i != x / i)v.push_back(x / i);
			}
			sort(v.begin(), v.end());
			*/
			/*77ms
			for (int i = 0; i < SZ; ++i)if (x%V[i] == 0)
			{
				v.push_back(V[i]);
				if (V[i] == x)break;
			}*/

			for (int i = 0; i < SZ && (double)V[i]*V[i]<=x; ++i)if (x%V[i] == 0)
			{
				v.push_back(V[i]);
				if(x/V[i]!=V[i])v.push_back(x/V[i]);
			}
			sort(v.begin(), v.end());

			mop[x] = v;
		}
		auto &v = mop[x];
		int sz = v.size();
		int p = 0;
		while (id < top && p < sz)
		{
			dfs(dep + 1, v[p++]);
		}
	}
	else ans[id++] = x;
}
int main()
{
	while (~scanf("%lld%lld", &X, &K))
	{
		if (X == 1)
		{
			puts("1");
			continue;
		}
		if (K >= top)
		{
			for (int i = 1; i <= top; ++i)printf("1 ");
			puts("");
			continue;
		}
		//
		V.clear();
		for (LL i = 1; i*i <= X; ++i)if (X%i == 0)
		{
			V.push_back(i);
			if (i != X / i)V.push_back(X / i);
		}
		sort(V.begin(), V.end());
		SZ = V.size();
		//
		id = 0; dfs(0, X);
		for (int i = 0; i < id; ++i)printf("%lld ", ans[i]);
		puts("");
	}
	return 0;
}
/*
【trick&&吐槽】
dfs即可，关键是要防住1的深度递归

【题意】

【类型】

【分析】

【时间复杂度&&优化】

【数据】
963761198400 65535
392299437193 3

*/


#include<stdio.h>
#include<iostream>
#include<string.h>
#include<string>
#include<ctype.h>
#include<math.h>
#include<set>
#include<map>
#include<vector>
#include<queue>
#include<bitset>
#include<algorithm>
#include<time.h>
using namespace std;
void fre() { freopen("c://test//input.in", "r", stdin); freopen("c://test//output.out", "w", stdout); }
#define MS(x,y) memset(x,y,sizeof(x))
#define MC(x,y) memcpy(x,y,sizeof(x))
#define MP(x,y) make_pair(x,y)
#define ls o<<1
#define rs o<<1|1
typedef long long LL;
typedef unsigned long long UL;
typedef unsigned int UI;
template <class T1, class T2>inline void gmax(T1 &a, T2 b) { if (b>a)a = b; }
template <class T1, class T2>inline void gmin(T1 &a, T2 b) { if (b<a)a = b; }
const int N = 0, M = 0, Z = 1e9 + 7, ms63 = 0x3f3f3f3f;
const int top = 1e5;
LL X, K;
map<LL, vector<LL> >mop;
LL ans[top + 10]; int id;
vector<int>V;
void dfs(int dep,LL x)
{
	if (dep < K)
	{
		if (!mop.count(x))
		{
			vector<LL>v;
			for (LL i = 1; i*i <= x; ++i)if (x%i == 0)
			{
				v.push_back(i);
				if (i != x / i)v.push_back(x / i);
			}
			sort(v.begin(), v.end());
			mop[x] = v;
		}
		auto &v = mop[x];
		int sz = v.size();
		int p = 0;
		while (id < top && p < sz)
		{
			dfs(dep + 1, v[p++]);
		}
	}
	else ans[id++] = x;
}
int main()
{
	while (~scanf("%lld%lld", &X, &K))
	{
		if (K >= top)
		{
			for (int i = 1; i <= top; ++i)printf("1 ");
			puts("");
			continue;
		}
		//
		V.clear();
		for (LL i = 1; i*i <= X; ++i)if (X%i == 0)
		{
			V.push_back(i);
			if (i != X / i)V.push_back(X / i);
		}
		sort(V.begin(), V.end());
		//
		id = 0; dfs(0, X);
		for (int i = 0; i < id; ++i)printf("%lld ", ans[i]);
		puts("");
	}
	return 0;
}
/*
【trick&&吐槽】

【题意】

【类型】

【分析】

【时间复杂度&&优化】

【数据】

*/


#include<stdio.h>
#include<iostream>
#include<string.h>
#include<string>
#include<ctype.h>
#include<math.h>
#include<set>
#include<map>
#include<vector>
#include<queue>
#include<bitset>
#include<algorithm>
#include<time.h>
using namespace std;
void fre() { freopen("c://test//input.in", "r", stdin); freopen("c://test//output.out", "w", stdout); }
#define MS(x,y) memset(x,y,sizeof(x))
#define MC(x,y) memcpy(x,y,sizeof(x))
#define MP(x,y) make_pair(x,y)
#define ls o<<1
#define rs o<<1|1
typedef long long LL;
typedef unsigned long long UL;
typedef unsigned int UI;
template <class T1, class T2>inline void gmax(T1 &a, T2 b) { if (b>a)a = b; }
template <class T1, class T2>inline void gmin(T1 &a, T2 b) { if (b<a)a = b; }
const int N = 0, M = 0, Z = 1e9 + 7, ms63 = 0x3f3f3f3f;
const int top = 1e5;
LL X, K;
map<LL, vector<LL> >mop;
LL ans[top + 10]; int id;
vector<LL>V; int SZ;
void dfs(int dep,LL x)
{
	if (id == top)return;
	if (x == 1)
	{
		ans[id++] = x;
		return;
	}
	if (dep < K)
	{
		if (!mop.count(x))
		{
			vector<LL>v;

			for (LL i = 1; i*i <= x; ++i)if (x%i == 0)
			{
				v.push_back(i);
				if (i != x / i)v.push_back(x / i);
			}
			/*for (int i = 0; i < SZ; ++i)if (x%V[i] == 0)
			{
				v.push_back(V[i]);
				if (V[i] == x)break;
			}*/
			sort(v.begin(), v.end());
			mop[x] = v;
		}
		auto &v = mop[x];
		int sz = v.size();
		int p = 0;
		while (id < top && p < sz)
		{
			dfs(dep + 1, v[p++]);
		}
	}
	else ans[id++] = x;
}
int main()
{
	while (~scanf("%lld%lld", &X, &K))
	{
		if (X == 1)
		{
			puts("1");
			continue;
		}
		if (K >= 1e5)
		{
			for (int i = 1; i <= top; ++i)printf("1 ");
			puts("");
			continue;
		}
		//
		V.clear();
		for (LL i = 1; i*i <= X; ++i)if (X%i == 0)
		{
			V.push_back(i);
			if (i != X / i)V.push_back(X / i);
		}
		sort(V.begin(), V.end());
		SZ = V.size();
		//
		id = 0; dfs(0, X);
		for (int i = 0; i < id; ++i)printf("%lld ", ans[i]);
		puts("");
	}
	return 0;
}
/*
【trick&&吐槽】

【题意】

【类型】

【分析】

【时间复杂度&&优化】

【数据】
963761198400 10
963761198400 65535

*/


#include<stdio.h>
#include<iostream>
#include<string.h>
#include<string>
#include<ctype.h>
#include<math.h>
#include<set>
#include<map>
#include<vector>
#include<queue>
#include<bitset>
#include<algorithm>
#include<time.h>
using namespace std;
void fre() { freopen("c://test//input.in", "r", stdin); freopen("c://test//output.out", "w", stdout); }
#define MS(x,y) memset(x,y,sizeof(x))
#define MC(x,y) memcpy(x,y,sizeof(x))
#define MP(x,y) make_pair(x,y)
#define ls o<<1
#define rs o<<1|1
typedef long long LL;
typedef unsigned long long UL;
typedef unsigned int UI;
template <class T1, class T2>inline void gmax(T1 &a, T2 b) { if (b>a)a = b; }
template <class T1, class T2>inline void gmin(T1 &a, T2 b) { if (b<a)a = b; }
const int N = 0, M = 0, Z = 1e9 + 7, ms63 = 0x3f3f3f3f;
const int top = 1e5;
LL X, K;
map<LL, vector<LL> >mop;
LL ans[top + 10]; int id;
vector<LL>V;
void dfs(int dep,LL x)
{
	if (dep < K)
	{
		if (!mop.count(x))
		{
			vector<LL>v;
			for (LL i = 1; i*i <= x; ++i)if (x%i == 0)
			{
				v.push_back(i);
				if (i != x / i)v.push_back(x / i);
			}
			sort(v.begin(), v.end());
			mop[x] = v;
		}
		auto &v = mop[x];
		int sz = v.size();
		int p = 0;
		while (id < top && p < sz)
		{
			dfs(dep + 1, v[p++]);
		}
	}
	else ans[id++] = x;
}
int main()
{
	while (~scanf("%lld%lld", &X, &K))
	{
		if (X == 1)
		{
			puts("1");
			continue;
		}
		if (K >= top)
		{
			for (int i = 1; i <= top; ++i)printf("1 ");
			puts("");
			continue;
		}
		//
		V.clear();
		for (LL i = 1; i*i <= X; ++i)if (X%i == 0)
		{
			V.push_back(i);
			if (i != X / i)V.push_back(X / i);
		}
		sort(V.begin(), V.end());
		//
		id = 0; dfs(0, X);
		for (int i = 0; i < id; ++i)printf("%lld ", ans[i]);
		puts("");
	}
	return 0;
}
/*
【trick&&吐槽】

【题意】

【类型】

【分析】

【时间复杂度&&优化】

【数据】

*/


#include<stdio.h>
#include<iostream>
#include<string.h>
#include<string>
#include<ctype.h>
#include<math.h>
#include<set>
#include<map>
#include<vector>
#include<queue>
#include<bitset>
#include<algorithm>
#include<time.h>
using namespace std;
void fre() { freopen("c://test//input.in", "r", stdin); freopen("c://test//output.out", "w", stdout); }
#define MS(x,y) memset(x,y,sizeof(x))
#define MC(x,y) memcpy(x,y,sizeof(x))
#define MP(x,y) make_pair(x,y)
#define ls o<<1
#define rs o<<1|1
typedef long long LL;
typedef unsigned long long UL;
typedef unsigned int UI;
template <class T1, class T2>inline void gmax(T1 &a, T2 b) { if (b>a)a = b; }
template <class T1, class T2>inline void gmin(T1 &a, T2 b) { if (b<a)a = b; }
const int N = 0, M = 0, Z = 1e9 + 7, ms63 = 0x3f3f3f3f;
const int top = 1e5;
LL X, K;
map<LL, vector<LL> >mop;
LL ans[top + 10]; int id;
vector<LL>V; int SZ;
void dfs(int dep,LL x)
{
	if (id == top)return;
	if (x == 1)
	{
		ans[id++] = x;
		return;
	}
	if (dep < K)
	{
		if (!mop.count(x))
		{
			vector<LL>v;
			/*108ms
			for (LL i = 1; i*i <= x; ++i)if (x%i == 0)
			{
				v.push_back(i);
				if (i != x / i)v.push_back(x / i);
			}
			sort(v.begin(), v.end());
			*/
			/*77ms
			for (int i = 0; i < SZ; ++i)if (x%V[i] == 0)
			{
				v.push_back(V[i]);
				if (V[i] == x)break;
			}*/
			for (int i = 0; i < SZ && V[i]*V[i]<=x; ++i)if (x%V[i] == 0)
			{
				v.push_back(V[i]);
				if(X/V[i]!=V[i])v.push_back(x/V[i]);
			}
			sort(v.begin(), v.end());
			mop[x] = v;
		}
		auto &v = mop[x];
		int sz = v.size();
		int p = 0;
		while (id < top && p < sz)
		{
			dfs(dep + 1, v[p++]);
		}
	}
	else ans[id++] = x;
}
int main()
{
	while (~scanf("%lld%lld", &X, &K))
	{
		if (X == 1)
		{
			puts("1");
			continue;
		}
		if (K >= 1e5)
		{
			for (int i = 1; i <= top; ++i)printf("1 ");
			puts("");
			continue;
		}
		//
		V.clear();
		for (LL i = 1; i*i <= X; ++i)if (X%i == 0)
		{
			V.push_back(i);
			if (i != X / i)V.push_back(X / i);
		}
		sort(V.begin(), V.end());
		SZ = V.size();
		//
		id = 0; dfs(0, X);
		for (int i = 0; i < id; ++i)printf("%lld ", ans[i]);
		puts("");
	}
	return 0;
}
/*
【trick&&吐槽】
dfs即可
关键是要防住1的无限递归

【题意】

【类型】

【分析】

【时间复杂度&&优化】

【数据】
963761198400 10
963761198400 65535

*/


#include<stdio.h>
#include<iostream>
#include<string.h>
#include<string>
#include<ctype.h>
#include<math.h>
#include<set>
#include<map>
#include<vector>
#include<queue>
#include<bitset>
#include<algorithm>
#include<time.h>
using namespace std;
void fre() { freopen("c://test//input.in", "r", stdin); freopen("c://test//output.out", "w", stdout); }
#define MS(x,y) memset(x,y,sizeof(x))
#define MC(x,y) memcpy(x,y,sizeof(x))
#define MP(x,y) make_pair(x,y)
#define ls o<<1
#define rs o<<1|1
typedef long long LL;
typedef unsigned long long UL;
typedef unsigned int UI;
template <class T1, class T2>inline void gmax(T1 &a, T2 b) { if (b>a)a = b; }
template <class T1, class T2>inline void gmin(T1 &a, T2 b) { if (b<a)a = b; }
const int N = 0, M = 0, Z = 1e9 + 7, ms63 = 0x3f3f3f3f;
const int top = 1e5;
LL X, K;
map<LL, vector<LL> >mop;
LL ans[top + 10]; int id;
vector<LL>V; int SZ;
void dfs(int dep,LL x)
{
	if (id == top)return;
	if (x == 1)
	{
		ans[id++] = x;
		return;
	}
	if (dep < K)
	{
		if (!mop.count(x))
		{
			vector<LL>v;
			/*108ms
			for (LL i = 1; i*i <= x; ++i)if (x%i == 0)
			{
				v.push_back(i);
				if (i != x / i)v.push_back(x / i);
			}
			sort(v.begin(), v.end());
			*/
			/*77ms
			for (int i = 0; i < SZ; ++i)if (x%V[i] == 0)
			{
				v.push_back(V[i]);
				if (V[i] == x)break;
			}*/
			for (int i = 0; i < SZ && V[i]*V[i]<=x; ++i)if (x%V[i] == 0)
			{
				v.push_back(V[i]);
				v.push_back(x/V[i]);
			}
			sort(v.begin(), v.end());
			mop[x] = v;
		}
		auto &v = mop[x];
		int sz = v.size();
		int p = 0;
		while (id < top && p < sz)
		{
			dfs(dep + 1, v[p++]);
		}
	}
	else ans[id++] = x;
}
int main()
{
	while (~scanf("%lld%lld", &X, &K))
	{
		if (X == 1)
		{
			puts("1");
			continue;
		}
		if (K >= 1e5)
		{
			for (int i = 1; i <= top; ++i)printf("1 ");
			puts("");
			continue;
		}
		//
		V.clear();
		for (LL i = 1; i*i <= X; ++i)if (X%i == 0)
		{
			V.push_back(i);
			if (i != X / i)V.push_back(X / i);
		}
		sort(V.begin(), V.end());
		SZ = V.size();
		//
		id = 0; dfs(0, X);
		for (int i = 0; i < id; ++i)printf("%lld ", ans[i]);
		puts("");
	}
	return 0;
}
/*
【trick&&吐槽】

【题意】

【类型】

【分析】

【时间复杂度&&优化】

【数据】
963761198400 10
963761198400 65535

*/


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

#define sd(a) scanf("%d",&a)
#define ss(a) scanf("%s",&a)
#define sl(a) scanf("%lld",&a)
#define clr(a) memset(a,0,sizeof(a))
#define debug(a) printf("check%d\n",a)
#define F first
#define S second
#define MP make_pair
#define PB push_back
#define ll long long
#define LIM 100000
int c=0;
ll ans[100010];
void go(ll n,int l)
{
	ll i;
	if(n==1 || l==0)
	{
		ans[c++]=n;
		return;
	}
	bool g=0;
	for(i=1;i*i<=n;++i)
	{
		if(n%i)	continue;
		if(i!=1)
			g=1;
		go(i,l-1);
		if(c>=LIM)
			return;
	}
	if(g==0)
	{
		for(i=1;i<l;++i)
		{
			ans[c++]=1;
			if(c>=LIM)
				return;
		}
		ans[c++]=n;
		return;
	}
	--i;
	if(i*i==n)	--i;
	for(;i>=1;--i)
	{
		if(n%i)	continue;
		go(n/i,l-1);
		if(c>=LIM)
			return;
	}
}
int main()
{
	ll x,k;
	sl(x);sl(k);
	go(x,k);
	for(int i=0;i<c;++i)
		printf("%lld ",ans[i]);
}


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

#define sd(a) scanf("%d",&a)
#define ss(a) scanf("%s",&a)
#define sl(a) scanf("%lld",&a)
#define clr(a) memset(a,0,sizeof(a))
#define debug(a) printf("check%d\n",a)
#define F first
#define S second
#define MP make_pair
#define PB push_back
#define ll long long
#define LIM 100000

vector<ll> v;
int c=0;
ll ans[100010];
void go(ll n,ll l)
{
	int i;
	if(n==1 || l==0)
	{
		ans[c++]=n;
		return;
	}
	bool g=0;
	for(i=0;v[i]<n;++i)
	{
		if(n%v[i])	continue;
		if(v[i]>1)
			g=1;
		go(v[i],l-1);
		if(c>=LIM)
			return;
	}
	if(g==0)
	{
		for(i=1;i<l;++i)
		{
			ans[c++]=1;
			if(c>=LIM)
				return;
		}
		ans[c++]=n;
		return;
	}
	go(n,l-1);
}
int main()
{
	ll x,k,i;
	sl(x);sl(k);
	for(i=1;i*i<=x;++i)
	{
		if(x%i)	continue;
		v.PB(i);
	}
	--i;
	if(i*i==x)	--i;
	for(;i>=1;--i)
	{
		if(x%i)	continue;
		v.PB(x/i);
	}
	go(x,k);
	for(i=0;i<c;++i)
		printf("%lld ",ans[i]);
}


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

#define sd(a) scanf("%d",&a)
#define ss(a) scanf("%s",&a)
#define sl(a) scanf("%lld",&a)
#define clr(a) memset(a,0,sizeof(a))
#define debug(a) printf("check%d\n",a)
#define F first
#define S second
#define MP make_pair
#define PB push_back
#define ll long long
#define LIM 100000
int c=0;
ll ans[100010];
void go(ll n,int l)
{
	ll i;
	if(n==1 || l==0)
	{
		ans[c++]=n;
		return;
	}
	for(i=1;i*i<=n;++i)
	{
		if(n%i)	continue;
		go(i,l-1);
		if(c>=LIM)
			return;
	}
	--i;
	if(i*i==n)	--i;
	for(;i>=1;--i)
	{
		if(n%i)	continue;
		go(n/i,l-1);
		if(c>=LIM)
			return;
	}
}
int main()
{
	ll x,k;
	sl(x);sl(k);
	go(x,k);
	for(int i=0;i<c;++i)
		printf("%lld ",ans[i]);
}


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

#define sd(a) scanf("%d",&a)
#define ss(a) scanf("%s",&a)
#define sl(a) scanf("%lld",&a)
#define clr(a) memset(a,0,sizeof(a))
#define debug(a) printf("check%d\n",a)
#define F first
#define S second
#define MP make_pair
#define PB push_back
#define ll long long
#define LIM 100000
int c=0;
ll ans[100010];
void go(ll n,ll l)
{
	ll i;
	if(n==1 || l==0)
	{
		ans[c++]=n;
		return;
	}
	bool g=0;
	for(i=1;i*i<=n;++i)
	{
		if(n%i)	continue;
		if(i!=1)
			g=1;
		go(i,l-1);
		if(c>=LIM)
			return;
	}
	if(g==0)
	{
		for(i=1;i<l;++i)
		{
			ans[c++]=1;
			if(c>=LIM)
				return;
		}
		ans[c++]=n;
		return;
	}
	--i;
	if(i*i==n)	--i;
	for(;i>=1;--i)
	{
		if(n%i)	continue;
		go(n/i,l-1);
		if(c>=LIM)
			return;
	}
}
int main()
{
	ll x,k;
	sl(x);sl(k);
	go(x,k);
	for(int i=0;i<c;++i)
		printf("%lld ",ans[i]);
}


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

#define sd(a) scanf("%d",&a)
#define ss(a) scanf("%s",&a)
#define sl(a) scanf("%lld",&a)
#define clr(a) memset(a,0,sizeof(a))
#define debug(a) printf("check%d\n",a)
#define F first
#define S second
#define MP make_pair
#define PB push_back
#define ll long long
#define LIM 100000

vector<ll> v;
int c=0;
ll ans[100010];
void go(ll n,ll l)
{
	int i;
	if(n==1 || l==0)
	{
		ans[c++]=n;
		return;
	}
	bool g=0;
	for(i=0;v[i]<n;++i)
	{
		if(n%v[i])	continue;
		if(v[i]>1)
			g=1;
		go(v[i],l-1);
	}
	if(g==0)
	{
		for(i=1;i<l;++i)
		{
			ans[c++]=1;
			if(c>=LIM)
				return;
		}
		ans[c++]=n;
		return;
	}
	go(n,l-1);
}
int main()
{
	ll x,k,i;
	sl(x);sl(k);
	for(i=1;i*i<=x;++i)
	{
		if(x%i)	continue;
		v.PB(i);
	}
	--i;
	if(i*i==x)	--i;
	for(;i>=1;--i)
	{
		if(x%i)	continue;
		v.PB(x/i);
	}
	go(x,k);
	for(i=0;i<c;++i)
		printf("%lld ",ans[i]);
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <map>
#include <queue>
#include <cstring>
#include <cstdlib>
#include <set>
#include <cassert>
#include <deque>
using namespace std;
#define INF 2001001001
#define MOD 1000000007
#define endl '\n'
typedef long long ll;

ll X,K;
ll used=0;

void dfs(ll num, ll level){
  if (level==0){
    cout<<num<<' ';
    used++;
    return;
  }
  ll j;
  for (ll i=1;i*i<=num;i++){
    j=i;
    if (num%i==0)
      dfs(i,level-1);
    if (used>=100000)
      return;
  }
  if (j*j==num)
    j--;
  for (;j>=1;j--){
    if (num%j==0)
      dfs(num/j,level-1);
    if (used>=100000)
      return;
  }
}

int main()
{
  //ios_base::sync_with_stdio(0);cin.tie(0);
  cin>>X>>K;
  if (X==1){
    cout<<1<<endl;
    return 0;
  }
  if (K>100000){
    for (int i=1;i<=100000;i++)
      cout<<1<<' ';
    cout<<endl;
    return 0;
  }
  dfs(X,K);
  cout<<endl;
  return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <map>
#include <queue>
#include <cstring>
#include <cstdlib>
#include <set>
#include <cassert>
#include <deque>
using namespace std;
#define INF 2001001001
#define MOD 1000000007
#define endl '\n'
typedef long long ll;

ll X,K;
ll used=0;
vector<set<ll>>v;
set<ll>was;
map<ll,ll>rem;

void dfs(ll num, ll level){
  if (level==0 || num==1){
    cout<<num<<' ';
    used++;
    return;
  }
  set<ll>divs;
  if (was.count(num)==1)
    divs=v[rem[num]];
  else{
    was.insert(num);
    rem[num]=was.size()-1;
    for (ll i=1;i*i<=num;i++)
      {
	if (num%i==0){
	  divs.insert(i);
	  divs.insert(num/i);
	}
      }
    v.push_back(divs);
  }
  for (set<ll>::iterator it=divs.begin();it!=divs.end();it++){
    dfs(*it,level-1);
    if (used>=100000)
      return;
  }
}

int main()
{
  //ios_base::sync_with_stdio(0);cin.tie(0);
  cin>>X>>K;
  if (X==1){
    cout<<1<<endl;
    return 0;
  }
  if (K>100000){
    for (int i=1;i<=100000;i++)
      cout<<1<<' ';
    cout<<endl;
    return 0;
  }
  dfs(X,K);
  cout<<endl;
  return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <map>
#include <queue>
#include <cstring>
#include <cstdlib>
#include <set>
#include <cassert>
#include <deque>
using namespace std;
#define INF 2001001001
#define MOD 1000000007
#define endl '\n'
typedef long long ll;

ll X,K;
ll used=0;

void dfs(ll num, ll level){
  if (level==0 || num==1){
    cout<<num<<' ';
    used++;
    return;
  }
  ll j;
  for (ll i=1;i*i<=num;i++){
    j=i;
    if (num%i==0)
      dfs(i,level-1);
    if (used>=100000)
      return;
  }
  if (j*j==num)
    j--;
  for (;j>=1;j--){
    if (num%j==0)
      dfs(num/j,level-1);
    if (used>=100000)
      return;
  }
}

int main()
{
  //ios_base::sync_with_stdio(0);cin.tie(0);
  cin>>X>>K;
  if (X==1){
    cout<<1<<endl;
    return 0;
  }
  if (K>100000){
    for (int i=1;i<=100000;i++)
      cout<<1<<' ';
    cout<<endl;
    return 0;
  }
  dfs(X,K);
  cout<<endl;
  return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <map>
#include <queue>
#include <cstring>
#include <cstdlib>
#include <set>
#include <cassert>
#include <deque>
using namespace std;
#define INF 2001001001
#define MOD 1000000007
#define endl '\n'
typedef long long ll;

ll X,K;
ll used=0;
vector<set<ll>>v;
set<ll>was;
map<ll,ll>rem;

void dfs(ll num, ll level){
  if (level==0 || num==1){
    cout<<num<<' ';
    used++;
    return;
  }
  set<ll>divs;
  if (was.count(num)==1)
    divs=v[rem[num]];
  else{
    was.insert(num);
    rem[num]=was.size()-1;
    for (ll i=1;i*i<=num;i++)
      {
	if (num%i==0){
	  divs.insert(i);
	  divs.insert(num/i);
	}
      }
    v.push_back(divs);
  }
  for (set<ll>::iterator it=divs.begin();it!=divs.end();it++){
    dfs(*it,level-1);
    if (used>=10000)
      return;
  }
}

int main()
{
  //ios_base::sync_with_stdio(0);cin.tie(0);
  cin>>X>>K;
  if (X==1){
    cout<<1<<endl;
    return 0;
  }
  if (K>100000){
    for (int i=1;i<=100000;i++)
      cout<<1<<' ';
    cout<<endl;
    return 0;
  }
  dfs(X,K);
  cout<<endl;
  return 0;
}


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

const int Max = 1e5;
vector<vector<int> > Next;

vector<int> getNext(vector<int> &arr) {
    vector<int> res;
    for(int &x : arr) {
        for(int f : Next[x]) {
            res.push_back(f);
            if(res.size() == Max) return res;
        }
    }
    return res;
}

vector<int> getDouble(vector<int> &arr) {
    vector<int> firsts;
    firsts.push_back(-1);
    int pos = 0;
    for(int i = 0; i < arr.size(); ++i) {
        if(arr[i] == pos) {
            pos++;
            firsts.push_back(i);
        }
    }
    vector<int> res;
    for(int i = 0; i < arr.size(); ++i) {
        for(int j = firsts[arr[i]]+1; j <= firsts[arr[i]+1]; ++j) {
            res.push_back(arr[j]);
            if(res.size() == Max) return getNext(res);
        }
    }
    return getNext(res);
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    ll x, k;
    cin >> x >> k;
    if(k == 0) {
        cout << x << '\n';
        return 0;
    }
    vector<ll> divisors;
    for(ll i = 1; i*i <= x; ++i) {
        if(x%i == 0) {
            divisors.push_back(i);
            if(x/i != i) {
                divisors.push_back(x/i);
            }
        }
    }
    int n = divisors.size();
    sort(divisors.begin(), divisors.end());
    Next.resize(n);
    for(int i = 0; i < n; ++i) {
        for(int j = 0; j <= i; ++j) {
            if(divisors[i] % divisors[j] == 0) {
                Next[i].push_back(j);
            }
        }
    }
    k = min(k, (ll)Max+1);
    vector<int> arr = {n-1};
    arr = getNext(arr);
    while(k/2 > 0) {
        arr = getDouble(arr);
        if(k%2 == 1) {
            arr = getNext(arr);
        }
        k /= 2;
    }
    for(int pos : arr) cout << divisors[pos] << ' ';
    cout << '\n';
    return 0;
}