//Coded by Chirath Nirodha
#include<bits/stdc++.h>
#define F first
#define S second
#define PB push_back
#define MP make_pair
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
using namespace std;
const ll mod=1e9+7;
inline void io(){
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
}
pair<int,int> s;
int t[50][50];
ll p[50][50];
ll dp[50][50];
bool visited[50][50];
string path[50][50];
void makevisit(int a,int b){
	visited[a][b]=true;
	if(a!=49 && t[a+1][b]==t[a][b])visited[a+1][b]=true;
	if(a!=0 && t[a-1][b]==t[a][b])visited[a-1][b]=true;
	if(b!=49 && t[a][b+1]==t[a][b])visited[a][b+1]=true;
	if(b!=0 && t[a][b-1]==t[a][b])visited[a][b-1]=true;
}
void makeunvisit(int a,int b){
	visited[a][b]=false;
	if(a!=49 && t[a+1][b]==t[a][b])visited[a+1][b]=false;
	if(a!=0 && t[a-1][b]==t[a][b])visited[a-1][b]=false;
	if(b!=49 && t[a][b+1]==t[a][b])visited[a][b+1]=false;
	if(b!=0 && t[a][b-1]==t[a][b])visited[a][b-1]=false;
}
ll ans=dp[s.F][s.S];
string as;
void dfs(int a,int b){
	if(a!=49 && t[a+1][b]!=t[a][b] && visited[a+1][b]==false){
		if(dp[a][b]+p[a+1][b]+10>=dp[a+1][b]){
			path[a+1][b]=path[a][b];
			path[a+1][b].PB('D');
			dp[a+1][b]=dp[a][b]+p[a+1][b];
			if(ans<dp[a+1][b]){
				ans=dp[a+1][b];
				as=path[a+1][b];
			}
			makevisit(a+1,b);
			dfs(a+1,b);	
			makeunvisit(a+1,b);
		}
	}
	if(a!=0 && t[a-1][b]!=t[a][b] && visited[a-1][b]==false){
		if(dp[a][b]+p[a-1][b]+10>=dp[a-1][b]){
			path[a-1][b]=path[a][b];
			path[a-1][b].PB('U');
			dp[a-1][b]=dp[a][b]+p[a-1][b];
			if(ans<dp[a-1][b]){
				ans=dp[a-1][b];
				as=path[a-1][b];
			}
			makevisit(a-1,b);
			dfs(a-1,b);	
			makeunvisit(a-1,b);
		}
	}
	if(b!=49 && t[a][b+1]!=t[a][b] && visited[a][b+1]==false){
		if(dp[a][b]+p[a][b+1]+10>=dp[a][b+1]){
			path[a][b+1]=path[a][b];
			path[a][b+1].PB('R');
			dp[a][b+1]=dp[a][b]+p[a][b+1];
			if(ans<dp[a][b+1]){
				ans=dp[a][b+1];
				as=path[a][b+1];
			}
			makevisit(a,b+1);
			dfs(a,b+1);	
			makeunvisit(a,b+1);
		}
	}
	if(b!=0 && t[a][b-1]!=t[a][b] && visited[a][b-1]==false){
		if(dp[a][b]+p[a][b-1]+10>=dp[a][b-1]){
			path[a][b-1]=path[a][b];
			path[a][b-1].PB('L');
			dp[a][b-1]=dp[a][b]+p[a][b-1];
			if(ans<dp[a][b-1]){
				ans=dp[a][b-1];
				as=path[a][b-1];
			}
			makevisit(a,b-1);
			dfs(a,b-1);	
			makeunvisit(a,b-1);
		}
	}
}
void solve(){
	cin>>s.F>>s.S;
	for(int i=0;i<50;i++)for(int j=0;j<50;j++)cin>>t[i][j];	
	for(int i=0;i<50;i++)for(int j=0;j<50;j++){cin>>p[i][j];dp[i][j]=p[i][j];}
	int a=s.F,b=s.S;
	makevisit(a,b);
	dfs(a,b);
	cout<<as<<endl;
}
int main(){
    io();
	solve();
	//int t;cin>>t;for(int i=0;i<t;i++)solve();
    return 0;
}

#pragma GCC optimize("O3", "unroll-loops")
#define fs first
#define sc second
#include <bits/stdc++.h>

using namespace std;

typedef pair<int, int> pii;

const int maxn = 2e5 + 10;

long long n, m, h[maxn], wi, pre1[maxn], pre2[maxn], pos, ans = 1e18;

int main() {
	ios::sync_with_stdio(false);
	cin.tie(NULL);
	cin >> n >> m;
	for (int i = 1; i <= n; i++)
		cin >> h[i];
	sort(h + 1, h + n + 1);
	for (int i = 2; i <= n; i += 2) {
		pre1[i] = pre1[i - 2] + (h[i] - h[i - 1]);
		pre2[i + 1] = pre2[i - 1] + (h[i + 1] - h[i]);
	}
	for (int j = 0; j < m; j++) {
		cin >> wi, pos = lower_bound(h + 1, h + n + 1, wi) - h;
		if (pos % 2)
			ans = min(ans, pre1[pos - 1] + (h[pos] - wi) + (pre2[n] - pre2[pos]));
		else
			ans = min(ans, pre1[pos - 2] + (wi - h[pos - 1]) + (pre2[n] - pre2[pos - 1]));
	}
	cout << ans << '\n';
	return 0;
}
