#include <iostream>
#include <fstream>
#include <cstdio>
#include <cstdlib>
#include <cassert>
#include <climits>
#include <ctime>
#include <numeric>
#include <vector>
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstring>
#include <iomanip>
#include <complex>
#include <deque>
#include <functional>
#include <list>
#include <map>
#include <string>
#include <sstream>
#include <set>
#include <unordered_set>
#include <unordered_map>
#include <stack>
#include <queue>
#include <forward_list>
#include <thread>
#include <random>
#include <array>
using namespace std;
typedef long long ll;
typedef long double ld;
#define pb push_back
void io()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
}
int main()
{
    io();
    int si,sj; cin>>si>>sj;
    int pos[50][50];
    for(int i=0; i<50; i++){
      for(int j=0; j<50; j++){
        cin>>pos[i][j];
      }
    }
    bool chek[3000];
    memset(chek,false,sizeof(chek));
    int arr[50][50];
    for(int i=0; i<50; i++){
      for(int j=0; j<50; j++){
        cin>>arr[i][j];
      }
    }
    string str="";
    chek[pos[si][sj]]=true;
    while(true){
      int qwe=-1;
      char keeper=' ';
      if(si!=0 && !chek[pos[si-1][sj]]){

        if(arr[si-1][sj]>=qwe){qwe=arr[si-1][sj]; keeper='U';}
      }
      if(si!=49 && !chek[pos[si+1][sj]]){

        if(arr[si+1][sj]>=qwe){qwe=arr[si+1][sj]; keeper='D';}
      }
      if(sj!=0 && !chek[pos[si][sj-1]]){

        if(arr[si][sj-1]>=qwe){qwe=arr[si][sj-1]; keeper='L';}
      }
      if(sj!=49 && !chek[pos[si][sj+1]]){

        if(arr[si][sj+1]>=qwe){qwe=arr[si][sj+1]; keeper='R';}
      }
      if(qwe==-1){
        break;
      }
      if(keeper=='U'){
        chek[pos[si-1][sj]]=true;
        si--;
        str+=keeper;
      }
      else if(keeper=='D'){
        chek[pos[si+1][sj]]=true;
        si++;
        str+=keeper;
      }
      else if(keeper=='L'){
        chek[pos[si][sj-1]]=true;
        sj--;
        str+=keeper;
      }
      else if(keeper=='R'){
        chek[pos[si][sj+1]]=true;
        sj++;
        str+=keeper;
      }
    }
    cout<<str<<endl;
}
//Aneegha Thithiesha Mahabaduge.

/*
 * Author : Andrew J
*/
#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define all(x) x.begin() , x.end()
#define rall(x) x.rbegin() , x.rend()
#define vz(x) x.size()
using ll = long long;
using ii = pair <int , int>;
void in(int &x);
// const int N = ;
const int INF = 1e9 + 50;
const int MOD = 1e9 + 7;

int f(int x) {
  vector <int> dig, desc, asc;

  while (x) {
    dig.pb(x % 10);
    x /= 10;
  }

  desc = dig, asc = dig;
  sort(all(asc));
  sort(rall(desc));

  int desc_int = 0, asc_int = 0;

  {
    int tens = 1;
    for (int x : desc) {
      asc_int += x * tens;
      tens *= 10;
    }
  }

  {
    int tens = 1;
    for (int x : asc) {
      desc_int += x * tens;
      tens *= 10;
    }
  }

  return desc_int - asc_int;
}

int main() {
  int n, k; scanf("%d%d", &n, &k);
  
  while (k--) {
    n = f(n);
  }

  printf("%d\n", n);
  return 0;
}

void in(int &x){
  bool neg = false;
  register int c;
  x = 0;
  c = getchar();
  if(c == '-'){
    neg = true;
    c = getchar();
  }
  for (; c > 47 && c < 58; c = getchar()) {
    x = (x << 1) + (x << 3) + c - 48;
  }
  if (neg) x *= -1;
}
