#include <iostream>
#include <string>
#include <vector>
#include <queue>
#include <utility>
#include <algorithm>
#include <cmath>
#include <deque>
 
using ll=long long;
using namespace std;
 
int main(){
    int R,C;
    cin>>R>>C;
 
    vector<vector<int>> A(R,vector<int>(C-1));
    vector<vector<int>> B(R-1,vector<int>(C));
 
    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];
        }
    }
 
 
    ll INF = 1e15;
    vector<vector<ll>> dp(R,vector<ll>(C,INF));
    dp[0][0] = 0;
    priority_queue<pair<ll, pair<int,int>>> q;
    q.push({0,{0,0}});
    while(!q.empty()){
        auto recv = q.top();
        ll nowc = recv.first*-1;
        int nx = recv.second.first;
        int ny = recv.second.second;
        q.pop();
        //cout<<nowc<<" "<<nx<<" "<<ny<<endl;
        /*
        if(dp[nx][ny] <= nowc){
            continue;
        }else{
            dp[nx][ny] = nowc;
        }
        //*/
        if(nx == R-1 && ny == C-1){
            break;
        }
        
        if(ny < C-1){
            ll cos = nowc+A[nx][ny];
            if(dp[nx][ny+1] > cos){
                dp[nx][ny+1] = cos;
                q.push({-1*cos, {nx, ny+1}});
            }
        }
        if(ny > 0){
            ll cos = nowc+A[nx][ny-1];
            if(dp[nx][ny-1] > cos){
                dp[nx][ny-1] = cos;
                q.push({-1*(nowc+A[nx][ny-1]), {nx,ny-1}});
            }
        }
        if(nx < R-1){
            ll cos = nowc+B[nx][ny];
            if(dp[nx+1][ny] > cos){
                dp[nx+1][ny] = cos;
                q.push({-1*(nowc+B[nx][ny]), {nx+1,ny}});
            }
        }
        //*
        for(int i=1;i<=nx;i++){
            if(nowc+1+i > dp[nx-i][ny]){
                break;
            }
            dp[nx-i][ny] = nowc+1+i;
            q.push({-1*(nowc+1+i), {nx-i,ny}});
        }
        //*/
        /*
        if(nx != 0){
            q.push({-1*(nowc)})
        }
        //*/
    }
    cout<<dp[R-1][C-1]<<endl;
 
    return 0;
}
#include<bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<int,int> P;

#define p_ary(ary,a,b) do { cout << "["; for (int count = (a);count < (b);++count) cout << ary[count] << ((b)-1 == count ? "" : ", "); cout << "]\n"; } while(0)
#define p_map(map,it) do {cout << "{";for (auto (it) = map.begin();;++(it)) {if ((it) == map.end()) {cout << "}\n";break;}else cout << "" << (it)->first << "=>" << (it)->second << ", ";}}while(0)

template<typename T1,typename T2>ostream& operator<<(ostream& os,const pair<T1,T2>& a) {os << "(" << a.first << ", " << a.second << ")";return os;}

const char newl = '\n';


template <typename T>
void dijkstra(int s,vector<vector<pair<int,T>>>& edges,vector<T>& dist) {
    priority_queue<pair<T,int>,vector<pair<T,int>>,greater<pair<T,int>>> que;
    dist[s] = 0;
    que.push(make_pair(0,s));
    while (!que.empty()) {
        pair<ll,int> p = que.top();
        que.pop();
        int v =  p.second;
        if (dist[v] < p.first) continue;
        for (pair<int,T>& e : edges[v]) {
            if (dist[e.first] > dist[v]+e.second) {
                dist[e.first] = dist[v]+e.second;
                que.push(make_pair(dist[e.first],e.first));
            }
        }
    }
}

int r,c;
int coord(int i,int j) {
    return i*c+j;
}

int main() {
    cin >> r >> c;
    vector<vector<pair<int,ll>>> edges(r*c*2);
    for (int i = 0;i < r;++i) for (int j = 0;j < c-1;++j) {
        int a;
        scanf("%d",&a);
        edges[coord(i,j)].push_back(make_pair(coord(i,j+1),a));
        edges[coord(i,j+1)].push_back(make_pair(coord(i,j),a));
    }
    
    for (int i = 0;i < r-1;++i) for (int j = 0;j < c;++j) {
        int a;
        scanf("%d",&a);
        edges[coord(i,j)].push_back(make_pair(coord(i+1,j),a));
    }
    for (int i = 0;i < r;++i) for (int j = 0;j < c;++j) {
        edges[coord(i,j)].push_back(make_pair(coord(i,j)+r*c,0));
        edges[coord(i,j)+r*c].push_back(make_pair(coord(i,j),1));
    }
    for (int i = 1;i < r;++i) for (int j = 0;j < c;++j) {
        edges[coord(i,j)+r*c].push_back(make_pair(coord(i-1,j)+r*c,1));
    }
    vector<ll> dist(2*r*c,1e18);
    dijkstra(0,edges,dist);
    cout << dist[coord(r-1,c-1)] << newl;
}