#include "bits/stdc++.h"
using namespace std;
using ll = long long;
const ll MOD = 998244353;
const double PI = 3.141592653589793238;
int main(){
  auto start = chrono::system_clock::now();
  ll si, sj;
  cin >> si >> sj;
  ll nowi = si, nowj = sj;
  vector<ll> t(2500);
  for (ll i = 0; i < 50; ++i) {
    for (ll j = 0; j < 50; ++j) {
      cin >> t[50 * i + j];
    }
  }

  vector<ll> p(2500);
  for (ll i = 0; i < 50; ++i) for (ll j = 0; j < 50; ++j) cin >> p[50 * i + j];

  string lastans = "";
  ll lastpnt = 0;
  map<ll, bool> used;
  string ans = "";
  while (true) {
    ll id = 50 * nowi + nowj;
    ll maxi = -1;
    used[t[id]] = true;
    char togo = ' ';
    if (nowj != 0 && !used[t[id - 1]] && maxi < p[id - 1]) {
      togo = 'L';
      maxi = p[id - 1];
    }
    if (nowj != 49 && !used[t[id + 1]] && maxi < p[id + 1]) {
      togo = 'R';
      maxi = p[id + 1];
    }
    if (nowi != 0 && !used[t[id - 50]] && maxi < p[id - 50]) {
      togo = 'U';
      maxi = p[id - 50];
    }
    if (nowi != 49 && !used[t[id + 50]] && maxi < p[id + 50]) {
      togo = 'D';
      maxi = p[id + 50];
    }
    if(togo != ' ')ans += togo;
    switch (togo)
    {
    case 'L': --nowj;
      break;
    case 'R': ++nowj;
      break;
    case 'U': --nowi;
      break;
    case 'D': ++nowi;
      break;
    default:
      goto exit;
    }
  }
exit:;
  nowi = si, nowj = sj;
  lastans = ans;
  ll now = 50 * nowi + nowj;
  lastpnt += p[now];
  for (ll i = 0; i < ans.size(); ++i) {
    switch (ans[i]) {
    case 'L': --nowj;
      break;
    case 'R': ++nowj;
      break;
    case 'U': --nowi;
      break;
    case 'D': ++nowi;
      break;
    }
    lastpnt += p[now];
  }

  string sample = "LRUD";
  random_device rnd;
  mt19937 mt(rnd());
  uniform_int_distribution<> cha(0, 3), rand(1,1000);
  auto dur = chrono::system_clock::now() - start;

  while (chrono::duration_cast<std::chrono::milliseconds>(dur).count() < 1980) {
    ans = lastans;

    uniform_int_distribution<> change(0, ans.size());
    ll ch = change(mt), at = cha(mt), r = rand(mt);
    if (ch == ans.size()) ans.push_back(sample[at]);
    else ans[ch] = sample[at];
    
    nowi = si, nowj = sj;
    now = 50 * nowi + nowj;
    map<ll, bool> used;
    used[t[now]] = true;
    ll pnt = p[now];
    for (ll i = 0; i < ans.size(); ++i) {
      switch (ans[i]) {
      case 'L': --nowj;
        break;
      case 'R': ++nowj;
        break;
      case 'U': --nowi;
        break;
      case 'D': ++nowi;
        break;
      }
      if (nowi < 0 || 49 < nowi || nowj < 0 || 49 < nowj) {
        pnt = -1;
        break;
      }
      now = 50 * nowi + nowj;
      if (used[t[now]]) {
        pnt = -1;
        break;
      }
      used[t[now]] = true;
      pnt += p[now];
    }
    if (pnt >= lastpnt) {
      lastpnt = pnt;
      lastans = ans;
    }
    else if(pnt != -1 && r <= 125){
      lastpnt = pnt;
      lastans = ans;
    }
    dur = chrono::system_clock::now() - start;
  }
  cout << lastans << "\n";
}

// C++ (GCC9.2.1) AtCoder
/*
Ctrl+Option+N で　実行

*/

#include <bits/stdc++.h>
#define INF 100000000000
#define MOD 1000000007
#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define all(x) (x).begin(),(x).end()
#define print(x) cout << (x) << endl;

typedef long long ll;

using namespace std;


int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr); 
  ll N,K;
  cin>>N>>K;

  ll last = -1;

  map <ll,ll> mp;

  REP(i,N){
    ll a,b;
    cin>>a>>b;
    mp[a] += b;
  }

  ll ans = 0;
  ll money = K;
  ll now = 0;

  for(auto itr = mp.begin();itr != mp.end(); itr++ ){
    ll next = itr -> first;
    ll get = itr -> second;
    //print(next + 10000)
    //print(money)

    if(money - (next - now) >= 0){
      money -= (next - now);
      now = next;
      money = money + get ;
      continue;
    } else {
      print(now + money)
      return 0;
    }

  }
  //print(money)

  assert(money >= 0);
  now += money;
  print(now);





  return 0;
}