/*
* * author: attacker
* * created: 03.04.2026 10:57:54
*/
#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#include "algo/debug.h"
#else
#define debug(...) 1
#endif
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
#define bpc __builtin_popcount
#define size(v) (int)(v.size())
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
int n;
cin >> n;
vector<vector<int>> g(9);
int cur, num, last;
for (int i = 0; i < n; i++) {
int type, val;
cin >> type >> val;
g[type].push_back(val);
if (type == 5 || type == 6) {
cur = type;
num = val;
} else if (type == 7 || type == 8) {
last = type;
}
}
vector<vector<int>> can(9);
can[1] = {3, 4, 8};
can[2] = {1, 2, 7};
can[3] = {3, 4, 8};
can[4] = {1, 2, 7};
can[5] = {3, 4, 8};
can[6] = {1, 2, 7};
can[7] = {};
can[8] = {};
for (int i = 1; i <= 8; i++) {
sort(g[i].rbegin(), g[i].rend());
}
const int inf = (int) 1e9;
vector<int> res = {num};
auto Choose = [&](int nw) {
pair<int, int> res = {inf, -1};
for (int type : can[nw]) {
if (g[type].empty()) continue;
if (type <= 4) {
if (type == 2 || type == 3 || !g[5 - type].empty()) {
if (res.first > g[type].back()) {
res = {g[type].back(), type};
}
}
}
}
if (res.second == -1) {
for (int type : can[nw]) {
if (g[type].empty()) continue;
if (type <= 4) {
if (res.first > g[type].back()) {
res = {g[type].back(), type};
}
}
}
}
if (res.second == -1) {
for (int type : can[nw]) {
if (g[type].empty()) continue;
if (res.first > g[type].back()) {
res = {g[type].back(), type};
}
}
if (res.second == -1) {
return res;
}
}
g[res.second].pop_back();
return res;
};
while (true) {
auto p = Choose(cur);
if (p.second == -1) break;
res.push_back(p.first);
cur = p.second;
}
if (size(res) == n) {
for (int i = 0; i < size(res); i++) {
cout << res[i] << " \n"[i == size(res) - 1];
}
} else {
cout << -1 << '\n';
}
return 0;
}