#include <bits/stdc++.h>
#define watch(x) cout <<(#x)<<" is "<<(x)<<endl
#define debug cout <<"hi"<<endl
#define maxn 3e5+10
#define inf32 0x3f3f3f3f

using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<ll, ll> pll;
typedef pair<int,int>pii;
const int MOD=1e9+7;
const int INF32=1<<30;
const ll INF64=1LL<<60;

vector<vector<int>>adj(1e5+10);
int sub[100000+10];
int dp[100000+10];

void dfs(int u){
   vector<int>X[2];
   sub[u] = 1;
   for(auto i:adj[u]){
      dfs(i);
      sub[u] += sub[i];
      X[sub[i]&1].push_back(dp[i]);
   }
   dp[u] = 1;
   sort(X[0].begin(),X[0].end());
   sort(X[1].begin(),X[1].end());
   for(auto i:X[0]){
       if(i<=0) dp[u] += i;
   }
   int sgn = 1;
   for(auto i:X[1]){
       dp[u] += sgn*i;
       sgn *= -1;
   }
   for(auto i:X[0]){
       if(i>0) dp[u] += sgn*i;
   }
}
void solve(){
     int n;cin >> n;
     for(int i = 0;i<n-1;i++){
        int v;cin >> v;
        adj[v].push_back(i+2);
     }
     memset(dp,0,sizeof(dp));
     memset(sub,0,sizeof(sub));
     dfs(1);
     cout << (dp[1]+sub[1])/2;
}

int main(){
  ios_base::sync_with_stdio(0);
  cin.tie(0); cout.tie(0);
  solve();
  return 0;
}


#pragma region header
#include <bits/stdc++.h>
using namespace std;
#define rep(i,n)for(int i=0;i<(n);i++)
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define pb push_back
#define int ll
#define each(i, a) for (auto &&i : (a))
using ll = long long;
using ld = long double;
using vi = vector<int>;
using vvi = vector<vi>;
using vs = vector<string>;
using vvs = vector<vs>;
using vd = vector<ld>;
using vvd = vector<vd>;
using vb = vector<bool>;
using vvb = vector<vb>;
using P = pair<int, int>;
using vp = vector<P>;
using int128 = __int128_t;//cin coutはできない
template <class T>
using greater_queue = priority_queue<T, vector<T>, greater<T>>;
int gcd(int a,int b){return b?gcd(b,a%b):a;}
int lcm(int a,int b){return a / gcd(a, b) * b;};


template <class T>
void CVEC(const T &v) {
	int c = v.size() - 1;
	for (int i = 0; i < c; i++) cout << v[i] << ' ';
	if (c > -1) cout << v[c];
	cout << '\n';
}

#pragma endregion header

// 0以上UINT_MAX(0xffffffff)以下の整数をとる乱数 xorshift https://ja.wikipedia.org/wiki/Xorshift
static uint32_t randXor()
{
    static uint32_t x = 123456789;
    static uint32_t y = 362436069;
    static uint32_t z = 521288629;
    static uint32_t w = 88675123;
    uint32_t t;

    t = x ^ (x << 11);
    x = y; y = z; z = w;
    return w = (w ^ (w >> 19)) ^ (t ^ (t >> 8));
}

// 0以上1未満の小数をとる乱数
static double rand01()
{
    return (randXor() + 0.5) * (1.0 / UINT_MAX);
}

// Time
#include <sys/time.h>
ll getTime() {
  struct timeval tv;
  gettimeofday(&tv, NULL);
  ll result =  tv.tv_sec * 1000LL + tv.tv_usec / 1000LL;
  return result;
}
ll start_time, time_limit = 1950;
void setTime(){ start_time = getTime();}
ll nowTime(){ return getTime() - start_time;}
bool timeup(){ return nowTime() > time_limit;}


int si, sj;
int t[50][50];
int p[50][50];
int dx[4]={1,0,-1,0};
int dy[4]={0,1,0,-1};
string cdir = "DRUL";


struct STATE{
	int x, y;
	set<int> visited;
	int score;
	string path;
};

void init(STATE &state){
	state.x = si;
	state.y = sj;
	state.visited.insert(t[si][sj]);
	state.score = p[si][sj];
	state.path = "";
}

bool modify(STATE &state){
	int x = state.x;
	int y = state.y;
	int mx = -1, mxdir = -1;
	int mx_nx = -1, mx_ny = -1;
	for(int dir = 0; dir < 4; dir++){
		int nx = x + dx[dir], ny = y + dy[dir];
		if(nx >= 50 || ny >= 50 || nx < 0 || ny < 0) continue;
		if(state.visited.count(t[nx][ny])) continue;
		if(mx <= p[nx][ny]){
			mx = p[nx][ny];
			mxdir = dir;
			mx_nx = nx;
			mx_ny = ny;
		}
	}
	if(mxdir == -1) return false;
	state.visited.insert(t[mx_nx][mx_ny]);
	state.path += cdir[mxdir];
	state.score += mx;
	state.x = mx_nx;
	state.y = mx_ny;
	return true;
}

bool modify2(STATE &state){
	int x = state.x;
	int y = state.y;
	int mx = -1, mxdir = -1;
	int mx_nx = -1, mx_ny = -1;
	int dir = randXor() % 4;
	int nx = x + dx[dir], ny = y + dy[dir];
	if(nx >= 50 || ny >= 50 || nx < 0 || ny < 0) return false;
	if(state.visited.count(t[nx][ny])) return false;
	mxdir = dir;
	mx_nx = nx;
	mx_ny = ny;
	if(mxdir == -1) return false;
	state.visited.insert(t[mx_nx][mx_ny]);
	state.path += cdir[mxdir];
	state.score += mx;
	state.x = mx_nx;
	state.y = mx_ny;
	return true;
}

string mountain(){
	STATE state;
	init(state);
	while(!timeup()){
		STATE new_state = state;
		if(rand01() > 1){
			if(modify(new_state)) {
				state = new_state;
			}
		}else{
			if(modify2(new_state)){
				state = new_state;
			}
		}
	}
	return state.path;
}


signed main(){
	setTime();
    cin >> si >> sj;
	rep(i,50)rep(j,50) cin >> t[i][j];
	rep(i,50)rep(j,50) cin >> p[i][j];
	string ans = mountain();
	cout << ans << endl;
}