/*
  Compete against Yourself.
  Author - Aryan Choudhary (@aryanc403)
*/

#pragma GCC optimize ("Ofast")
#pragma GCC target ("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma GCC optimize ("-ffloat-store")

#include<iostream>
#include<bits/stdc++.h>
using namespace std;
#define fo(i,n)   for(i=0;i<(n);++i)
#define repA(i,j,n)   for(i=(j);i<=(n);++i)
#define repD(i,j,n)   for(i=(j);i>=(n);--i)
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()
#define pb push_back
#define mp make_pair
#define X first
#define Y second
#define endl "\n"

typedef long long int lli;
typedef long double mytype;
typedef pair<lli,lli> ii;
typedef vector<ii> vii;
typedef vector<lli> vi;

clock_t time_p=clock();
void aryanc403()
{
#ifdef ARYANC403
    time_p=clock()-time_p;
    cerr<<"Time Taken : "<<(float)(time_p)/CLOCKS_PER_SEC<<"\n";
#endif
}

#ifdef ARYANC403
#include "/home/aryan/codes/PastCodes/header.h"
#else
    #define dbg(args...)
#endif

const lli INF = 0xFFFFFFFFFFFFFFFL;

lli seed;
mt19937 rng(seed=chrono::steady_clock::now().time_since_epoch().count());
inline lli rnd(lli l=0,lli r=INF)
{return uniform_int_distribution<lli>(l,r)(rng);}

class CMP
{public:
bool operator()(ii a , ii b) //For min priority_queue .
{    return ! ( a.X < b.X || ( a.X==b.X && a.Y <= b.Y ));   }};

void add( map<lli,lli> &m, lli x,lli cnt=1)
{
    auto jt=m.find(x);
    if(jt==m.end())         m.insert({x,cnt});
    else                    jt->Y+=cnt;
}

void del( map<lli,lli> &m, lli x,lli cnt=1)
{
    auto jt=m.find(x);
    if(jt->Y<=cnt)            m.erase(jt);
    else                      jt->Y-=cnt;
}

bool cmp(const ii &a,const ii &b)
{
    return a.X<b.X||(a.X==b.X&&a.Y<b.Y);
}

const lli mod = 1000000007L;
// const lli maxN = 1000000007L;

    lli T,n,i,j,k,in,cnt,l,r,u,v,x,y;
    lli m;
    set<lli> s;
    set<ii> st;
    vi a,b,c;
    //priority_queue < ii , vector < ii > , CMP > pq;// min priority_queue .

void solve()
{
b.pb(0);

// if(-st.begin()->X==n-1)
// {
//     lli v=st.begin()->Y;
//     b.pb(v);
//     st.erase({-c[a[v]],a[v]});
//     st.erase({-c[v],v});
//     c[a[v]]--;
//     st.insert({-c[a[v]],a[v]});
//     s.erase(v);
// }
dbg(b);
dbg(st);
// return;
lli u=0;

const lli dr=1;//rnd(0,10);

while(sz(s)>3&&!s.empty())
{
    dbg(u,b,s);
    dbg(*st.begin(),sz(st),sz(b),n);
    auto it=s.upper_bound(u);
    bool fl=true,gl=true;


    if((dr&&-st.begin()->X>0)||(!dr&&-st.begin()->X>=0))
    if((-st.begin()->X)+sz(b)==n)
    {
        lli v=st.begin()->Y;
        b.pb(v);
        if(v==a[b.back()])
            gl=false;
        else
        {
            st.erase({-c[a[v]],a[v]});
            st.erase({-c[v],v});
            c[a[v]]--;
            if(s.find(a[v])!=s.end())
                st.insert({-c[a[v]],a[v]});
            s.erase(v);
            continue;
        }
    }

    while(gl&&fl&&it!=s.end())
    {
        if(*it==a[b.back()])
        {
            ++it;
            continue;
        }

        lli v=*it;
        s.erase(it);
        b.pb(v);
        st.erase({-c[a[v]],a[v]});
        st.erase({-c[v],v});
        c[a[v]]--;
        
        if(s.find(a[v])!=s.end())
        {
            st.insert({-c[a[v]],a[v]});
            dbg(v,a[v],*s.find(a[v]));
        }
        u=0;
        fl=false;
        break;
    }

    if(gl&&!fl&&it==s.end())
    {
        dbg(fl);
        cout<<-1<<endl;
        exit(0);
    }

    if(!gl||fl)
    {
        s.insert(u=b.back());
        lli v=u;
        st.erase({-c[a[v]],a[v]});
        c[a[v]]++;
        st.insert({-c[v],v});
        if(s.find(a[v])!=s.end())
            st.insert({-c[a[v]],a[v]});
        b.pop_back();
    }
}

vi d;
for(auto x:s)
    d.pb(x);
sort(all(d));

do{
    bool fl=false;

    for(auto x:d)
    {
        if(a[b.back()]==x)
            fl=true;
        b.pb(x);
    }

    if(!fl)
        break;
    
    b.pop_back();
    b.pop_back();
    b.pop_back();


}while(next_permutation(all(d)));

dbg(b);
}

int main(void) {
    ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
    // freopen("txt.in", "r", stdin);
    // freopen("txt.out", "w", stdout);
// cout<<std::fixed<<std::setprecision(35);
// cin>>T;while(T--)
{

    cin>>n;

    if(n==2)
    {
        cout<<-1<<endl;
        return 0;
    }

    a.clear();a.reserve(n);
    a.pb(0);
    c.resize(n+1,0);

    repA(i,1,n)
    {
        // in=(i%n)+1;
        // if(i<=n-20)
        //     in=n-1;
        // else
        //     in=n-i+1;
        cin>>in;
        a.pb(in);
        c[in]++;
        s.insert(i);
    }

    repA(i,1,n)
        st.insert({-c[i],i});

    solve();
    repA(i,1,n)
        cout<<b[i]<<" ";
    cout<<endl;
}   aryanc403();
    return 0;
}


Measured memory_bytes: 13296