#include <bits/stdc++.h>
// #include <atcoder/all>
#define rep(i,n) for(int i = 0; i < (n); ++i)
#define srep(i,s,t) for(int i = s; i < t; ++i)
#define drep(i,n) for(int i = (n)-1; i >= 0; --i)
using namespace std;
// using namespace atcoder;
typedef long long int ll;
typedef pair<int,int> P;
#define yn {puts("Yes");}else{puts("No");}
const int MAX_N = 300005;

class Tentousuu{
public:
    Tentousuu() = delete;
    Tentousuu(vector<int> a_){
        a = a_; b = a_;
        sort(b.begin(), b.end());
        n = a.size();
        nn = 10;
        while(nn <= n + 3) nn *= 2; 
        G.resize(nn);
        now.resize(nn);
        bit.resize(nn);
    }
    Tentousuu(vector<int> a_, vector<int> b_){
        a = a_; b = b_;
        n = a.size();
        nn = 10;
        while(nn <= n + 3) nn *= 2; 
        G.resize(nn);
        now.resize(nn);
        bit.resize(nn);
    }

    ll Calc(){
        vector<int> c(n*2);
        rep(i,n){
            c[i] = a[i];
            c[i+n] = b[i];
        }

        vector<int> z = zaatsu(c);
        rep(i,n){
            a[i] = z[i];
            b[i] = z[i+n];
        }
        rep(i,n){
            G[a[i]].push_back(i+1);
        }

        ll ans = 0;
        
        rep(i,n){
            int x = b[i];
            int ite = G[x][now[x]];
            now[x]++;
            int real = ite + sums(ite);
            ans += real - (i+1);
            adds(1,1);
            adds(ite,-1);
        }

        return ans;
    }

private:
    int n;
    vector<int> a, b, c;
    vector<vector<int>> G;
    vector<int> now;
    vector<ll> bit;
    int nn;

    // 座標圧縮 a[n] -> z[n](zは1-index)
    vector<int> zaatsu(vector<int> a){
        int n = a.size();
        vector<int> b, pre_b, z;
        rep(i,n)pre_b.push_back(a[i]);
        sort(pre_b.begin(), pre_b.end());
        rep(i,n){
            if(i == 0){
                b.push_back(pre_b[i]);
            }else{
                if(pre_b[i] != pre_b[i-1]){
                    b.push_back(pre_b[i]);
                }
            }
        }
        rep(i,n){
            int tmp_z = lower_bound(b.begin(), b.end(), a[i]) - b.begin() + 1;
            z.push_back(tmp_z);
        }
        
        return z;
    }

    
    void adds(int a, ll w){
        for(int x = a; x <= nn; x += x & -x) bit[x] += w;
    }
    ll sums(int a){ // bit[1,a]の和を返す.
        ll ret = 0;
        for(int x = a; x > 0; x -= x & -x) ret += bit[x];
        return ret;
    }
};
    

int main() {
    int n; cin >> n;
    vector<int> a(n);
    rep(i,n) cin >> a[i];
    vector<int> b(n);
    rep(i,n) cin >> b[i];

    rep(i,n){
        a[i] -= n - 1 - i;
        b[i] -= n - 1 - i;
    }
    int aa[n] = {};
    int bb[n] = {};
    rep(i,n){
        aa[i] = a[i];
        bb[i] = b[i];
    }
    sort(aa,aa+n);
    sort(bb,bb+n);

    rep(i,n){
        if(aa[i] != bb[i]){
            cout << -1 << endl;
            return 0;
        }
    }

    Tentousuu t(a, b);
    ll ans = t.Calc();
    
    cout << ans << endl;
    return 0;
}




// My favourite LGM is Jiangly :>)
#pragma GCC optimize("O3","unroll-loops","omit-frame-pointer","inline") //Optimization flags
#pragma GCC option("arch=native","tune=native","no-zeroupper") //Enable AVX
#pragma GCC target("avx","popcnt")  //Enable AVX
#include <x86intrin.h> //SSE Extensions
#include <bits/stdc++.h> 
using namespace std;
#define eb emplace_back
#define mp make_pair
#define hello cout<<"hello"<<"\n"
#define forr(i,a,b) for(int i=a;i<b;i++)
#define it(s)	for(auto itr:s)
#define dvg(s) 	for(auto itr:s)	cout<<itr<<" ";cout<<endl;
#define dbg(s)	cout<<#s<<"= "<<s<<endl;
typedef long long int lli;
typedef unsigned long long int ulli;
const lli INF=(lli)1e17+5;
const ulli MOD=1e9+7;

int main()
{
    ios_base::sync_with_stdio(false);cin.tie(NULL);
	int n;cin>>n;vector<int> v(n,0);bitset<(int)1e5+5> vis;vis.reset();vis.set(0);
	int s=0;
	for(int i=0;i<n;i++)
	{
		cin>>v[i];s+=v[i];
		for(int j=(int)1e5+1;j>=v[i];j--)
		{
			if(vis[j-v[i]]) vis.set(j);
		}
	}
	int ans=0;
	int start=0;if(s&1) start=s/2+1;else start=s/2;
	for(int i=start;i<=s;i++)
	{
		if(vis[i])	{ans=i;break;}
	}
	cout<<ans<<"\n";




		


    
		
	
    return 0;
}




















