#include <bits/stdc++.h>
using namespace std;
using ull = unsigned long long;
using ullvect2d = vector<vector<ull>>;
using pairsull = pair<string,ull>;
#define loop(i,n) for(ull i=0;i<n;i++)

ull x,y;
ullvect2d tile(50, vector<ull>(50));
ullvect2d score(50, vector<ull>(50));
vector<ull> tap;

bool has(ull x){
  auto itr = find(tap.begin(),tap.end(),x);
  size_t idx = distance(tap.begin(),itr);
  return idx != tap.size();
}
bool canUp(){
  if(y==0){ return false; }
  return !has(tile[y-1][x]);
}
bool canDown(){
  if(y==49){ return false; }
  return !has(tile[y+1][x]);
}
bool canLeft(){
  if(x==0){ return false; }
  return !has(tile[y][x-1]);
}
bool canRight(){
  if(x==49){ return false; }
  return !has(tile[y][x+1]);
}
bool canMove(){ return canUp() || canDown() || canLeft() || canRight(); }
void moveUp(){ y--; cout << "U"; }
void moveDown(){ y++; cout << "D"; }
void moveLeft(){ x--; cout << "L"; }
void moveRight(){ x++; cout << "R"; }
void move1(){  // 右下
  if(canDown()){ moveDown(); }
  else if(canRight()){ moveRight(); }
  else if(canUp()){ moveUp(); }
  else if(canLeft()){ moveLeft(); }
}
void move2(){  // 左下
  if(canDown()){ moveDown(); }
  else if(canLeft()){ moveLeft(); }
  else if(canUp()){ moveUp(); }
  else if(canRight()){ moveRight(); }
}
void move3(){  // 右上
  if(canUp()){ moveUp(); }
  else if(canRight()){ moveRight(); }
  else if(canDown()){ moveDown(); }
  else if(canLeft()){ moveLeft(); }
}
void move4(){  // 左上
  if(canUp()){ moveUp(); }
  else if(canLeft()){ moveLeft(); }
  else if(canDown()){ moveDown(); }
  else if(canRight()){ moveRight(); }
}
void move5(){  // 上
  if(canUp()){ moveUp(); }
  else if(canDown()){ moveDown(); }
  else if(canRight()){ moveRight(); }
  else if(canLeft()){ moveLeft(); }
}
void move6(){  // 左
  if(canLeft()){ moveLeft(); }
  else if(canRight()){ moveRight(); }
  else if(canUp()){ moveUp(); }
  else if(canDown()){ moveDown(); }
}
void move7(){  // 下
  if(canDown()){ moveDown(); }
  else if(canUp()){ moveUp(); }
  else if(canRight()){ moveRight(); }
  else if(canLeft()){ moveLeft(); }
}
void move8(){  // 右
  if(canRight()){ moveRight(); }
  else if(canLeft()){ moveLeft(); }
  else if(canUp()){ moveUp(); }
  else if(canDown()){ moveDown(); }
}
void bestmove(){
  vector<pairsull> a;
  if(canUp()){ a.push_back(make_pair("U",score[y-1][x])); }
  if(canDown()){ a.push_back(make_pair("D",score[y+1][x])); }
  if(canLeft()){ a.push_back(make_pair("L",score[y][x-1])); }
  if(canRight()){ a.push_back(make_pair("R",score[y][x+1])); }
  sort(a.begin(),a.end(),[](const pairsull &b,const pairsull &c){return b.second > c.second;});
  pairsull bestMove = a[0];
  if(bestMove.first == "U"){ moveUp(); }
  else if(bestMove.first == "D"){ moveDown(); }
  else if(bestMove.first == "L"){ moveLeft(); }
  else if(bestMove.first == "R"){ moveRight(); }
}

int main(){
  cin >> y >> x;
  loop(i,50){
    loop(j,50){
      cin >> tile[i][j];
    }
  }
  loop(i,50){
    loop(j,50){
      cin >> score[i][j];
    }
  }
  while(true){
    tap.push_back(tile[y][x]);
    if(canMove()){
      if(x<20 && y<20){ move1(); }
      else if(x<30 && y<20){ move7(); }
      else if(x<50 && y<20){ move2(); }
      else if(x<20 && y<30){ move8(); }
      else if(x<30 && y<30){ bestmove(); }
      else if(x<50 && y<30){ move6(); }
      else if(x<20 && y<50){ move3(); }
      else if(x<30 && y<50){ move5(); }
      else if(x<50 && y<50){ move4(); }
    }
    else{ break; }
  }
  cout << endl;
  return 0;
}
#include<bits/stdc++.h>
using namespace std;

#define F                   first
#define S                   second
#define int                 long long int
#define pb                  push_back
#define mp                  make_pair
#define pii                 pair<int,int>
#define vi                  vector<int>
#define mii                 map<int,int>
#define pqb                 priority_queue<int>
#define pqs                 priority_queue<int,vi,greater<int> >
#define mod                 1000000007
#define endl                '\n'
#define rep(i,a,b)            for(int i=a;i<b;i++)
#define db(...) __f(#__VA_ARGS__, __VA_ARGS__)

template <typename Arg1>
void __f(const char* name, Arg1&& arg1) { cout << name << " : " << arg1 << '\n'; }
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args) {
	const char* comma = strchr(names + 1, ',');
	cout.write(names, comma - names) << " : " << arg1 << " | "; __f(comma + 1, args...);
}
void c_p_c()
{
	ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif
}
void solve() {
	int n, k;
	cin >> n >> k;
	int a[n];
	int b[n];
	vector<pii> v;

	rep(i, 0, n) {
		cin >> a[i] >> b[i];
		v.pb({a[i], b[i]});
	}
	sort(v.begin(), v.end());
	int cur = 0;
	int left = k;
	rep(i, 0, n) {
		//db(cur, left);
		if (v[i].F - cur > left) {
			break;
		}
		else {
			left = left - (v[i].F - cur) + v[i].S;
			cur = v[i].F;
		}
	}
	//db(cur, left);
	int ans = cur + left;
	cout << ans << endl;
}
int32_t main()
{
	//c_p_c();
	int t = 1;
	// cin >> t;
	while (t--) {
		solve();
	}


	return 0;
}
