#include <bits/stdc++.h>
#define fo(a,b,c) for (a=b; a<=c; a++)
#define fd(a,b,c) for (a=b; a>=c; a--)
#define ll long long
using namespace std;

int n,i,j,k,l,tot;
int ans[200001];
int a[200001];
bool bz[200001];

void swap(int &x,int &y) {int z=x;x=y;y=z;}
void Exit()
{
    printf("-1\n");
    exit(0);
}

int main()
{
#ifdef file
    freopen("c.in","r",stdin);
#endif

    scanf("%d",&n);
    fo(i,1,n) scanf("%d",&a[i]);

    l=1;
    fo(i,1,n)
        if (a[i]==l && i>l)
        {
            fd(j,i-1,l)
                if (!bz[j])
                    bz[j]=1,swap(a[j],a[j+1]),ans[++tot]=j;
                else
                    Exit();

            while (l<=n && a[l]==l) ++l;
            if (l>n) break;
        }
    fo(i,1,n-1) if (a[i]>a[i+1]) Exit();

    if (tot<n-1) Exit();
    else
    {
        fo(i,1,n-1)
            printf("%d\n",ans[i]);
    }

    fclose(stdin);
    fclose(stdout);
    return 0;
}

#include <bits/stdc++.h>
using namespace std;
template <typename T> void read(T &t) {
	t=0; char ch=getchar(); int f=1;
	while (ch<'0'||ch>'9') { if (ch=='-') f=-1; ch=getchar(); }
	do { (t*=10)+=ch-'0'; ch=getchar(); } while ('0'<=ch&&ch<='9'); t*=f;
}
const int maxn=(4e5)+10;
int n,fa[maxn],dis[maxn],dep[maxn];
vector<int> g[maxn];
int lg[maxn];
int mx,son[maxn];
void dfs(int u,int p) {
	dis[u]=dis[p]+1; fa[u]=p;
	for (int v : g[u]) if (v!=p) dfs(v,u);
	if (!mx||dis[u]>dis[mx]) mx=u;
}
int st[maxn][20];
int dfn[maxn],tot,idx,euler[maxn],pos[maxn],ans[maxn];
void dfs2(int u,int p) {
	dfn[++idx]=u;
	for (int v : g[u]) if (v!=p&&v!=son[u]) dfs2(v,u);
	if (son[u]) dfs2(son[u],u);
}
int MN(int x,int y) {
    if (dep[x]<dep[y]) return x;
    return y;
}
int lca(int x,int y) {
    x=pos[x],y=pos[y];
    if (x>y) swap(x,y);
    int j=lg[y-x+1];
    return MN(st[x][j],st[y-(1<<j)+1][j]);
}
int DIS(int x,int y) {
    return dep[x]+dep[y]-2*dep[lca(x,y)];
}
void pre(int u,int p) {
    dep[u]=dep[p]+1;
    euler[++tot]=u; pos[u]=tot;
    for (int v : g[u]) if (v!=p) {
        pre(v,u);
        euler[++tot]=u;
    }
}
int main() {
	//freopen("1.txt","r",stdin);
    for (int i=2;i<maxn;i++) lg[i]=lg[i>>1]+1;
	read(n); int x,y,z;
	for (int i=1;i<n;i++) {
		read(x),read(y);
		g[x].push_back(y);
		g[y].push_back(x);
	}
	pre(1,0);
	for (int i=1;i<=tot;i++) st[i][0]=euler[i];
    for (int j=1;j<=19;j++)
    for (int i=1;i+(1<<j)-1<=tot;i++)
        st[i][j]=MN(st[i][j-1],st[i+(1<<(j-1))][j-1]);
	mx=0; dfs(1,0); x=mx;
	mx=0; dfs(x,0); y=mx;
	z=y;
	while (fa[z]) son[fa[z]]=z,z=fa[z];
	dfs2(x,0);
	ans[x]=1;
	for (int i=2;i<=n;i++)
		ans[dfn[i]]=ans[dfn[i-1]]+DIS(dfn[i],dfn[i-1]);
	for (int i=1;i<=n;i++) printf("%d ",ans[i]); puts("");
	return 0;
}
/*
  REMEMBER:
  1. Think TWICE, Code ONCE!
  Are there any counterexamples to your algo?
    
  2. Be careful about the BOUNDARIES!
  N=1? P=1? Something about 0?
    
  3. Do not make STUPID MISTAKES!
  Array size? Integer overflow? Time complexity? Memory usage? Precision error?
*/