#include <bits/stdc++.h>

clock_t start;

constexpr int H = 50, W = 50;
int si, sj;
int t[H][W], p[H][W];

struct cand {
    std::string s;
    int score, i, j;
    std::vector<bool> seen;
    bool operator<(const cand& rhs) const { return score < rhs.score; }
    bool valid(const char& c) {
        if (c == 'L') return (0 < j && !seen[t[i][j - 1]]);
        if (c == 'R') return (j < W - 1 && !seen[t[i][j + 1]]);
        if (c == 'U') return (0 < i && !seen[t[i - 1][j]]);
        if (c == 'D') return (i < H - 1 && !seen[t[i + 1][j]]);
    }
    cand move(const char& c) {
        cand ret = *this;
        ret.s.push_back(c);
        if (c == 'L') ret.j--;
        else if (c == 'R') ret.j++;
        else if (c == 'U') ret.i--;
        else ret.i++;
        ret.score += p[ret.i][ret.j];
        ret.seen[t[ret.i][ret.j]] = true;
        return ret;
    }
};

int main() {
    start = clock();

    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);

    std::cin >> si >> sj;
    for (int i = 0; i < H; i++) for (int j = 0; j < W; j++) std::cin >> t[i][j];
    for (int i = 0; i < H; i++) for (int j = 0; j < W; j++) std::cin >> p[i][j];

    constexpr int N = 1000;
    int n = 1;
    std::vector<bool> seen(H * W, false);
    seen[t[si][sj]] = true;
    cand ans = {"", p[si][sj], si, sj, seen};
    std::vector<cand> c(N, ans);
    while (clock() - start <= 1.95 * CLOCKS_PER_SEC) {
        std::priority_queue<cand> pq;
        for (int i = 0; i < n; i++) {
            ans = std::max(ans, c[i]);
            if (c[i].valid('L')) pq.push(c[i].move('L'));
            if (c[i].valid('R')) pq.push(c[i].move('R'));
            if (c[i].valid('U')) pq.push(c[i].move('U'));
            if (c[i].valid('D')) pq.push(c[i].move('D'));
        }
        n = std::min(N, (int)pq.size());
        for (int i = 0; i < n; i++) {
            c[i] = pq.top();
            pq.pop();
        }
    }
    std::cout << std::max(ans, c[0]).s << '\n';
}
#include<bits/stdc++.h> 
using namespace std;

#define ll long long int
#define absdiff(a,b) (a>b)?a-b:b-a
#define mp make_pair 
#define pb push_back
#define ff first
#define ss second
#define fast  ios_base::sync_with_stdio(false);cin.tie(0);
#define MOD 1000000007 
#define ll_MAX 19223372036859
#define endl "\n"
ll fast_expo(ll x,ll p){
  if(p==0) return 1;
  else if(p%2==0){ ll t=fast_expo(x,p/2)%MOD;return (t*t)%MOD;} 
  else return (x*(fast_expo(x,p-1))%MOD)%MOD;}

ll dp[1005][1005], n, m, arr1[1005], arr2[1005];
vector<ll> AdjL[1005];

int main(){
  fast
  #ifndef ONLINE_JUDGE
        freopen("input.txt", "r", stdin);
        freopen("output.txt", "w", stdout);
  #endif
  
  ll t_c; t_c = 1;
  // cin >> t_c;

  for(ll t_i = 0; t_i < t_c; ++t_i){
    ll n, m; cin >> n >> m;
    ll a[n + 1], b[m + 1];
    for(ll i = 1; i <= n; ++i)
      cin >> a[i];
    for(ll i = 1; i <= m; ++i)
      cin >> b[i];

    ll dp[n + 1][m + 1];
    for(ll j = 0; j <= m; ++j){
      dp[0][j] = j;
    }

    for(ll i = 1; i <= n; ++i){
      dp[i][0] = i;
      for(ll j = 1; j <= m; ++j){
        if(a[i] == b[j]) dp[i][j] = dp[i - 1][j - 1];
        else dp[i][j] = min({dp[i - 1][j] + 1, dp[i][j - 1] + 1, dp[i - 1][j - 1] + 1}); 
      }
    } 

    cout << dp[n][m];
  }
  return 0;
}