#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for(int i = 0; i < n; ++i)
#define ll long long
template <typename T>
bool PN(T x){ if (x <= 1) return false; if (x == 2) return true; for (int i = 2; i < sqrt(x) + 1; i++) if (x % i == 0) return false; return true;}
const ll MOD = 1e9+7;
const ll LINF = 1001002003004005006ll;
const int INF = 1001001001;
long long Comb(int n, int i){long long ans = 1; if(i>n || i < 0) return 0; if(i == 0 || i == n) return 1; else {for(int j = 1; j <= i; ++j){ans *=(n+1-j); ans /= j; ans %= MOD;} }return ans;}
template<typename T> T gcd(T a, T b){if(b == 0) return a;else return gcd(b, a%b);}
template<typename T> T lcm(T a, T b){if(b > a) swap(a, b); T g = gcd(a, b);return a / g * b;}

void solve(){
  int n; cin >> n;
  int a[n];
  int sum = 0;
  for(int i = 0; i < n; ++i) {
    cin >> a[i];
    sum += a[i];
  }
  int dp[n + 1][sum + 1] = {};
  for(int i = 1; i <= n; ++i) {
    for(int j = 0; j <= sum; j++) {
      if(j-a[i-1] >= 0) dp[i][j] = max(dp[i-1][j-a[i-1]] + a[i-1], dp[i][j]);
      dp[i][j] = max(dp[i-1][j], dp[i][j]);
    }
  }
  //cout << dp[n][sum/2] << endl;
  cout << sum-dp[n][sum/2] << endl;
}

int main(void){
  solve();
  return 0;
}

#include <bits/stdc++.h>
using namespace std;
//const long nPrime = 1000000007;
//const long nPrime = 998244353;

class BIT{
    public:
    BIT(int n){
        m_n = 1;
        while(m_n < n){
            m_n *= 2;
        }
        m_vi.resize(m_n, 0);
    }
    ~BIT(){
    }
  
    void Add(const int i, long x){
        if(i >= m_n || i < 0){
            return;
        }
        m_vi[i] += x;  //FIXME
        int iTmp = i;
        while(iTmp != 0){
            iTmp -= (iTmp & (-iTmp));
            m_vi[iTmp] += x;  //FIXME  
        }
    }
  
    long GetNum(const int i){
        if(i >= m_n || i < 0){
            return -1;  //error
        }
        long nBase = m_vi[i];
        int iFin = i & (-i);
        int iTmp = 1;
        long nSum = 0;
        while(iTmp < m_n){
            if(iTmp == iFin){
                break;
            }
            nSum += m_vi[i+iTmp];  //FIXME
            iTmp *= 2;
        }
        return (nBase - nSum);
    }
    
    //[i,j)のsum
    //i>jの処理を考えること
    long GetSum(int i, int j){
        if(i > m_n || j > m_n){
            return -1;  //error
        }
        if(i != 0){
            return (GetSum(0, j) - GetSum(0, i));
        }        
        int iTmp = j;
        long nSum = 0;
        while(iTmp < m_n){
            nSum += m_vi[iTmp];  //FIXME
            if(iTmp == 0){
                break;
            }
            iTmp += (iTmp & (-iTmp));
        }
        return m_vi[0] - nSum;
    }
    private:
    int m_n;
    vector<long> m_vi;
};

//正数列の反転数  値が大きいときor負の値を含むときは座圧とかしておくこと
long GetInversion(const vector<int> &viNum){
    int n = viNum.size();
    int nMax = 0;
    for(int i = 0; i < n; i++){
        nMax = max(viNum[i], nMax);
    }
    BIT zBit(nMax+1);
    long nReturn = 0;
    for(int i = n-1; i >= 0; i--){
        nReturn += zBit.GetSum(0,viNum[i]);
        zBit.Add(viNum[i], 1);
    }
    return nReturn;
}

int main() {
    long n;
    cin >> n;
    map<long, vector<long>> miA, miB;
    for(long i = 0; i < n; i++){
        long a;
        cin >> a;
        auto itr = miA.find(a+i);
        if(itr == miA.end()){
            vector<long> viTmp(1,i);
            miA.insert(make_pair(a+i, viTmp));
        }
        else {
            itr->second.push_back(i);
        }
    }
    for(long i = 0; i < n; i++){
        long a;
        cin >> a;
        auto itr = miB.find(a+i);
        if(itr == miB.end()){
            vector<long> viTmp(1,i);
            miB.insert(make_pair(a+i, viTmp));
        }
        else {
            itr->second.push_back(i);
        }
    }
  
    vector<int> viNum(n);
    for(auto itrA = miA.begin(); itrA != miA.end(); itrA++){
        auto itrB = miB.find(itrA->first);
        if(itrB == miB.end()){
            cout << -1 << endl;
            return 0;
        }
        if(itrA->second.size() != itrB->second.size()){
            cout << -1 << endl;
            return 0;
        }
        for(int i = 0; i < itrA->second.size(); i++){
            viNum[itrA->second[i]] = itrB->second[i];
        }
    }
  
    cout << GetInversion(viNum) << endl;
    return 0;
}