#include <bits/stdc++.h>

using namespace std;

#define pb push_back
#define mp make_pair
#define sz(x) ((int)x.size())
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define manytests int TT;cin >> TT; while (TT--)

#define FOR(i,a,b) for (int i = (a); i < (b); ++i)
#define FORd(i,a,b) for (int i = (a); i >= (b); --i)

int dx[8] = {-1, 1, 0, 0, 1, 1, -1, -1}, dy[8] = {0, 0, 1, -1, 1, 1, -1, -1};

#define ll long long
#define ld long double
#define fi first
#define se second
#define rev reverse

#define vi vector<int>
#define vl vector<ll>
#define vc vector<char>
#define vd vector<double>
#define vs vector<string>
#define vld vector<ld>
#define vb vector<bool>
#define vvi vector<vi>
#define pii pair<int, int>
#define pl pair<ll, ll>
#define pld pair<ld, ld>
#define vpi vector<pii>
#define vpl vector<pl>
#define vpld vector<pld>

void ckmin(ll &a, ll b) { if (a > b) a = b; }
void ckmax(ll &a, ll b) { if (a < b) a = b; }

const int mxn = 1e5+5;
int c[mxn];
int vis[mxn];
vi ans;
vvi g;

void dfs(int v, int p = -1) {
    if (!vis[c[v]]) ans.pb(v);
    vis[c[v]]++;
    for (int u : g[v]) if (u != p) dfs(u, v);
    vis[c[v]]--;
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    
    int n;
    cin >> n;
    FOR(i,0,n) cin >> c[i];
    g.resize(n);
    FOR(i,0,n-1) {
        int x, y;
        cin >> x >> y;
        --x, --y;
        g[x].pb(y);
        g[y].pb(x);
    }
    dfs(0);
    sort(all(ans));
    for (auto x : ans) cout << x + 1 << "\n";

    return 0;
}

#include <bits/stdc++.h>
#define No return puts("-1"), 0
using namespace std;

typedef long long i64;
const int N = 2e5+10;
int n, m, a[N], b[N], c[N], to[N], bt[N];
vector<int> da[N], db[N];
void add(int x, int v=1) { for(; x <= n; x += x&-x) bt[x] += v; }
int ask(int x) { int y = 0; for(; x; x -= x&-x) y += bt[x]; return y; }

signed main() {
	cin >> n;
	for(int i = 1; i <= n; i ++) scanf("%d", &a[i]), a[i] += i, c[i] = a[i];
	for(int i = 1; i <= n; i ++) scanf("%d", &b[i]), b[i] += i;
	sort(c+1, c+1+n); m = unique(c+1, c+1+n) - c-1;
	for(int i = 1; i <= n; i ++) {
		a[i] = lower_bound(c+1, c+1+m, a[i]) - c;
		int p = lower_bound(c+1, c+1+m, b[i]) - c;
		if(c[p] != b[i]) No;
		da[a[i]].push_back(i), db[p].push_back(i);
	}
	for(int i = 1; i <= m; i ++) {
		if(da[i].size() != db[i].size()) No;
		for(int j = 0; j < da[i].size(); j ++) 
			to[da[i][j]] = db[i][j];
	}
	i64 ans = 0;
	for(int i = n; i; i --) {
		ans += ask(to[i]);
		add(to[i]);
	}
	cout << ans << endl;
	return 0;
}