#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
#define f(i,a,b) for( ll i = a; i < b ; i++ )
#define af(i,a,b) for( ll i = a; i >= b ; i--)
#define rep(i,a,b,k) for(ll i = a; i < b ; i+= k )
#define arep(i,a,b,k) for( ll i = a; i >= b ; i-= k)
#define ones(ini) (ll) __builtin_popcountll(x)
#define todo(y,val) all_of(all(y),[&val](ll x) {return (x == ((ll)val));})
#define nada(y,val) none_of(all(y),[&val](ll x) {return (x == ((ll)val));})
#define algu(y,val) any_of(all(y),[&val](ll x) {return (x == ((ll)val));})
#define cuantos(a,x) count(all(a),x)
#define fs first
#define sc second
#define pb push_back
#define sz(a) (ll) a.size()
#define all(a) a.begin(), a.end()
#define sor(a) sort( a.begin(), a.end() )
#define fastio ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0)
#define ller ios::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr)
#define watch(x) cout << (#x) << " is " << (x) <<"\n"
#define test ll deftestcases;cin>>deftestcases;while(deftestcases--)
#define PI acos(-1)
using namespace std;
typedef long long ll;
typedef long double ld; 
typedef pair<ll,ll> ii;
typedef vector<ll>  vi;
typedef vector<ii> vii;
template<class T> using ordered_set =tree<T, null_type, less<T>, rb_tree_tag,tree_order_statistics_node_update> ;
const ll inf = 2e9;
const ll mod = 1e9+7;   
const ll MAX = 6e2+10;

struct celda{
    ll u=0,v=0,dis=0;
    bool operator<(const celda& other) const{
        return dis>other.dis;
    }
};

ll dis[MAX][MAX],a[MAX][MAX],b[MAX][MAX];
ll n,m;
priority_queue<celda> pq;

void fun(ll u,ll v,ll dd){
    if(dis[u][v]>dd){
        dis[u][v] = dd;
        celda aux;
        aux.u = u,aux.v = v,aux.dis = dd;
        pq.push(aux);
    }
}

void dijkstra(){
    f(i,0,n+4){
        f(j,0,m+4){
            dis[i][j] = inf;
        }
    }
    fun(0,0,0);
    while(sz(pq)){
        auto pos = pq.top();
        if(pos.u==n-1 && pos.v == m-1){
            cout<<dis[pos.u][pos.v]<<"\n";return;
        }
        pq.pop();
        if(dis[pos.u][pos.v]<pos.dis) continue;
        if(pos.v<m-1){
            fun(pos.u,pos.v+1,pos.dis+a[pos.u][pos.v]);
        }
        if(pos.v>0){
            fun(pos.u,pos.v-1,pos.dis+a[pos.u][pos.v-1]);
        }
        if(pos.u<n-1){
            fun(pos.u+1,pos.v,pos.dis+b[pos.u][pos.v]);
        }
        f(uwu,1,pos.u+1){
            fun(pos.u-uwu,pos.v,pos.dis+uwu+1);
        }
    }
}


int main(){
    fastio;
    cin>>n>>m;
    f(i,0,n){
        f(j,0,m-1) cin>>a[i][j];
    }
    f(i,0,n-1){
        f(j,0,m) cin>>b[i][j];
    }
    dijkstra();
}
#include <iostream>
#include <algorithm>
#include <stack>
#include <queue>
#include <vector>
#include <bitset>
#include <iomanip>
#include <cmath>
#include <set>
#include <unordered_set>
#include <functional>
#include <map>
#include <unordered_map>
#include <string>
#include <tuple>
using namespace std;
using ll = long long;
const int MOD=1e9+7;
const int MAX=100009;
ll fac[100009],finv[100009],inv[100009];

#define pi 3.14159265358979323846
#define ee 2.71828182845904523536
#define INF 5e18

ll gcd(ll x, ll y) { // 最大公約数
    if (y == 0)return x;
    else return gcd(y, x % y);
}

bool prime(ll x) { //素数判定
    if(x==1)return false;
    if (x!=2&&x % 2 == 0)return false;
    for (ll i = 3; i <= sqrt(x); i+=2) {
        if (x % i == 0)return false;
    }
    return true;
}

ll modinv(ll a, ll m) { //逆元
    long long b = m, u = 1, v = 0;
    while (b) {
        long long t = a / b;
        a -= t * b; swap(a, b);
        u -= t * v; swap(u, v);
    }
    u %= m;
    if (u < 0) u += m;
    return u;
}

ll modpow(ll a, ll n, ll mod) { //二分累乗法
    ll res = 1;
    while (n) {
        if (n % 2)res = res * a % mod;
        a = a * a % mod;
        n /= 2;
    }
    return res;
}

void COMinit() { //二項係数計算の前処理
    fac[0] = fac[1] = 1;
    finv[0] = finv[1] = 1;
    inv[1] = 1;
    for (int i = 2; i < MAX; i++){
        fac[i] = fac[i - 1] * i % MOD;
        inv[i] = MOD - inv[MOD%i] * (MOD / i) % MOD;
        finv[i] = finv[i - 1] * inv[i] % MOD;
    }
}

ll COM(int n, int k){ //二項係数計算
    if (n < k) return 0;
    if (n < 0 || k < 0) return 0;
    return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}

struct UnionFind {   // UnionFind
    vector<int> par;
    vector<int> siz;

    UnionFind(int N) : par(N), siz(N, 1) {
        for (int i = 0; i < N; i++) par[i] = i;
    }

    int root(int x) {
        if (par[x] == x) return x;
        return par[x] = root(par[x]);
    }

    void unite(int x, int y) {
        int rx = root(x);
        int ry = root(y);
        if (rx == ry) return;
        if (par[rx] > par[ry])swap(rx, ry);
        siz[rx] += siz[ry];
        par[ry] = rx;
    }

    bool same(int x, int y) {
        int rx = root(x);
        int ry = root(y);
        return rx == ry;
    }
    int size(int x) {
        return siz[root(x)];
    }
};

int main() {
    int r,c;
    cin>>r>>c;
    int a[500][500];
    int b[500][500];
    for(int i=0;i<r;i++){
        for(int j=0;j<c-1;j++)cin>>a[j][i];
    }
    for(int i=0;i<r-1;i++){
        for(int j=0;j<c;j++)cin>>b[j][i];
    }
    priority_queue<tuple<int,int,int>,vector<tuple<int,int,int>>,greater<tuple<int,int,int>>> pq;
    pq.push(make_tuple(0,0,0));
      vector<vector<int>> co(c+1,vector<int>(r+1,5e9));
    --r;--c;
    co[0][0]=0;
    while(!pq.empty()){
        auto cur=pq.top();
        pq.pop();
        int x=get<1>(cur);
        int y=get<2>(cur);
        int cost=get<0>(cur);
        if(x==c&&y==r){
            cout<<cost<<endl;
            return 0;
        }
        if(x<c&&co[x+1][y]>cost+a[x][y]){
            pq.push(make_tuple(cost+a[x][y],x+1,y));
            co[x+1][y]=cost+a[x][y];
        }
        if(x>0&&co[x-1][y]>cost+a[x-1][y]){
            pq.push(make_tuple(cost+a[x-1][y],x-1,y));
            co[x-1][y]=cost+a[x-1][y];
        }
        if(y<r&&co[x][y+1]>cost+b[x][y]){
            pq.push(make_tuple(cost+b[x][y],x,y+1));
            co[x][y+1]=cost+b[x][y];
        }
        for(int i=1;i<=y;i++){
            if(co[x][y-i]>cost+1+i){
                pq.push(make_tuple(cost+1+i,x,y-i));
                co[x][y-i]=cost+1+i;
            }
        }
    }
}           