#include <algorithm>
// #include <atcoder/all>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <tuple>
#include <unordered_map>
#include <vector>
/*
cd $dir && g++ -std=c++17 -Wall -Wextra -O2 -DATCODERDEBUG
-I/mnt/d/MyProjects/atcoder/lib/ac-library $fileName && echo 'compilation ok!'
&&  ./a.out
*/
using namespace std;
#define REP(i, n) for (ll i = 0; i < ll(n); i++)
#define FOR(i, a, b) for (ll i = a; i <= ll(b); i++)
#define ALL(x) x.begin(), x.end()
#define dame(a)        \
  {                    \
    cout << a << endl; \
    return 0;          \
  }
typedef long long ll;

class dstream : public ostream {};

template <typename T>
dstream &operator<<(dstream &os, T v) {
#ifdef ATCODERDEBUG
  cout << "\033[1;31m" << v << "\033[0m";
#endif
  return os;
}

ostream &endd(std::ostream &out) {
#ifdef ATCODERDEBUG
  out << std::endl;
#endif
  return out;
}
dstream dout;

template <typename T>
ostream &operator<<(ostream &os, const vector<T> &v) {
  os << "[ ";
  for (auto const &x : v) {
    os << x;
    if (&x != &v.back()) {
      os << " : ";
    }
  }
  os << " ]" << endl;
  return os;
}

template <typename T>
ostream &operator<<(ostream &os, const vector<vector<T>> &v) {
  os << "[" << endl;
  for (auto const &x : v) {
    os << " ";
    os << x;
  }
  os << "]" << endl;
  return os;
}

template <typename T, typename U>
ostream &operator<<(ostream &os, const pair<T, U> &v) {
  os << "[ " << v.first << " " << v.second << " ]" << endl;
  return os;
}

template <typename T, typename U, typename Comp = less<>>
bool chmax(T &xmax, const U &x, Comp comp = {}) {
  if (comp(xmax, x)) {
    xmax = x;
    return true;
  }
  return false;
}

template <typename T, typename U, typename Comp = less<>>
bool chmin(T &xmin, const U &x, Comp comp = {}) {
  if (comp(x, xmin)) {
    xmin = x;
    return true;
  }
  return false;
}

ll op(ll a, ll b) { return min(a, b); }

ll e() { return 1e17; }

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

  ll N, x, c;
  cin >> N;
  vector<vector<ll>> c2xs(N, vector<ll>());
  REP(i, N) {
    cin >> x;
    cin >> c;
    c2xs[c - 1].emplace_back(x);
  }

  REP(i, N) { sort(c2xs[i].begin(), c2xs[i].end()); }

  ll left = 0;
  ll right = 0;
  ll left_v = 0;
  ll right_v = 0;

  REP(c, N) {
    ll siz = c2xs[c].size();
    if (siz == 0) continue;

    ll minX = c2xs[c][0];
    ll maxX = c2xs[c][siz - 1];

    ll n_right_v = min(left_v + abs(left - minX), right_v + abs(right - minX)) +
                   (maxX - minX);
    ll n_left_v = min(right_v + abs(right - maxX), left_v + abs(left - maxX)) +
                  (maxX - minX);

    right_v = n_right_v;
    left_v = n_left_v;
    right = maxX;
    left = minX;

    dout << left << " " << left_v << " " << right << " " << right_v << endd;
  }

  ll ans = 1e17;
  chmin(ans, left_v + abs(left));
  chmin(ans, right_v + abs(right));

  cout << ans << endl;
  // cout << std::fixed << std::setprecision(15) << sqrt(ans) << endl;
}
#include <bits/stdc++.h>
#define rep(i,n) for(int i=0; i<n; i++)
#define reps(i,s,n) for(int i=s; i<n; i++)
#define per(i,n) for(int i=n-1; i>=0; i--)
#define pers(i,n,s) for(int i=n-1; i>=s; i--)
#define all(v) v.begin(),v.end()
#define fi first
#define se second
#define pb push_back
#define si(v) int(v.size())
#define lb(v,n) lower_bound(all(v),n)
#define lbi(v,n) lower_bound(all(v),n) - v.begin()
#define ub(v,n) upper_bound(all(v),n)
#define ubi(v,n) upper_bound(all(v),n) - v.begin()
 
#define mod 1000000007
#define infi 1010000000
#define infl 1100000000000000000
 
#define outve(v) for(auto i : v) cout << i << " ";cout << endl
#define outmat(v) for(auto i : v){for(auto j : i) cout << j << " ";cout << endl;}
#define in(n,v) for(int i=0; i<(n); i++){cin >> v[i];}
#define IN(n,m,v) rep(i,n) rep(j,m){cin >> v[i][j];}
#define cyes cout << "Yes" << endl
#define cno cout << "No" << endl
#define cYES cout << "YES" << endl
#define cNO cout << "NO" << endl
#define csp << " " <<
#define outset(n) cout << fixed << setprecision(n);
 
using namespace std;
using ll = long long;
using ull = unsigned long long;
using ld = long double;
using vi = vector<int>;
using vvi = vector<vector<int>>;
using vd = vector<double>;
using vvd = vector<vector<double>>;
using vl = vector<ll>;
using vvl = vector<vector<ll>>;
using vs = vector<string>;
using pii = pair<int,int>;
using pll = pair<ll,ll>;
template<typename T> using ve = vector<T>;
template<typename T> using pq2 = priority_queue<T>;
template<typename T> using pq1 = priority_queue<T,vector<T>,greater<T>>;
template<typename T> bool chmax(T &a, T b) {if(a < b) {a = b;return 1;}return 0;}
template<typename T> bool chmin(T &a, T b) {if(a > b) {a = b;return 1;}return 0;}



int compress(vi& x1, vi& x2){
    
    int n = si(x1);
    vi a;
    rep(i,n){
        a.pb(x1[i]);
        a.pb(x2[i]);
    }
    sort(all(a));
    a.erase(unique(all(a)),a.end());
    
    rep(i,n){
        x1[i] = lbi(a,x1[i]);
        x2[i] = lbi(a,x2[i]);
    }
    
    return si(a);
}



template <typename T>
class BIT {
    ve<T> node;
    int n;
    
    T find(int m){
        T s = 0;
        while (m > 0) {
            s += node[m];
            m -= (m & (-m));
        }
        return s;
    }
    
public:
    BIT(int _n) : n(_n){
        node.assign(n+1,0);
    }
    
    T sum(int a, int b){
        T s_a = find(a);
        T s_b = find(b+1);
        return s_b - s_a;
    }
    
    
    void update(int m, T x){
        m++;
        while (m <= n) {
            node[m] += x;
            m += (m & (-m));
        }
    }
    
};





void solve(){
    
    int N;
    cin >> N;
    vi A(N),B(N);
    in(N,A);
    in(N,B);
    ll sum = 0;
    rep(i,N) sum += A[i]-B[i];
    if(sum != 0){
        cout << -1 << endl;
        return;
    }
    
    
    rep(i,N) A[i] += i, B[i] += i;
    int m = compress(A,B);
    
    ve<set<int>> C(m+1);
    rep(i,N) C[A[i]].insert(i);
    
    BIT<ll> bi(N+10);
    
    ll ans = 0;
    rep(i,N){
        if(si(C[B[i]]) == 0){
            cout << -1 << endl;
            return;
        }
        
        int a = *C[B[i]].begin();
        C[B[i]].erase(a);
        //cout << a csp i csp bi.sum(0,a+1) << endl;
        ans += (ll)abs(a+bi.sum(0,a+1)-i);
        bi.update(0,1);
        bi.update(a+1,-1);
    }
    
    cout << ans << endl;
    
    
}



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

    solve();

    return 0;
}
