// #pragma GCC optimize("Ofast,unroll-loops")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,tune=native")
#include <bits/stdc++.h>
#define ll long long int
#define vi vector<int>
#define vvi vector<vector<int>>
#define vll vector<long long>
#define vs vector<string>
#define vc vector<char>
#define vb vector<bool>
#define forn(i, s, n)  for(ll i=(ll)s; i<(ll)(n); i++)
#define all(c) c.begin(),c.end()
#define pb push_back
#define pll pair<long long int, long long int>
#define pii pair<int, int>
#define lld long double
#define F first
#define S second
#define PI 3.141592653589793238
#define prec(n) fixed<<setprecision(n)
#define ordered_set tree<ll, null_type,less<ll>, rb_tree_tag,tree_order_statistics_node_update>
#define debug(...) fprintf(stderr, __VA_ARGS__), fflush(stderr)
#define itsval(args...) { string _s = #args; replace(_s.begin(), _s.end(), ',', ' '); stringstream _ss(_s); istream_iterator<string> _it(_ss); itval(_it, args); }
using namespace std;
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
// using namespace __gnu_pbds;

void itval(istream_iterator<string> it) {}
template<typename T, typename... Args>
void itval(istream_iterator<string> it, T a, Args... args) {
    cerr << *it << " = " << a << endl;
    itval(++it, args...);
}
const ll MOD = 1e9 + 7;
template <typename T>
inline void print(T x) {cout << x << "\n";}
template <typename T>
inline void printvec(T x) {for (auto a : x)cout << a << ' '; cout << '\n';}
// -----------------------------------------------------------------------

struct custom {
    bool operator()(const pair<int, pii> &p1, const pair<int, pii> &p2)const {
        if (p1.F == p2.F)
            return p1.S.F > p2.S.F;
        return p1.F > p2.F;
    }
};

// Calculate a^b % MOD -------------------------------------------------
ll get_pow(ll a, ll b, ll M = MOD) {
    ll res = 1;
    while (b) {
        if (b & 1) res = (res * a) % M;
        a = (a * a) % M;
        b >>= 1;
    }
    return res;
}
// ---------------------------------------------------------------------
const ll N = 3e5 + 5, inf = 2e9;
std::vector<pii> adj[N];
int a[502][502], b[502][502], ind[502][502], dist[N];
void solve()
{
    int R, C;
    cin >> R >> C;
    forn(i, 1, R + 1)
    {
        forn(j, 1, C)
        {
            cin >> a[i][j];
        }
    }
    forn(i, 1, R)
    {
        forn(j, 1, C + 1)
        cin >> b[i][j];
    }
    forn(i, 1, R + 1)
    {
        forn(j, 1, C + 1) {
            ind[i][j] = j + C * i;
            dist[ind[i][j]] = inf;
        }
    }

    forn(i, 1, R + 1)
    {
        forn(j, 1, C + 1)
        {
            if (j < C)
            {
                adj[ind[i][j]].pb({a[i][j], ind[i][j + 1]});
            }
            if (j > 1)
            {
                adj[ind[i][j]].pb({a[i][j - 1], ind[i][j - 1]});
            }
            if (i < R)
            {
                adj[ind[i][j]].pb({b[i][j], ind[i + 1][j]});
            }
            forn(k, 1, i)
            {
                adj[ind[i][j]].pb({1 + k, ind[i - k][j]});
            }
        }
    }
    dist[ind[1][1]] = 0;
    set<pii>s;
    s.insert({0, ind[1][1]});
    while (!s.empty())
    {
        pll t = *s.begin();
        s.erase(s.begin());
        for (auto x : adj[t.S])
        {
            if (dist[x.S] > x.F + t.F)
            {
                s.erase({dist[x.S], x.S});
                dist[x.S] = x.F + t.F;
                s.insert({dist[x.S], x.S});
            }
        }
    }

    cout << dist[ind[R][C]] << '\n';
}


int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif

    int test = 1;
    clock_t z = clock();
    //cin >> test;
    forn(tes, 0, test)
    {

        solve();
    }
    debug("Total Time:%.4f\n", (double)(clock() - z) / CLOCKS_PER_SEC);
    return 0;
}
#include<bits/stdc++.h>

using namespace std;
 
#define  GODSPEED       ios:: sync_with_stdio(0);cin.tie(0);cout.tie(0);cout<<fixed;cout<<setprecision(15);
#define  f              first                 
#define  s              second
#define  newl           cout<<"\n";
#define  pb             push_back  
#define  mset(a,x)      memset(a,x,sizeof(a))
#define  debv(a)        for(auto it: a)cout<<it<<" ";newl;
#define  deb1(a)        cout<<a<<"\n";
#define  deb2(a,b)      cout<<a<<" "<<b<<"\n";
#define  deb3(a,b,c)    cout<<a<<" "<<b<<" "<<c<<"\n";
#define  deb4(a,b,c,d)  cout<<a<<" "<<b<<" "<<c<<" "<<d<<"\n";
#define  uniq(a)        a.resize(unique(a.begin(), a.end()) - a.begin());
#define  all(a)         a.begin(),a.end()           
 
typedef  long long             ll;
typedef  long double           ld;
typedef  pair<ll,ll>           pll;
typedef  vector<ll>            vll;
typedef  vector<pll>           vpll;

const ll   N     =  1e5+5;
const ll   mod   =  998244353;
const ll   INF   =  0x7f7f7f7f7f7f7f7f;
const int  INFi  =  0x7f7f7f7f;

ll n, k;

void solve(){
	cin >> n >> k;
	ll ans = 0;
	for(int i = 2; i <= 2 * n; i++){
		ll sum = k + i;
		ll x = i;
		if(sum > 2 * n or sum < 2) continue;
		if(x > n) x -= 2 * (x - (n + 1));
		if(sum > n) sum -= 2 * (sum - (n + 1));
		ans += (x - 1) * (sum - 1);
	}
	deb1(ans)
}

int main(){
	GODSPEED;
	int test = 1;
	//cin >> test;
	for(int i = 1; i <= test; i++){
		solve();
	}
}