
#include <iostream>
#include <vector>
using namespace std;

vector<vector<int>> tile(50,vector<int>(50));
vector<vector<int>> pile(50,vector<int>(50));
vector<vector<char>> board(50,vector<char>(50,'-'));
string route;
long long score;

void print(vector<vector<int>> t){

    for (int i=0;i<50;i++){
        for (int j=0;j<50;j++){
            if(j==50-1)
                cout << t[i][j] << endl;
            else 
                cout << t[i][j] ;

        }
    }


}

void write(int dx ,int dy){

    if(dx == -1 && dy == 0)
        route.push_back('U');
    else if(dx ==1 && dy == 0)
        route.push_back('D');
    else if(dx == 0 && dy == -1)
        route.push_back('L');
    else
        route.push_back('R');

}

void check_same_tile(int x,int y){

     for(int dx=-1;dx<=1;dx++){
        for(int dy=-1;dy<=1;dy++){
            if(dx!=dy && dx != -dy){
                if(0<=x+dx && x+dx <50  && 0 <= y+dy && y+dy < 50 && tile[x][y] == tile[x+dx][y+dy]){
                    board[x+dx][y+dy] ='*';
                }
            }
        }      
    }

}

bool solve(int x,int y){

    score += pile[x][y];
    
    check_same_tile(x,y);

 //   print(tile);

    for(int dx=-1;dx<=1;dx++){
        for(int dy=-1;dy<=1;dy++){
            if(dx!=dy && dx != -dy){
                if(0<=x+dx && x+dx <50  && 0 <= y+dy && y+dy < 50 && tile[x][y]!=tile[x+dx][y+dy] && board[x+dx][y+dy] != '*'){
                    board[x+dx][y+dy] ='*';
                    write(dx,dy);
                    return solve(x+dx,y+dy);
                }
            }
        }      
    }

    return false;
}

int main(){

    int si,sj;
    cin >> si >> sj;

    for (int i=0;i<50;i++){
        for (int j=0;j<50;j++){
            cin >> tile[i][j];
        }
    }

    for (int i=0;i<50;i++){
        for (int j=0;j<50;j++){
            cin >> pile[i][j];
        }
    }

    solve(si,sj);

    cout << route << endl;

}
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<cctype>
using namespace std;

template <typename T>
inline void read(T&x){
    x=0; char temp=getchar(); bool f=false;
    while(!isdigit(temp)){if(temp=='-') f=true; temp=getchar();}
    while(isdigit(temp)){x=(x<<1)+(x<<3)+temp-'0'; temp=getchar();}
    if(f) x=-x;
}
template <typename T>
void print(T x){
    if(x<0) putchar('-'),x=-x;
    if(x>9) print(x/10);
    putchar(x%10+'0');
}

typedef long long ll;
const int MAXN = 1e6+5;

//basic
int n;
ll k;

//DP
ll f[4][MAXN*3],prefix[4][MAXN*3];
//f[i][j]=\sigma k=1->n f[i-1][j-k];

int main(){
    read(n),read(k);
    f[0][0]=1; for(register int i=0;i<=n;i++) prefix[0][i]=1;
    for(register int i=1;i<=3;i++){
        for(register int j=i;j<=i*n;j++){
            int temp=j-min(n,j-i+1)-1;
            f[i][j]=prefix[i-1][j-1]-(temp>0? prefix[i-1][temp]:0);
        }
        for(register int j=1;j<=3*n;j++) prefix[i][j]=prefix[i][j-1]+f[i][j];
    }
    int sum=3;
    while(f[3][sum]<k) k-=f[3][sum],sum++;
    for(register int i=1;i<=n;i++){
        int l=max(sum-i-n,1),r=min(sum-i-1,n);
        if(l>r) continue;
        if(k>r-l+1){k-=r-l+1; continue;}
        print(i),putchar(' '),print(l+k-1),putchar(' '),print(sum-i-(l+k-1));
        break;
    }
    return 0;
}