#include<bits/stdc++.h>
#include<vector>
#include<string>
#include<time.h>
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
using namespace std;
bool flag[2510];
int t[51][51];
int p[51][51];
int dx[4];
int dy[4];
string direction = "UDLR";
string route;
string bestroute;
time_t start_time, end_time;
int nx,ny;
int mx;
int sm,maxsum;
bool is_legal_action(int nx,int ny,bool *flag){
	if(0<=nx&&nx<50&&0<=ny&&ny<50&&flag[t[nx][ny]]==0){
		return true;
	}else{
		return false;
	}
}
int legallistlength(int x,int y,bool *flag){
  	int k=0;
  	rep(i,4){
      	nx = x + dx[i];
		ny = y + dy[i];
      	if(is_legal_action(nx,ny,flag)){
          	k++;
        }
    }
    return k;
}
void dfs(int x,int y,bool *flag){
  	end_time = time(NULL);
  	if(end_time - start_time>1.95)return;
  	flag[t[x][y]]=1;
  	sm+=p[x][y];
  	//sm++;
  	rep(i,4){
		nx = x + dx[i];
		ny = y + dy[i];
		if(is_legal_action(nx,ny,flag)){
          	//cout << x <<" " <<  y <<" " << i << endl;
          	route.push_back(direction[i]);
			dfs(nx,ny,flag);
			route.pop_back();
        }
    }
    if(maxsum<sm){
          	maxsum=sm;
      		bestroute=route;
    }
  	sm-=p[x][y];
  	//sm--;
  	flag[t[x][y]]=0;
  	return;
}
/*void func(int x,int y,bool *flag,vector<char>& route){
	flag[t[x][y]]=1;
	int s=-1;
	int nxt=-1;
	rep(i,4){
		nx = x + dx[i];
		ny = y + dy[i];
		if(is_legal_action(nx,ny,flag)){
			if(s<=p[nx][ny]){
				s=p[nx][ny];
				nxt=i;
			}
          	sm=0;
          	maxsum=-1;
          	dfs(nx,ny,flag,22);
          	if(maxsum>=s){
              	s=maxsum;
              	nxt=i;
            }
          	//cout << i << " " << maxsum <<endl;
          	
		}
      	//cout <<x<<" " << y <<direction[i]<< nxt << endl;
	}
  	flag[t[x][y]]=1;
	if(nxt==-1){
		for (auto i: route){
			cout << i;
		}
		cout << "\n";
	}else{
		route.push_back(direction[nxt]);
		func(x + dx[nxt],y + dy[nxt],flag,route);
		route.pop_back();
	}
	flag[t[x][y]]=0;
	return;
}*/
int main(){
  	start_time = time(NULL);
	int si,sj;
	scanf("%d %d",&si,&sj);
	int M=0;
	rep(i,50)rep(j,50){
		cin >> t[i][j];
		if(M<t[i][j])M=t[i][j];
	}
	M++;
	rep(i,50)rep(j,50) scanf("%d", &p[i][j]);
	rep(i,2500)flag[i]=0;
	dx[0]=-1;
  	dx[1]=1;
  	dy[2]=-1;
  	dy[3]=1;
	dfs(si,sj,flag);
  	for(auto i: bestroute){
      	cout << i;
    }
  	cout << "\n";
	return 0;
}
#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int (i)=0;(i)<(n);(i)++)
#define ll long long
#define pp pair<ll,ll>
#define ld long double
#define all(a) (a).begin(),(a).end()
#define mk make_pair
int inf=1000001000;
ll INF=2e18;
ll MOD=1000000007;
ll mod=998244353;

int main() {
    int n,l;
    cin >> n >> l;
    vector<int> a(n+2),b(n+2);
    rep(i,n) cin >> a[i+1];
    rep(i,n) cin >> b[i+1];
    a[0]=0;
    a[n+1]=l+1;
    b[0]=0;
    b[n+1]=l+1;
    ll ans=0;
    for (int i=1;i<=n;i++){
        int l=-1,r=n+1;
        while(r-l>1){
            int mid=(r+l)/2;
            if (mid<=i) {
                l=mid;
                continue;
            }
            if (a[mid]-(mid-i)>=b[i]) r=mid;
            else l=mid;
        }
        if (a[r]-(r-i)==b[i] && a[i]!=b[i]){
            rep(j,r-i){
                a[r-j-1]=a[r]-j-1;
                ans++;
            }
        }
    }
    for (int i=n;i>=1;i--){
        int l=0,r=n+2;
        while(r-l>1){
            int mid=(r+l)/2;
            if (mid>=i) {
                r=mid;
                continue;
            }
            if (a[mid]+(i-mid)<=b[i]) l=mid;
            else r=mid;
        }
        if (a[l]+(i-l)==b[i] && a[i]!=b[i]){
            rep(j,i-l){
                a[l+j+1]=a[l]+j+1;
                ans++;
            }
        }
    }
    bool w=true;
    rep(i,n){
        if (a[i+1]!=b[i+1]) w=false;
    }
    if (w) cout << ans << endl;
    else cout << -1 << endl;
}
