// #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 <atcoder/all>
#include <bits/stdc++.h>
#define FOR(i, a, n) for(ll i = (ll)a; i < (ll)n; i++)
#define FORR(i, n) for(ll i = (ll)n - 1LL; i >= 0LL; i--)
#define rep(i, n) FOR(i, 0, n)
#define ALL(x) begin(x), end(x)
using namespace std;
using ll = long long;
constexpr ll Mod = 998244353;
constexpr ll mod = 1e9 + 7;
constexpr ll inf = 1LL << 60;
const double PI = acos(-1);
template <typename T1, typename T2> inline bool chmax(T1 &a, T2 b) {
    return a < b && (a = b, true);
}
template <typename T1, typename T2> inline bool chmin(T1 &a, T2 b) {
    return a > b && (a = b, true);
}
/*-------------------------------------------*/

int main() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);

    int a, b;
    cin >> a >> b;
    int y = (a - b) / 2;
    int x = a - y;
    cout << x << " " << y << endl;

    return 0;
}