//#pragma GCC optimize ("Ofast")
//#pragma GCC optimize ("unroll-loops")
//#pragma GCC target("avx,avx2,fma")

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/rope>
#define pb push_back
#define F first
#define S second
#define ins insert
#define mp make_pair
#define fo(i, n1, n, x) for(int i = n1; i <= n; i += x)
#define foo(i, n, n1, x) for(int i = n; i >= n1; i -= x)
#define bit __builtin_popcount
#define md (l + ((r - l) / 2))
#define all(x) x.begin(),x.end()
#define eb emplace_back
#define ub upper_bound
#define lb lower_bound
#define ios ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define file(s) if (fopen(s".in", "r")) freopen(s".in", "r", stdin), freopen(s".out", "w", stdout)

using namespace std;
//using namespace __gnu_pbds;
//using namespace __gnu_cxx;


using ll = long long;

//#define int ll

using pii = pair<int,int>;
using pll = pair<ll,ll>;
using vi = vector<int>;



const int N = 2e5 + 11, mod = 1e9 + 7, mod2 = 998244353;
const int MAX = 1e5 + 11;
const int INF1 = 2e9 + 11;
const ll INF2 = 6e18 + 11;
const double INF3 = 1e8 + 11;
const int base = 500;
const int P = 31;
const int dx[] = {1, -1, 0, 0, 1, 1, -1, -1};
const int dy[] = {0, 0, 1, -1, 1, -1, 1, -1};
const double EPS = 1e-4;
const double PI = acos(-1.0);


//template<typename T> using ordered_set = tree <T, null_type, less <T>, rb_tree_tag, tree_order_statistics_node_update>;
template<typename T1, typename T2> inline void chmin(T1 &a, T2 b) { if (a > b) a = b; }
template<typename T1, typename T2> inline void chmax(T1 &a, T2 b) { if (a < b) a = b; }


mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int rnd(int l, int r) { return uniform_int_distribution<int> (l, r)(rng); }

int r, c, a[507][507], b[507][507], d[507][507][2];
main() {
  file("paint");
  ios;
  cin >> r >> c;
  fo(i, 1, r, 1) fo(j, 1, c - 1, 1) cin >> a[i][j];
  fo(i, 1, r - 1, 1) fo(j, 1, c, 1) cin >> b[i][j];
  fo(i, 1, r, 1) fo(j, 1, c, 1) d[i][j][0] = d[i][j][1] = INF1;
  d[1][1][0] = 0;
  priority_queue < pair <pii, pii> > q;
  q.push(mp(mp(0, 0), mp(1, 1)));
  function<bool(int, int)> valid = [&](int x, int y) { return (1 <= x and x <= r and 1 <= y and y <= c); };
  while (!q.empty()) {
    pair <pii, pii> v = q.top();
    q.pop();
    int nx, ny;
    int t = v.F.S;
    if (-v.F.F != d[v.S.F][v.S.S][t]) continue;
    nx = v.S.F;
    ny = v.S.S + 1;
    if (valid(nx, ny) and d[nx][ny][0] > d[v.S.F][v.S.S][t] + a[v.S.F][v.S.S]) {
      d[nx][ny][0] = d[v.S.F][v.S.S][t] + a[v.S.F][v.S.S];
      q.push(mp(mp(-d[nx][ny][0], 0), mp(nx, ny)));
    }
    nx = v.S.F;
    ny = v.S.S - 1;
    if (valid(nx, ny) and d[nx][ny][0] > d[v.S.F][v.S.S][t] + a[v.S.F][v.S.S - 1]) {
      d[nx][ny][0] = d[v.S.F][v.S.S][t] + a[v.S.F][v.S.S - 1];
      q.push(mp(mp(-d[nx][ny][0], 0), mp(nx, ny)));
    }
    nx = v.S.F + 1;
    ny = v.S.S;
    if (valid(nx, ny) and d[nx][ny][0] > d[v.S.F][v.S.S][t] + b[v.S.F][v.S.S]) {
      d[nx][ny][0] = d[v.S.F][v.S.S][t] + b[v.S.F][v.S.S];
      q.push(mp(mp(-d[nx][ny][0], 0), mp(nx, ny)));
    }
    if (t) {
      nx = v.S.F - 1;
      ny = v.S.S;
      if (valid(nx, ny) and d[nx][ny][t] > d[v.S.F][v.S.S][t] + 1) {
        d[nx][ny][t] = d[v.S.F][v.S.S][t] + 1;
        q.push(mp(mp(-d[nx][ny][t], t), mp(nx, ny)));
      }
    } else {
      nx = v.S.F;
      ny = v.S.S;
      t ^= 1;
      if (valid(nx, ny) and d[nx][ny][t] > d[v.S.F][v.S.S][1-t] + 1) {
        d[nx][ny][t] = d[v.S.F][v.S.S][1-t] + 1;
        q.push(mp(mp(-d[nx][ny][t], t), mp(nx, ny)));
      }
    }
  }
  cout << min(d[r][c][0], d[r][c][1]);
  return 0;
}
/**

*/

#include <bits/stdc++.h>

#define eprintf(args...) fprintf(stderr, args)
#define rep(i, n) for (int i = 0; i < (int)(n); ++ i)

const int mxn = 2e5 + 5;
int n, a[mxn], b[mxn];
std::map <int, std::vector <int> > oa, ob;

int s[mxn];

void update(int x, int v) {
	for (++ x; x < mxn; x += x & -x) s[x] += v;
}

int query(int x) {
	int ans = 0;
	for (++ x; x; x -= x & -x) ans += s[x];
	return ans;
}

int frm[mxn];

int main() {
	scanf("%d", &n);
	rep(i, n) scanf("%d", &a[i]), a[i] += i;
	rep(i, n) scanf("%d", &b[i]), b[i] += i;
	rep(i, n) oa[a[i]].push_back(i);
	rep(i, n) ob[b[i]].push_back(i);
	for (auto x : oa) {
		std::vector <int> va = x.second, vb = ob[x.first];
		if (va.size() != vb.size()) return printf("%d\n", -1), 0;
		rep(i, va.size()) frm[vb[i]] = va[i];
	}
	rep(i, n) update(i, 1);
	long long ans = 0;
	rep(i, n) ans += query(frm[i] - 1), update(frm[i], -1);
	printf("%lld\n", ans);
	return 0;
}
