#include "bits/stdc++.h"
#define ll long long
#define lld long double
#define MOD 1000000007
#define CAP 10000000
#define inf 1000000000000000000LL
#define pii pair<ll,ll>
#define f first
#define s second
#define pb push_back
#define mp make_pair
#define sz(v) v.size()
#define all(v)    v.begin(),v.end()
#define fast  ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
using namespace std;
ll power(ll x,ll y, ll md=MOD){ll res = 1;x%=md;while(y>0){if(y&1)res = (res*x)%md;x = (x*x)%md;y = y>>1;}return res%md;}

#define int ll
// #define endl '\n'

signed main() {
	// fast;

	int n;
	cin>>n;

	vector<int> v(n);
	for(int i=0;i<n;i++) cin>>v[i], v[i]--;

	int st = -1;
	vector<int> ans;
	set<int> set1;
	for(int i=0;i<n;i++) set1.insert(i);
	// ans.pb(0);

	int hate[n] = {};
	for(int i=0;i<n;i++) hate[v[i]]++;

	set<pii> hatred;
	for(int i=0;i<n;i++) hatred.insert({hate[i],i});

	for(int i=0;i<n;i++) {
		if(i==n-3) {

			auto it1 = prev(hatred.end());
		// if(i==n-1) cout<<"$$$$"<<(*it1).f<<" "<<(*it1).s<<endl;
		if((*it1).f>n-i-1) break;
		int temp=-1;
		// cout<<"$"<<(*it1).f<<" "<<n-i-1<<endl;
		if((*it1).f==n-i-1 && v[st]!=(*it1).s) {
			if(v[st]==(*it1).s) break;
			temp = (*it1).s;
		}


			int c1 = *set1.begin();
			int c2 = *next(set1.begin());
			int c3 = *next(next(set1.begin()));

			if(v[st]!=c1&&v[c1]!=c2&&v[c2]!=c3) {
				ans.pb(c1);assert(temp==-1||temp==c1); ans.pb(c2); ans.pb(c3);
			}
			else if(v[st]!=c1&&v[c1]!=c3&&v[c3]!=c2) {
				ans.pb(c1);assert(temp==-1||temp==c1); ans.pb(c3); ans.pb(c2);
			}
			else if(v[st]!=c2&&v[c2]!=c1&&v[c1]!=c3) {
				ans.pb(c2);assert(temp==-1||temp==c2); ans.pb(c1); ans.pb(c3);
			}
			else if(v[st]!=c2&&v[c2]!=c3&&v[c3]!=c1) {
				ans.pb(c2);assert(temp==-1||temp==c2); ans.pb(c3); ans.pb(c1);
			}
			else if(v[st]!=c3&&v[c3]!=c1&&v[c1]!=c2) {
				ans.pb(c3);assert(temp==-1||temp==c3); ans.pb(c1); ans.pb(c2);
			}
			else if(v[st]!=c3&&v[c3]!=c2&&v[c2]!=c1) {
				ans.pb(c3);assert(temp==-1||temp==c3); ans.pb(c2); ans.pb(c1);
			}

			hate[v[c1]]--;
			hate[v[c2]]--;
			hate[v[c3]]--;
			if(set1.count(c1)) assert(hate[v[c1]]==0);
			if(set1.count(c2)) assert(hate[v[c2]]==0);
			if(set1.count(c3)) assert(hate[v[c3]]==0);

			break;
		}
		// cout<<"----\n"; for(auto x: hatred)cout<<x.f<<" "<<x.s<<endl;
		auto it1 = prev(hatred.end());
		// if(i==n-1) cout<<"$$$$"<<(*it1).f<<" "<<(*it1).s<<endl;
		if((*it1).f>n-i-1) break;
		// cout<<"$"<<(*it1).f<<" "<<n-i-1<<endl;
		if((*it1).f==n-i-1 && v[st]!=(*it1).s) {
			if(v[st]==(*it1).s) break;
			st = (*it1).s;
			hatred.erase(it1);

			set1.erase(st);
			ans.pb(st);
			// cout<<">>"<<i<<" "<<st<<endl;

			hatred.erase({hate[v[st]],v[st]});
			hate[v[st]]--;
			if(set1.count(v[st])) hatred.insert({hate[v[st]],v[st]});

			continue;
		}
		// cout<<"#"<<st<<endl;

		auto it = set1.begin();
		while(it!=set1.end()) {
			int ptr = *it;
			if(st!=-1&&v[st]==ptr) it++;
			else break;
		}
		if(it==set1.end()) break;
		// cout<<">"<<i<<" "<<*it<<endl;

		st = *it;
		// if(*it==1) cout<<hate[v[st]]<<","<<v[st]<<endl;
		hatred.erase({hate[v[st]],v[st]});
		hate[v[st]]--;
		if(set1.count(v[st])) hatred.insert({hate[v[st]],v[st]});
		hatred.erase({hate[st],st});
		set1.erase(it);
		ans.pb(st);
	}

	if(ans.size()!=n) {
		cout<<-1;
		return 0;
	}
	for(auto x: ans) cout<<x+1<<" ";
}

Measured memory_bytes: 13312