#include <bits/stdc++.h>
using namespace std;
 
int main(){
    int n, k = 0;
    cin >> n;
    int p[n + 1], c[n + 1] = {}, a[n + 1] = {}, gc = 0, ga = 0;
    vector<pair<int, int>> v;
    for(int i = 1; i <= n; i++){
        cin >> p[i];
        if(p[i] == i) k = -1;
        else if(p[i] > i){
            c[p[i]]--;
            c[i]++;
        }
        else{
            a[p[i]]++;
            a[i]--;
        }
        v.push_back(make_pair(p[i], i));
    }
    for(int i = 1; i < n; i++){
        gc += c[i];
        ga += a[i];
        if(ga * gc != 1) k = -1;
    }
    if(k == -1) cout << k << endl;
    else{
        sort(v.begin(), v.end());
        bool b[n] = {};
        for(int i = 0; i < n; i++){
            if(v[i].first < v[i].second){
                for(int j = v[i].second - 1; j >= v[i].first; j--){
                    if(b[j] == 0){
                        cout << j << endl;
                        b[j] = 1;
                    }
                }
            }
            else{
                for(int j = v[i].second; j < v[i].first; j++){
                    if(b[j] == 0){
                        cout << j << endl;
                        b[j] = 1;
                    }
                }
            }
        }
    }
}
#include "bits/stdc++.h"
#include <chrono>
#include <random>
#define lli long long int
using namespace std;
#define mod 1000000007
#define mod1 998244353
#define fastio ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL); 
#define INF 1000000000
#define common cout << "Case #" << w+1 << ": "  
#define MAXN 200000


vector<int>g;
void comb(vector<pair<int,int> >&cnt,int ind,int ans)
{
    if(ind==cnt.size())
    {
        g.push_back(ans);
        return;
    }
    int p=1;
    for(int i=0;i<=cnt[ind].second;i++)
    {
        comb(cnt,ind+1,ans*p);
        p*=cnt[ind].first;
    }
}

unordered_map<int,int> primefactors(int n)  
{
    int f=0;
    unordered_map<int,int>m;
    while (n % 2 == 0)  
    {  
        f+=1;  
        n = n/2;  
    }  
    if(f>0)
    {
        m[2]=f;
        f=0;
    } 
    for (int i = 3; i <= sqrt(n); i = i + 2)  
    {   
        while (n % i == 0)  
        {  
            f+=1;  
            n = n/i;  
        } 
        if(f>0)
        {
            m[i]=f;
            f=0;
        }  
    }  
    if (n > 2)  
        m[n]=1; 
    return m;
}

map<int,vector<int> >m;

void func(int num)
{
    for(int i=0;i<g.size();i++)
        m[g[i]].push_back(num/g[i]);
    g.clear();
}

int gcd(int a,int b)
{
    if(b>a)
        swap(a,b);
    if(a%b==0)
        return b;
    return gcd(b,a%b);
}

int main()
{
    fastio;
    int n;
    cin >> n;
    vector<int>v(n);
    for(int i=0;i<n;i++)
        cin >> v[i];
    sort(v.begin(),v.end());
    vector<vector<int> >vimp;
    for(int i=0;i<n;i++)
    {
        unordered_map<int,int>um=primefactors(v[i]);
        vector<pair<int,int> >t;
        unordered_map<int,int>:: iterator it;
        for(it=um.begin();it!=um.end();it++)
            t.push_back({it->first,it->second});
        comb(t,0,1);
        vimp.push_back(g);
        func(v[i]);
    }
    map<int,vector<int> >:: iterator it;
    map<int,int>mgcd;
    for(it=m.begin();it!=m.end();it++)
    {
        vector<int>t=it->second;
        int a=t[0];
        for(int i=1;i<t.size();i++)
            a=gcd(a,t[i]);
        mgcd[it->first]=a;
    }
    set<int>found;
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<vimp[i].size();j++)
        {
            if(vimp[i][j]<=v[0] and mgcd[vimp[i][j]]==1)
                found.insert(vimp[i][j]);
        }
    }
    cout << found.size() << "\n";
}