#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define FAST ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#define ALL(a) a.begin(), a.end()
#define RALL(a) a.rbegin(), a.rend()
#define pb push_back
#define MOD 1000000007
#define sz5 100005
#define sz6 1000005
#define UP upper_bound
#define LB lower_bound
#define F first
#define S second
#define PI pair<int,int>
#define PL pair<ll,ll>
#define VI vector<int>
#define VL vector<ll>
#define pr pair<int, pair<int,int>>

const ll inf = 1e9;

int r,c;
int A[505][505], B[505][505];

int main() {
    FAST

    int la,lb;
    cin>>r>>c;
    for(la=1;la<=r;la++){
        for(lb=1;lb<c;lb++)
            cin>>A[la][lb];
    }
    for(la=1;la<r;la++){
        for(lb=1;lb<=c;lb++)
            cin>>B[la][lb];
    }

    priority_queue<pr, vector<pr>, greater<pr>> q;
    bool mk[505][505]={0};

    q.push({0, {1, 1}});
    vector<vector<int>> dis(505, vector<int>(505, inf));
    dis[1][1]=0;

    while(!q.empty()){
        int d=q.top().first, x=q.top().second.first, y=q.top().second.second;
        q.pop();

        if(dis[x][y] != d)
            continue;

        if(x==r && y==c){
            cout<<d<<endl;
            return 0;
        }

        if(y<c && dis[x][y+1] > dis[x][y]+A[x][y]){
            dis[x][y+1] = dis[x][y]+A[x][y];
            q.push({dis[x][y+1], {x, y+1}});
        }
        if(y>1 && dis[x][y-1] > dis[x][y]+A[x][y-1]){
            dis[x][y-1] = dis[x][y]+A[x][y-1];
            q.push({dis[x][y-1], {x, y-1}});
        }
        if(x<r && dis[x+1][y] > dis[x][y]+B[x][y]){
            dis[x+1][y] = dis[x][y]+B[x][y];
            q.push({dis[x+1][y], {x+1, y}});
        }

        for(la=x-1, lb=1;la>=1;la--,lb++){
            if(dis[la][y] > dis[x][y]+1+lb){
                dis[la][y] = dis[x][y]+1+lb;
                q.push({dis[la][y], {la, y}});
            }
        }
    }
    

    return 0;
}
#include <bits/stdc++.h>
using namespace std;

#define pb push_back
#define S second
#define F first
#define f(i,n) for(int i=0;i<n;i++)
#define fast ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define vi vector<int>
#define pii pair<int,int>
#define all(x) x.begin(),x.end()

const int N = 505;
const int inf = 1e9;

int n,m;
int a[N][N];
int b[N][N];

vector<pii> g[2*N*N];
vector<bool> vis(2*N*N,0);
vector<int> dis(2*N*N,inf);

inline int encode(int i,int j)
{
    return (i*m) + j;
}

void solve()
{
   cin >> n >> m;
   
   f(i,n) f(j,m-1) cin >> a[i][j];
   f(i,n-1) f(j,m) cin >> b[i][j];
    
   f(i,n) f(j,m)
   {
       if(j > 0) g[encode(i,j)].push_back({encode(i,j-1),a[i][j-1]});
       if(j < m-1) g[encode(i,j)].push_back({encode(i,j+1),a[i][j]});
       if(i < n-1) g[encode(i,j)].push_back({encode(i+1,j),b[i][j]});
       if(i > 0) g[encode(i,j) + (n*m)].push_back({encode(i-1,j) + (n*m),1});
   
       g[encode(i,j)].push_back({encode(i,j) + (n*m),1});
       g[encode(i,j) + (n*m)].push_back({encode(i,j),0});
   }
    
   dis[0] = 0;
   priority_queue<pii,vector<pii>,greater<pii> > PQ;
   PQ.push({0,0}); 
  
   while(!PQ.empty())
   {
       auto x = PQ.top();  PQ.pop();
       if(vis[x.S] == 1) continue;
       vis[x.S] = 1;
      
       for(auto z : g[x.S])
       {
           if(dis[z.F] > dis[x.S] + z.S)
           {
               dis[z.F] = dis[x.S] + z.S;
               PQ.push({dis[z.F],z.F});
           }
       }
   }  
    
   cout << dis[n*m - 1]; 
}

signed main()
{
    fast;
    solve();
}