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

const long long MOD = 1e9+7;
const long long N = 25e4+10;
 
#define ll int
// typedef long long ll;
typedef long double ld;

ll r, c, n;

ll get(ll i, ll j) {
    return (i * c + j);
}

vector<array<int, 2>> g[N];
int d[N];
 
void dijkstra(int s) {
    fill(d, d+n+1, MOD);
   d[s] = 0;
   using pii = pair<int, int>;
   priority_queue<pii, vector<pii>, greater<pii>> q;
   q.push({0, s});
   while (!q.empty()) {
      int v = q.top().second;
      int d_v = q.top().first;
      q.pop();
      if (d_v != d[v])
         continue;
      for (auto [to, len] : g[v]) {
         if (d[v] + len < d[to]) {
            d[to] = d[v] + len;
            q.push({d[to], to});
         }
      }
   }
}


void test_case() { 
    cin >> r >> c;
    n = r * c;
    vector<vector<ll>> a(r, vector<ll>(c)), b(r, vector<ll>(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];
        }
    }

    for(int i = 0; i < r; i++) {
        for(int j = 0; j < c-1; j++) {
            ll x = get(i, j);
            ll y = get(i, j+1);
            g[x].push_back({y, a[i][j]});
            g[y].push_back({x, a[i][j]});
        }
    }
    for(int i = 0; i < r-1; i++) {
        for(int j = 0; j < c; j++) {
            ll x = get(i, j);
            ll y = get(i+1, j);
            g[x].push_back({y, b[i][j]});
        }
    }
    for(int i = 0; i < r; i++) {
        for(int j = 0; j < c; j++) {
            for(int k = 1; k <= i; k++) {
                ll x = get(i, j);
                ll y = get(i-k, j);
                g[x].push_back({y, 1+k});
            }
        }
    }
    ll st = get(0, 0);
    ll ed = get(r-1, c-1);
    dijkstra(st);
    cout << d[ed] << '\n';



}

int32_t main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL); cout.tie(NULL);
    int tc = 1;
    // cin >> tc;
    while(tc--)
        test_case();
    return 0;
}
#include <bits/stdc++.h>
using namespace std;
#define int long long
using vec_int = vector<int>;
using P = pair<int,int>;
using T = tuple<int,int,int>;
using T2 = tuple<int,int,int, int>;
using ll = long long;
#define rep(i, n) for(int i = 0; i < (int)(n); i++)

int charToInt(char c){
    char zero_num = '0';
    return (int)c - (int)zero_num;
}

signed main(){

    int R, C; cin>>R>>C;
    vector<vec_int> A(R, vec_int(C-1));
    rep(i,R)rep(j,C-1)cin>>A.at(i).at(j);

    vector<vec_int> B(R-1, vec_int (C));
    rep(i,R-1)rep(j,C)cin>>B.at(i).at(j);

    vector<vec_int> visited(R, vec_int(C, 0));

    priority_queue<T2, vector<T2>, greater<T2>> pq;
    pq.emplace(0, 0, 0, 0);



    while(!pq.empty()){
        int cost, r, c, type; tie(cost, r, c, type)=pq.top(); pq.pop();
        if(visited.at(r).at(c)==1)continue;
        if(r==R-1&&c==C-1){
            cout<<cost<<endl;
            return 0;
        }
        visited.at(r).at(c) = 1;
        if(c<C-1){
            if(visited.at(r).at(c+1)==0){
                pq.emplace(cost+A.at(r).at(c), r, c+1, 1);
            }
        }
        if(c>0){
            if(visited.at(r).at(c-1)==0){
                pq.emplace(cost+A.at(r).at(c-1), r, c-1, 1);
            }
        }
        if(r<R-1){
            if(visited.at(r+1).at(c)==0){
                pq.emplace(cost+B.at(r).at(c), r+1, c, 0);
            }
        }
        if(type==1){
            for(int i=0;i<r;i++){
                if(visited.at(i).at(c)==0){
                    pq.emplace(cost+(r-i+1), i, c, 0);
                }
            }
        }
    }





    return 0;
}