#include <bits/stdc++.h>
using namespace std;
using lint     = long long;
const lint inf = 1LL << 60;
const lint mod = 1000000007;

// 0-indexed bottom up Segment Tree
// UNIT is the identity element of operation func
template <typename T = int>
struct SegmentTree {
    using F = function<T(T, T)>;
    int n;
    vector<T> dat;
    F func;
    T UNIT;

    SegmentTree(int n_, F func_, T UNIT_) : func(func_), UNIT(UNIT_) {
        n = 1;
        // full binary tree: num of leaves = n = 2^k >= n_
        while (n < n_)
            n *= 2;
        dat.assign(2 * n - 1, UNIT);
    }
    SegmentTree(vector<T> v_, F func_, T UNIT_) : func(func_), UNIT(UNIT_) {
        n      = 1;
        int nv = v_.size();
        while (n < nv)
            n *= 2;
        dat.assign(2 * n - 1, UNIT);
        for (int i = 0; i < nv; ++i) {
            dat[n - 1 + i] = v_[i];
        }
        for (int i = n - 2; i >= 0; --i) {
            dat[i] = func(dat[2 * i + 1], dat[2 * i + 2]);
        }
    }
    void update(int k, T a) {
        // leaves are at index n-1 to 2*n-2
        k += n - 1;
        dat[k] = a;
        while (k > 0) {
            // k -> parent node
            k = (k - 1) / 2;
            // func(child nodes)
            dat[k] = func(dat[2 * k + 1], dat[2 * k + 2]);
        }
    }
    // get result of func() in [l, r)
    T query(int l, int r) {
        l += n - 1;
        r += n - 1;
        T retl = UNIT, retr = UNIT;
        while (l < r) {
            if ((l & 1) == 0)
                retl = func(retl, dat[l]);
            if ((r & 1) == 0)
                retr = func(dat[r - 1], retr);
            l = l / 2;
            r = (r - 1) / 2;
        }
        return func(retl, retr);
    }
};
auto f = [](pair<int, int> l, pair<int, int> r) {
    if (l.first == r.first) {
        return min(l, r);
    } else
        return max(l, r);
};
const pair<int, int> unit = {-100000, 100000};

int main() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    lint n;
    cin >> n;
    vector<lint> a(n);
    vector<pair<int, int>> in(n);
    for (int i = 0; i < n; ++i) {
        cin >> a[i];
        a[i]--;
        in[a[i]].first++;
        in[i].second = i;
    }
    SegmentTree<pair<int, int>> seg(in, f, unit);
    set<int> unused;
    for (int i = 0; i < n; ++i) {
        unused.insert(i);
    }
    vector<int> ret;
    int ng = -1;
    for (int i = 0; i < n - 5; ++i) {
        auto mx = seg.query(0, n);
        // if (mx.first >= n - i - 1 && ng == mx.second) {
        //     cout << -1 << "\n";
        //     return 0;
        // }
        int j = 100000000;
        // if (mx.first >= n - i - 1) {
        //     j = mx.second;
        // } else if (mx.first == n - i - 2) {
        //     for (auto &u : unused) {
        //         if (ng != u && a[u] != mx.second) {
        //             j = u;
        //             break;
        //         }
        //         if (ng != mx.second)
        //             j = min(j, mx.second);
        //     }
        // } else {
        auto it = unused.begin();
        if (*it == ng)
            it = next(it);
        j = *it;
        if (mx.first == n - i - 1)
            j = mx.second;
        // }
        ret.push_back(j);
        unused.erase(j);
        ng = a[j];
        seg.update(j, {-1, j});
        int k  = a[j];
        auto u = seg.query(k, k + 1);
        seg.update(u.second, {u.first - 1, u.second});
    }
    vector<int> rest;
    for (auto &u : unused) {
        rest.push_back(u);
    }
    do {
        bool ok = true;
        int ngg = ng;
        for (int i = 0; i < (int)rest.size(); ++i) {
            if (ngg == rest[i])
                ok = false;
            ngg = a[rest[i]];
        }
        if (ok) {
            for (auto &r : rest) {
                ret.push_back(r);
            }
            break;
        }
    } while (next_permutation(rest.begin(), rest.end()));
    if ((int)ret.size() != n) {
        cout << -1 << "\n";
        return 0;
    }
    for (int i = 0; i < n; ++i) {
        if (i)
            cout << " ";
        cout << ret[i] + 1;
    }
    cout << "\n";
    return 0;
}

Measured memory_bytes: 9200