#include "bits/stdc++.h"

using namespace std;
using ll = long long;
using P = pair<ll, ll>;
const ll INF = (1LL << 61);
ll mod = 1000000007;
int dx[4] = { 1, 0, -1,0 }, dy[4] = { 0, 1, 0, -1 };
int si, sj;
int N = 50;
int t[50][50];
int p[50][50];
map<pair<int, int>, pair<int, int>>mp;
bool seen[50][50];
int f(string s) {
	int ans = p[si][sj];
	int n = s.size();
	for (int i = 0; i < N; i++)for (int j = 0; j < N; j++)seen[i][j] = false;
	seen[si][sj] = true;
	int nowi = si, nowj = sj;
	for (int i = 0; i < n; i++) {
		char now = s[i];
		int ni = nowi, nj = nowj;
		int c = 0;
		if (now == 'D') {
			c = 0;
		}
		else if (now == 'R') {
			c = 1;
		}
		else if (now == 'U') {
			c = 2;
		}
		else {
			c = 3;
		}
		ni += dx[c];
		nj += dy[c];
		if (ni < 0 || ni >= N || nj < 0 || nj >= N)return 0;
		//if (t[ni][nj] == t[nowi][nowj] || seen[mp[{ni, nj}].first][mp[{ni, nj}].second])return 0;
		if (seen[ni][nj])return 0;
		seen[ni][nj] = true;
		ans += p[ni][nj];
		nowi = ni, nowj = nj;
	}
	return ans;
}

signed main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	clock_t start = clock();
	cin >> si >> sj;
	
	for (int i = 0; i < N; i++) {
		for (int j = 0; j < N; j++) {
			cin >> t[i][j];
		}
	}
	for (int i = 0; i < N; i++) {
		for (int j = 0; j < N; j++) {
			cin >> p[i][j];
		}
	}
	for (int i = 0; i < N; i++) {
		for (int j = 0; j < N; j++) {
			mp[{i, j}] = { i, j };
			for (int s = 0; s < 4; s++) {
				int ni = i + dx[s], nj = j + dy[s];
				if (0 <= ni && ni < N && 0 <= nj && nj < N && t[ni][nj] == t[i][j]) {
					mp[{i, j}] = { ni, nj };
				}
			}
		}
	}
	string ans = "";
	int nowscore = 0;
	clock_t end = clock();
	int cnt = 0;
	while ((double)(end - start) / CLOCKS_PER_SEC < 0.98) {
		end = clock();
		cnt++;
		queue<pair<int, int>>q;
		vector<vector<int>>dist(N, vector<int>(N, -1));
		q.push({ si, sj });
		dist[si][sj] = 0;
		string s = "";

	
		while (!q.empty()) {
			pair<int, int> pa = q.front(); q.pop();
			vector<int>v;
			for (int s = 0; s < 4; s++) {
				int ni = pa.first + dx[s], nj = pa.second + dy[s];
				if (0 <= ni && ni < N && 0 <= nj && nj < N && dist[ni][nj] == -1) {
					if (t[ni][nj] != t[pa.first][pa.second] && dist[mp[{ni, nj}].first][mp[{ni, nj}].second] == -1) {
						v.push_back(s);
					}
				}
			}
			int n = v.size();
			if (n == 0)break;
			int idx = v[rand() % n];
			if (idx == 2)s += 'U';
			else if (idx == 0)s += 'D';
			else if (idx == 1)s += 'R';
			else s += 'L';
			int ni = pa.first + dx[idx], nj = pa.second + dy[idx];
			if (0 <= ni && ni < N && 0 <= nj && nj < N && dist[ni][nj] == -1) {
				if (t[ni][nj] != t[pa.first][pa.second]) {
					q.push({ ni, nj });
					dist[ni][nj] = dist[pa.first][pa.second] + 1;
				}
			}
		}
		int tscore = f(s);
		if (nowscore < tscore) {
			nowscore = tscore;
			ans = s;
		}
	}
	cout << ans << endl;
	return 0;
}
#include <bits/stdc++.h>
//#include "atcoder/all"
#define rep(i,n) for(int i=0;i<n;i++)
using namespace std;
//using namespace atcoder;
#define all(a) a.begin(),a.end()
typedef long long ll;
typedef vector<ll> vi;
typedef pair<ll,ll> P;
constexpr ll inf=1ll<<61;
constexpr ll mod=998244353;
int a[1005],b[1005];
ll dp[1005][1005];
int main(){
	int n,m;cin>>n>>m;
	rep(i,n)cin>>a[i];
	rep(i,m)cin>>b[i];
	rep(i,n+1)rep(j,m+1)dp[i][j]=inf;
	dp[0][0]=0;
	rep(i,n+1){
		rep(j,m+1){
			dp[i+1][j]=min(dp[i+1][j],dp[i][j]+1);
			dp[i][j+1]=min(dp[i][j+1],dp[i][j]+1);
			if(a[i]==b[j]){
				dp[i+1][j+1]=min(dp[i+1][j+1],dp[i][j]);
			}
			else{
				dp[i+1][j+1]=min(dp[i+1][j+1],dp[i][j]+1);
			}
		}
	}
	cout<<dp[n][m]<<endl;
}