#include <bits/stdc++.h>

#define Fast cin.tie(0), ios::sync_with_stdio(0)
#define All(x) x.begin(), x.end()
#define louisfghbvc int t; cin >> t; for(int tt = 0; tt < t; ++tt)
#define sz(x) (int)(x).size()
#define sort_unique(x) sort(x.begin(), x.end()); x.erase(unique(x.begin(), x.end()));
using namespace std;
typedef long long LL;
typedef pair<LL, LL> ii;
typedef vector<LL> vi;

template<typename T> istream& operator>>(istream &is, vector<T> &v) { for(auto &it : v) is >> it; return is; }
template<typename T> ostream& operator<<(ostream &os, const vector<T> &v) { os << '{'; string sep = ""; for(const auto &x : v) os << sep << x, sep = ", "; return os << '}'; }
template<typename A, typename B> ostream& operator<<(ostream &os, const pair<A, B> &p) { return os << '(' << p.first << ", " << p.second << ')'; }

void dbg_out() { cerr << " end.\n"; }
template<typename Head, typename... Tail> void dbg_out(Head H, Tail... T) { cerr << ' ' << H; dbg_out(T...); }

const int N = 5e2+5;
const int INF = 0x3f3f3f3f;
/**
Read problem statement carefully
**/
int dp[N][N];
LL A[N][N], B[N][N];

int r, c;
void solve(int x){
    memset(dp, 0x3f, sizeof dp);
    
    cin >> r >> c;
    for(int i = 1; i <= r; ++i)
        for(int j= 1; j < c; ++j)
            cin >> A[i][j];
    
    for(int i = 1; i < r; ++i)
        for(int j = 1; j <= c; ++j)
            cin >> B[i][j];

    dp[1][1] = 0;
    priority_queue<ii, vector<ii>, greater<ii>> pq;
    pq.push({0, 0});

    while(sz(pq)){
        auto [cost, idx] = pq.top(); pq.pop();
        int x = idx/c, y = idx%c;
        x++, y++;
        if(cost > dp[x][y]) continue;
        dp[x][y] = cost;
        if(y < c){
            if(dp[x][y+1] > dp[x][y] + A[x][y]){
                dp[x][y+1] = dp[x][y] + A[x][y];
                pq.push({dp[x][y+1], (x-1) * c + y});
            }
        }
        if(y > 1){
            if(dp[x][y-1] > dp[x][y] + A[x][y-1]){
                dp[x][y-1] = dp[x][y] + A[x][y-1];
                pq.push({dp[x][y-1], (x-1) * c + y - 2});
            }
        }
        if(x < r){
            if(dp[x+1][y] > dp[x][y] + B[x][y]){
                dp[x+1][y] = dp[x][y] + B[x][y];
                pq.push({dp[x+1][y], x * c + y - 1});
            }
        }
        for(int i = 1; i < r && x - i > 0; ++i){
            int ga = 1 + i;
            if(dp[x][y] + ga < dp[x-i][y]){
                dp[x-i][y] = dp[x][y] + ga;
                pq.push({dp[x-i][y], (x - i - 1) * c + y-1}); 
            }
        }
    }

    cout << dp[r][c] << "\n";
}

int main()
{
    // Fast;
    // louisfghbvc
        solve(1);
    return 0;
}

/**
Enjoy the problem.
**/

//...Bismillahir Rahmanir Rahim. . .

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;

// typedefs...
typedef double db;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef pair<ll, ll> pll;
typedef trie<string,null_type,trie_string_access_traits<>,pat_trie_tag,trie_prefix_search_node_update>pref_trie;
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> oset;

// constants...
const double PI = acos(-1);
const ll mod = 998244353; // 1000000007;
const int MXS = 2e5+5;
const ll MXI = 1e9+5;
const ll MXL = 1e18+5;
const ll INF = 1e9+5;
const ll INFLL = 1e18+5;
const ll EPS = 1e-9;

// defines...
#define MP        make_pair
#define PB        push_back
#define fi         first
#define se         second
#define sz(x)      (int)x.size()
#define all(x)      begin(x), end(x)
#define si(a)       scanf("%d", &a)
#define sii(a, b)    scanf("%d%d", &a, &b)
#define ordered_set tree<ll,null_type,less_equal<ll>,rb_tree_tag,tree_order_statistics_node_update>
#define boost_      ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0);
#define iter_(i,n)    for (int i = 0; i < int(n); i++)
#define for_n(i, n)  for (int i = 1; i <= int(n); i++)
#define print_array(a) for(int i=0;i<n;i++)  cout<<a[i]<<" ";
#define rev(i,n)     for(int i=n;i>=0;i--)
#define itr          ::iterator
#define s_sort(s)    sort(s.begin(),s.end())
#define n_sort(a, n)    sort(a,a+n)
#define precise_impact cout<<setprecision(10)<<fixed;
#define endl         "\n"
// functions...
ll gcd(ll a, ll b){ while (b){ a %= b; swap(a, b);} return a;}
ll lcm(ll a, ll b){ return (a/gcd(a, b)*b);}
ll ncr(ll a, ll b){ ll x = max(a-b, b), ans=1; for(ll K=a, L=1; K>=x+1; K--, L++){ ans = ans * K; ans /= L;} return ans;}
ll bigmod(ll a,ll b){ if(b==0){ return 1;} ll tm=bigmod(a,b/2); tm=(tm*tm)%mod; if(b%2==1) tm=(tm*a)%mod; return tm;}
ll egcd(ll a,ll b,ll &x,ll &y){ if(a==0){ x=0; y=1; return b;} ll x1,y1; ll d=egcd(b%a,a,x1,y1); x=y1-(b/a)*x1; y=x1; return d;}
ll modpow(ll a,ll p) {ll ans=1;while(p){if(p%2)ans=(ans*a)%mod;a=(a*a)%mod;p/=2;} return ans;}
ll inverse_mod(ll n) {return modpow(n,mod-2);}
//ll ncr_mod(ll n,ll r) {return (((fact[n]*inverse_mod(fact[r]))%mod)*inverse_mod(fact[n-r]))%mod;}
int main()
{
    boost_;
    ll n,k;
    vector<pair<ll,ll> > v;
    cin>>n>>k;
    for(int i=0;i<n;i++)
    {
        ll num,x;
        cin>>num>>x;
        v.PB({num,x});
    }
    sort(all(v));
    ll pos=0;
    for(int i=0;i<n;i++)
    {
        if(pos+k<v[i].fi) {cout<<pos+k<<endl;return 0;}

        k=k-(v[i].fi-pos)+v[i].se;
        pos=v[i].fi;
    }
    cout<<pos+k<<endl;
}
