#include <bits/stdc++.h>
using namespace std;
 
#define REP(i, s) for (int i = 0; i < s; ++i)
#define ALL(v) (v).begin(), (v).end()
#define COUT(x) cout << #x << " = " << (x) << " (L" << __LINE__ << ")" << endl
#define EACH(i, s) for (__typeof__((s).begin()) i = (s).begin(); i != (s).end(); ++i)
#define DEBUG
//#define int long long
#define INF 1e18
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
template<class T1, class T2> ostream& operator << (ostream &s, pair<T1,T2> P)
{ return s << '<' << P.first << ", " << P.second << '>'; }
template<class T> ostream& operator << (ostream &s, vector<T> P)
{ for (int i = 0; i < P.size(); ++i) { if (i > 0) { s << " "; } s << P[i]; } return s; }
template<class T> ostream& operator << (ostream &s, vector<vector<T> > P)
{ for (int i = 0; i < P.size(); ++i) { s << endl << P[i]; } return s << endl; }
template<class T> ostream& operator << (ostream &s, set<T> P)
{ EACH(it, P) { s << "<" << *it << "> "; } return s << endl; }
template<class T1, class T2> ostream& operator << (ostream &s, map<T1,T2> P)
{ EACH(it, P) { s << "<" << it->first << "->" << it->second << "> "; } return s << endl; }
template<class T>void show(vector<T>v){for (int i = 0; i < v.size(); i++){cerr<<v[i]<<" ";}cerr<<"\n";}
typedef long long ll;

using Edge = pair<int, int>;
using Graph = vector<vector<Edge>>;

Graph G;
int n, d[250010];

// <最短距離, 頂点の番号>
using P = pair<int, int>;

void dijkstra(int s)
{
    priority_queue<P, vector<P>, greater<P>> que;
    fill(d, d + n, INF);
    d[s] = 0;
    que.push(P(0, s));

    while (!que.empty())
    {
        P p = que.top();
        que.pop();
        int v = p.second;
        if (d[v] < p.first)
            continue;

        for (int i = 0; i < G[v].size(); ++i)
        {
            Edge e = G[v][i];
            if (d[e.first] > d[v] + e.second)
            {
                d[e.first] = d[v] + e.second;
                que.push(P(d[e.first], e.first));
            }
        }
    }
}

int a[510][510];
int b[510][510];
signed main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int r,c;
    cin>>r>>c;
    n=r*c;
    G.assign(r*c , vector<Edge>());
    for (int i = 0; i < r; i++)
    {
        for (int j = 0; j < c-1; j++)
        {
            cin>>a[i][j];
        }
        
    }
    for (int i = 0; i < r-1; i++)
    {
        for (int j = 0; j < c; j++)
        {
            cin>>b[i][j];
            
        }
        
    }
    
    for (int i = 0; i < r; i++)
    {
        for (int j = 0; j < c; j++)
        {
            
            if(j<c-1) G[c*i + j].push_back({c*i+j+1, a[i][j]});
            if(j>=1) G[c*i + j].push_back({c*i+j-1, a[i][j-1]});
            if(i<r-1) G[c*i + j].push_back({c*(i+1)+j, b[i][j]});
        }
        
    }
    for (int i = 0; i < r; i++)
    {
        for (int j = 0; j < c; j++)
        {
            for (int k = 1; k < r; k++)
            {
                if(i - k < 0 ) continue;
                G[c*i+j].push_back({c*i+j - c*k , 1 + k});
            }
            
        }
        
    }
    dijkstra(0);
    cout<<d[r*c-1]<<endl;
    
    
    return 0;
}
#include <bits/stdc++.h>
typedef unsigned long long ull;
using namespace std;
const double maxv = 1e100;
ull n, k, money, ans, ai, bi;
vector <vector <long long> > ab;
int main() {
	cin >> n >> k;
	money = k;
	for(int i = 0; i < n; i++) {
		cin >> ai >> bi;
		vector <long long> abi;
		abi.push_back(ai);
		abi.push_back(bi);
		ab.push_back(abi);
	}
	sort(ab.begin(), ab.end());
	for(int i = 0; i < n && money > 0 && ans <= maxv; i++) {
		if((i > 0 && money < ab[i][0] - ab[i - 1][0]) || (i == 0 && money < ab[i][0])) {
			ans += money;
			money = 0;
			break;
		}
		if(i == 0) {
			money -= ab[i][0];
			money += ab[i][1];
			ans += ab[i][0];
		}
		else {
			money -= ab[i][0] - ab[i - 1][0];
			money += ab[i][1];
			ans += ab[i][0] - ab[i - 1][0];
		}
	}
	if(money > 0) ans += money;
	if(money < 0) ans -= 0 - money;
	cout << ans;
	return 0;
}