#include<bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef long double ld;


const ll maxn=3e5+10;
const ll mod=1e9+7;
const ll m2=1e9+7;
const ll INF64 = ll(1e18);
const ll max2=10500000;
const ll N = 1000001;
const ll MAXN=2e5+10;
const ll ALPHABET_SIZE = 2;
vector<pair<int,int>> adj[maxn];

int main()
{
	#ifndef ONLINE_JUDGE
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
//long long tt = clock();
#endif

ios_base::sync_with_stdio(NULL);  cin.tie(0); cout.tie(0);
int n,m,i,j,k;
cin>>n>>m;
int A[n][m-1],B[n-1][m];
for(i=0;i<n;++i)
for(j=0;j<m-1;++j)
	cin>>A[i][j];
for(i=0;i<n-1;++i)
	for(j=0;j<m;++j)
		cin>>B[i][j];
int m1[n][m];
int c=0;
for(i=0;i<n;++i)
{
	for(j=0;j<m;++j)
	{
		m1[i][j]=c++;

	}
}

for(i=0;i<n;++i)
{
	for(j=0;j<m;++j)
	{
		if(j-1>=0)
		{
			adj[m1[i][j]].push_back(make_pair(m1[i][j-1],A[i][j-1]));

		}
		if(j+1<m)
		{
			adj[m1[i][j]].push_back(make_pair(m1[i][j+1],A[i][j]));

		}
		if(i+1<n)
		{
			adj[m1[i][j]].push_back(make_pair(m1[i+1][j],B[i][j]));

		}
		for(k=0;k<max(n,m);++k)
		{
			if(i-k>=0)
			{
				adj[m1[i][j]].push_back(make_pair(m1[i-k][j],1+k));

			}
			else
				break;
		}
	}
}
int sz=n*m;
int dis[sz];
set<pair<int,int>> st;
for(i=0;i<sz;++i)
{
	dis[i]=INT_MAX;

}
dis[0]=0;
st.insert(make_pair(0,0));
while(st.size()!=0)
{
	int u=(*st.begin()).second;
	int d=(*st.begin()).first;
	st.erase(st.begin());
	//cout<<u<<"\n";
	for(auto x:adj[u])
	{
		int v=x.first,w=x.second;
		
		if(dis[v]>dis[u]+w)
		{
			if(st.find(make_pair(dis[v],v))!=st.end())
				st.erase(st.find(make_pair(dis[v],v)));
			dis[v]=dis[u]+w;
			st.insert(make_pair(dis[v],v));
		}
	}
}
cout<<dis[m1[n-1][m-1]];


return(0);
}

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep2(i, j, k) for(int i = j; i < k; i++)
#define print(x) cout << x
#define newline cout << endl
#define space cout << ' '
#define INF 1000000000007
using namespace std;
using ll = long long;
using vi = vector<int>;
using vl = vector<ll>;

int main(){
    int n;
    cin >> n;
    vector<vector<int>> x(n+1);
    int t1,t2;
    rep(i,n){
        cin >> t1 >> t2;
        x[t2].push_back(t1);
    }
    int l=0,r=0;
    vector<vector<ll>> dp(n+1,vector<ll>(2));
    dp[0][0]=0; dp[0][1]=0;
    rep(i,n){
        if(x[i+1].size()==0){
            dp[i+1][0]=dp[i][0];
            dp[i+1][1]=dp[i][1];
            continue;
        }
        else{
            sort(x[i+1].begin(),x[i+1].end());
            ll l2l,l2r,r2l,r2r;
            if(l<=x[i+1][0]){
                l2r=dp[i][0]+x[i+1][x[i+1].size()-1]-l;
                l2l=dp[i][0]+2*x[i+1][x[i+1].size()-1]-l-x[i+1][0];
            }
            else if(l<x[i+1][x[i+1].size()-1]){
                l2r=dp[i][0]+l+x[i+1][x[i+1].size()-1]-2*x[i+1][0];
                l2l=dp[i][0]+2*x[i+1][x[i+1].size()-1]-l-x[i+1][0];
            }
            else{
                l2r=dp[i][0]+l+x[i+1][x[i+1].size()-1]-2*x[i+1][0];
                l2l=dp[i][0]+l-x[i+1][0];   
            }
            if(r<=x[i+1][0]){
                r2r=dp[i][1]+x[i+1][x[i+1].size()-1]-r;
                r2l=dp[i][1]+2*x[i+1][x[i+1].size()-1]-r-x[i+1][0];
            }
            else if(r<x[i+1][x[i+1].size()-1]){
                r2r=dp[i][1]+r+x[i+1][x[i+1].size()-1]-2*x[i+1][0];
                r2l=dp[i][1]+2*x[i+1][x[i+1].size()-1]-r-x[i+1][0];
            }
            else{
                r2r=dp[i][1]+r+x[i+1][x[i+1].size()-1]-2*x[i+1][0];
                r2l=dp[i][1]+r-x[i+1][0];   
            }
            dp[i+1][0]=min(l2l,r2l);
            dp[i+1][1]=min(l2r,r2r);
            l=x[i+1][0];
            r=x[i+1][x[i+1].size()-1];
        }
    }
    ll ans=min(dp[n][0]+abs(l),dp[n][1]+abs(r));
    print(ans);
    return 0;
}