#include #include #include #include #include #include #include using namespace std; using ll = long long; int main() { ios::sync_with_stdio(false); cin.tie(0); int n, p, q; cin >> n >> p >> q; p--; q--; vector a(n); for (int i = 0; i < n; i++) { cin >> a[i]; a[i]--; } if (!next_permutation(a.begin(), a.end())) { cout << -1 << endl; exit(0); } auto f = [&]() { for (int i = 0; i < n; i++) { cout << a[i] + 1 << " \n"[i == n - 1]; } exit(0); }; int i0 = -1, i1; for (int i = 0; i < n; i++) { if (a[i] == p) i0 = i; if (a[i] == q) { if (i0 >= 0) f(); i1 = i; } } if (p > q) { pair a1 = { p, i0 }; for (int i = i1 + 1; i < n; i++) { if (a[i] > q) a1 = min(a1, { a[i], i }); } if (a1.first == p) { swap(a[i1], a[i0]); sort(a.begin() + i1 + 1, a.end()); } else { swap(a[i1], a[a1.second]); sort(a.begin() + i1 + 1, a.end()); for (int i = i1 + 1; i < n; i++) { if (a[i] == q) i0 = i; if (a[i] == p) { rotate(a.begin() + i0, a.begin() + i0 + 1, a.begin() + i + 1); } } } f(); } else { pair a1 = { n, -1 }; for (int i = i1 + 1; i < n; i++) { if (a[i] > q) a1 = min(a1, { a[i], i }); } if (a1.second >= 0) { swap(a[i1], a[a1.second]); sort(a.begin() + i1 + 1, a.end()); f(); } else { for (int i = i1 - 1; i >= 0; i--) { if (a[i] < a[i + 1]) { int i0 = i; pair a1 = { n, -1 }; for (int i = i0 + 1; i < n; i++) { if (a[i] > a[i0] && a[i] != q) a1 = min(a1, { a[i], i }); } if (a1.second < 0) continue; swap(a[i0], a[a1.second]); sort(a.begin() + i0 + 1, a.end()); f(); } } } } cout << -1 << endl; return 0; }