#include <iostream>
using ll = long long;
using namespace std;

/* initialize */
int temp, n;
/* main */
int partition(int A[],int p,int r){
    int x = A[r];
    int i = p-1;
    for (int j = p; j < r; j++) {
        if (A[j] <= x){
            i++;
            temp = A[i];
            A[i] = A[j];
            A[j] = temp;
        }
    }
    temp = A[i+1];
    A[i+1] = A[r];
    A[r] = temp;
    return i+1;
}

int main() {
    int q;
    cin >> n;
    int A[100000];
    for (int j = 0; j < n; ++j) {
        scanf("%d", &A[j]);
    }
    q = partition(A, 0, n-1);
    for (int k = 0; k < n; k++) {
        if(k) printf(" ");
        if (k == q) printf("[%d]", A[k]);
        else printf("%d", A[k]);
    }
    cout << "\n";
    return 0;
}

#include <iostream>

using namespace std;

const long MOD = 1000000007;
typedef pair<int, int> P;
typedef long long ll;

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

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

    int B[N];
    long ans = 0;
    for(int i=0; i<N; i++){
        if(i-A[i]<0){
            cout << -1 << endl;
            return 0;
        }
        B[i] = i - A[i];
    }

    for(int i=1; i<N; i++){
        if(B[i]<B[i-1]) {
            cout << -1 << endl;
            return 0;
        }
        if(B[i-1]<B[i])ans += (i-1) - B[i-1];
    }
    ans += (N-1)-B[N-1];
    cout << ans << endl;

    return 0;
}
