#include<bits/stdc++.h>
using namespace std;
#define INF 0x3f3f3f3f
#define swap(a,b) (a^=b^=a^=b)
#define ll long long int
#define ull unsigned long long int
#define uint unsigned int
#define format(a) memset(a,0,sizeof(a))
#define rep(x,y,z) for(int x=y;x<=z;x++)
#define dec(x,y,z) for(int x=y;x>=z;x--)
#define mst(x) memset(x,0,sizeof(x))
const int maxn=1e5+20;
inline int read()
{
    int ans=0;
    char last=' ',ch=getchar();
    while(ch<'0'|ch>'9')last=ch,ch=getchar();
    while(ch>='0' && ch<='9')ans=ans*10+ch-'0',ch=getchar();
    if(last=='-')ans=-ans;
    return ans;
}
int n,x,y;
int c[maxn],ok[maxn];
map<int,int>ma;
vector<int>node[maxn];
void dfs(int cur,int fa)
{
    if(ma[c[cur]])ok[cur]=0;
    else ok[cur]=1;
    ma[c[cur]]++;
    for(int i=0;i<node[cur].size();i++)
    {
        int nxt=node[cur][i];
        if(nxt==fa)continue;
        dfs(nxt,cur);
    }
    ma[c[cur]]--;
}
int main()
{
    n=read();
    rep(i,1,n)c[i]=read();
    rep(i,1,n-1)
    {
        int x=read(),y=read();
        node[x].push_back(y);
        node[y].push_back(x);
    }
    dfs(1,0);
    rep(i,1,n)
    {
        if(ok[i])cout<<i<<"\n";
    }
    return 0;
}


#include<bits/stdc++.h>
#define rep(i,n) for(int i=0;i<n;++i)
#define reps(i,s,n) for(int i=s;i<n;++i)
using namespace std;
using ll = long long;
using P = pair<ll, ll>;

int main()
{
	int r, c;
	cin >> r >> c;
	vector<vector<P>> g(2 * r * c);
	//横方向
	rep(i, r)rep(j, c - 1)
	{
		int a; cin >> a;
		int idx = i * c + j;
		g[idx].emplace_back(idx+1,a);
		g[idx + 1].emplace_back(idx, a);
	}
	//縦方向
	rep(i, r - 1)rep(j, c)
	{
		int b; cin >> b;
		int idx = i * c + j;
		g[idx].emplace_back(idx + c, b);
	}
	//高速道路の出入口
	rep(i, r)rep(j, c)
	{
		int idx = i * c + j;
		g[idx].emplace_back(idx + r * c, 1);
		g[idx+r*c].emplace_back(idx, 0);
	}
	//高速道路
	reps(i,1,r)rep(j, c)
	{
		int idx = i * c + j + r * c;
		g[idx].emplace_back(idx - c, 1);
	}

	const int INF = 1e9;
	priority_queue<P, vector<P>, greater<P>> pq;
	pq.emplace(0, 0);
	vector<int> dist(2 * r * c, INF);
	dist[0] = 0;
	while (!pq.empty())
	{
		int c, s;
		tie(c, s) = pq.top();
		pq.pop();
		if (c > dist[s])continue;
		for (auto [es,ec]: g[s])
		{
			if (dist[s] + ec >= dist[es])continue;
			dist[es] = dist[s] + ec;
			pq.emplace(dist[es],es);
		}
	}
	cout << dist[r * c - 1] << endl;	
	return 0;
}