#include <bits/stdc++.h>
using namespace std;

#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
template<class T> using oset = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

#define f first
#define s second
#define pb push_back
#define ar array
#define all(x) x.begin(), x.end()
#define siz(x) (int) x.size()
#define uid(a, b) uniform_int_distribution<int>(a, b)(rng)
#define FOR(i, a, b) for(auto i=(a); i<(b); i++)
#define ROF(i, a, b) for(auto i=(b)-1; i>=(a); i--)
#define F0R(i, n) FOR(i, 0, n)
#define R0F(i, n) ROF(i, 0, n)

using ll=long long;
using ld=long double;
using pii=pair<int, int>;
using pll=pair<ll, ll>;
using vi=vector<int>;
using vl=vector<ll>;
using vpii=vector<pii>;

template<class T> bool ckmin(T&a, const T&b) {return b<a?a=b,1:0;}
template<class T> bool ckmax(T&a, const T&b) {return b>a?a=b,1:0;}
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

const int mxN=2e5+10;
const int MOD=1e9+7; 
const ll infLL=1e18;
const ld eps=1e-6;

oset<int> o; 
int n; 

int a[mxN], b[mxN]; 

map<int, vi> proc;

ll ans;

int main(){
	cin.tie(0)->sync_with_stdio(0);
	cin >> n;
	F0R(i, n)
		cin >> a[i], a[i]+=i, proc[a[i]].pb(i);
	F0R(i, n)
		cin >> b[i], b[i]+=i; 
	for(auto&x:proc) reverse(all(x.s)); 
	F0R(i, n)
		o.insert(i); 
	F0R(i, n){
		if(siz(proc[b[i]])==0){
			cout << "-1\n"; return 0;
		}
		int v=proc[b[i]].back(); proc[b[i]].pop_back(); 
		// cout << "V: " << v << endl;
		// for(auto x:o) cout << x << ' ';
		// cout << endl; 	
		ans+=o.order_of_key(v); 
		o.erase(v); 
	}
	cout << ans << "\n"; 
	return 0; 
}


#include <iostream>
#include <string>
#include <algorithm>
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <iomanip>
#include <cmath>
#include <stdio.h>
#include <queue>
#include <deque>
#include <cstdio>
#include <set>
#include <map>
#include <bitset>
#include <stack>
#include <cctype>
using namespace std;
int t[55][55];
int p[55][55];
bool bo[2600] = { false };
int b1[4];
int b2[4];
int b3[4] = { 1,-1,0,0 };
int b4[4] = { 0,0,1,-1 };
int b5[4] = { 0,1,2,3 };
int main() {
	int x, y;
	cin >> x >> y;
	stack<pair<int, int>> s;
	s.push(make_pair(x, y));
	for (int i = 0; i < 50; i++) {
		for (int j = 0; j < 50; j++) {
			cin >> t[i][j];
		}
	}
	bo[t[x][y]] = true;
	for (int i = 0; i < 50; i++) {
		for (int j = 0; j < 50; j++) {
			cin >> p[i][j];
		}
	}
	string ans = "";
	int score = -1000000007;
	do {
		for (int i = 0; i < 4; i++) {
			b1[i] = b3[b5[i]];
			b2[i] = b4[b5[i]];
		}
		string ans1 = "";
		int score1 = 0;
		while (1) {
			int nx = s.top().first;
			int ny = s.top().second;
			bool bo1 = false;
			for (int i = 0; i < 4; i++) {
				if (nx + b1[i] < 0 || nx + b1[i] >= 50 || ny + b2[i] < 0 || ny + b2[i] >= 50)continue;
				if (!bo[t[nx + b1[i]][ny + b2[i]]]) {
					bo[t[nx + b1[i]][ny + b2[i]]] = true;
					s.push(make_pair(nx + b1[i], ny + b2[i]));
					bo1 = true;
					break;
				}
			}
			if (!bo1) {
				score1 += p[nx][ny];
				while (s.size() > 1) {
					s.pop();
					if (nx == s.top().first + 1) {
						ans1 += 'D';
					}
					if (nx == s.top().first - 1) {
						ans1 += 'U';
					}
					if (ny == s.top().second + 1) {
						ans1 += 'R';
					}
					if (ny == s.top().second - 1) {
						ans1 += 'L';
					}
					nx = s.top().first;
					ny = s.top().second;
					score1 += p[nx][ny];
				}
				reverse(ans1.begin(), ans1.end());
				if (score < score1) {
					ans = ans1;
					score = score1;
				}
				break;
			}
		}
		while (!s.empty()) {
			s.pop();
		}
		s.push(make_pair(x, y));
		for (int i = 0; i <= 2500; i++) {
			bo[i] = false;
		}
		bo[t[x][y]] = true;
	} while (next_permutation(b5, b5 + 4));
	cout << ans << endl;
}