#pragma GCC optimize("Ofast") #include // #include // #include // #include // using namespace __gnu_pbds; // #include // namespace multiprecisioninteger = boost::multiprecision; // using cint=multiprecisioninteger::cpp_int; using namespace std; using ll=long long; #define double long double using datas=pair; using ddatas=pair; using tdata=pair; using vec=vector; using mat=vector; using pvec=vector; using pmat=vector; // using llset=tree,rb_tree_tag,tree_order_statistics_node_update>; #define For(i,a,b) for(i=a;i<(ll)b;++i) #define bFor(i,b,a) for(i=b,--i;i>=(ll)a;--i) #define rep(i,N) For(i,0,N) #define rep1(i,N) For(i,1,N) #define brep(i,N) bFor(i,N,0) #define brep1(i,N) bFor(i,N,1) #define all(v) (v).begin(),(v).end() #define allr(v) (v).rbegin(),(v).rend() #define vsort(v) sort(all(v)) #define vrsort(v) sort(allr(v)) #define endl "\n" #define eb emplace_back #define print(v) cout<>N>>A>>B; vec v(N); rep(i,N){ cin>>v[i]; if(v[i]==A)pa=i; if(v[i]==B)pb=i; } if(!next_permutation(all(v))){ print(-1); return 0; } if(pa se; set::iterator itr; while(--i>=0){ se.insert(v[i]); if(i<=pb){ itr=se.upper_bound(v[i]); if(itr!=se.end()&&*itr==B)++itr; if(itr!=se.end())break; } } if(i==-1){ print(-1); return 0; } v[i]=*itr; se.erase(itr); itr=se.begin(); if(A