Language: C++

#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <ctime>
#include <cmath>

#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <iostream>

#define X first
#define Y second
#define PB push_back
#define TRACE(x) printf("%s = %d\n", #x, x);

/*

           _   _ _____ __  __          _      _      _      _      
     /\   | \ | |_   _|  \/  |   /\   | |    | |    | |    | |     
    /  \  |  \| | | | | \  / |  /  \  | |    | |    | |    | |     
   / /\ \ | . ` | | | | |\/| | / /\ \ | |    | |    | |    | |     
  / ____ \| |\  |_| |_| |  | |/ ____ \| |____| |____| |____| |____ 
 /_/    \_\_| \_|_____|_|  |_/_/    \_\______|______|______|______|
                                                                   
                                                                   
                          ..,co88oc.oo8888cc,..
  o8o.               ..,o8889689ooo888o"88888888oooc..
.88888             .o888896888".88888888o'?888888888889ooo....
a888P          ..c6888969""..,"o888888888o.?8888888888"".ooo8888oo.
088P        ..atc88889"".,oo8o.86888888888o 88988889",o888888888888.
888t  ...coo688889"'.ooo88o88b.'86988988889 8688888'o8888896989^888o
 888888888888"..ooo888968888888  "9o688888' "888988 8888868888'o88888
  ""G8889""'ooo888888888888889 .d8o9889""'   "8688o."88888988"o888888o .
           o8888'""""""""""'   o8688"          88868. 888888.68988888"o8o.
           88888o.              "8888ooo.        '8888. 88888.8898888o"888o.
           "888888'               "888888'          '""8o"8888.8869888oo8888o .
      . :.:::::::::::.: .     . :.::::::::.: .   . : ::.:."8888 "888888888888o
                                                        :..8888,. "88888888888.
                                                        .:o888.o8o.  "866o9888o
                                                         :888.o8888.  "88."89".
                                                        . 89  888888    "88":.
                    atc                                 :.     '8888o
                                                         .       "8888..
                                                                   888888o.
                                                                    "888889,
                                                             . : :.:::::::.: :.

------------------------------------------------
Thank you for visiting https://asciiart.website/
This ASCII pic can be found at
https://asciiart.website/index.php?art=animals/cats


*/

using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair < int, int > pii;
typedef pair < ll, ll > pll;
typedef vector < int > vi;
typedef vector < ll > vl;
typedef vector < pii > vp;
typedef set < int > si;
typedef set < ll > sl;
typedef set < pii > sp;
typedef pair < ld, ld > pt;

const int N = 1e5 + 500;
const int M = 1e6 + 500;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
const int LOG = 20;
const int ALP = 30;
const int BASE = 31337;
const double EPS = 1e-9;
const double PI = 3.1415926535;

set < int > S;
set < pii > S2;
vector < int > sol;
vector < int > pos;
int a[N], n, cnt[N];

int main(){
	scanf("%d", &n);
	for(int i = 1;i <= n;i++)
		scanf("%d", a + i), S.insert(i);
	int ne = 0;
	for(int i = 0;i < n;i++){
		if(S.size() <= 11)
			{break;}
		if(*S.begin() != ne){
			sol.PB(*S.begin());
			ne = a[sol.back()];
			S.erase(*S.begin());
		}
		else{
			int rmb = *S.begin(); S.erase(S.begin());
			sol.PB(*S.begin());
			ne = a[sol.back()];
			S.erase(*S.begin());
			S.insert(rmb);
		}
	}
	vector < int > kraj;
	for(int x : S) kraj.PB(x);
	do{
		if(kraj[0] == ne) continue;
		int moz = 1;
		for(int i = 0;i + 1 < (int)kraj.size();i++)
			if(a[kraj[i]] == kraj[i + 1]) moz = 0;
		if(moz){
			if((int)pos.size() == 0 || kraj < pos)
				pos = kraj;
		}
	} while(next_permutation(kraj.begin(), kraj.end()));
	S.clear(); sol.clear();
	if((int)pos.size() + (int)sol.size() != n){
		for(int i = 1;i <= n;i++){
			S.insert(i); // v[a[i]].PB(i);
			cnt[a[i]]++;
		}
		for(int i = 1;i <= n;i++) S2.insert({cnt[i], i});
		int ne = 0;
		for(int i = 0;i < n;i++){
			int nov;
			if(S2.rbegin() -> X != n - i - 1){
				nov = *S.begin();
				if(nov == ne)
					nov = *(++S.begin());
			}
			else{
				nov = S2.rbegin() -> Y;	
			}
			//printf("nov = %d\n", nov);
			sol.PB(nov); ne = a[nov];
			S2.erase({cnt[nov], nov});
			S.erase(nov); 
			if(S.count(a[nov]))
				smanji(a[nov]);
		}
		for(int i = 0;i + 1 < n;i++){
			if(a[sol[i]] == sol[i + 1]){
				printf("-1\n"); return 0;
			}
		}
		for(int x : sol) printf("%d ", x);
		printf("\n");
		return 0;
	}
	for(int x : sol) printf("%d ", x);
	for(int x : pos) printf("%d ", x);
	printf("\n");
	return 0;
}

















Compiler output:
a.cc: In function 'int main()':
a.cc:145:33: error: 'smanji' was not declared in this scope
  145 |                                 smanji(a[nov]);
      |                                 ^~~~~~