#include<bits/stdc++.h>
#define rep(i,n) for(int i = 0; i < (n); ++i)
using namespace std ;
typedef long long ll ;

const int mod=1000000007;
const int INF=1e9 ;
const int N=2e5 ;

 ll AbsDiff(ll a[], int n)
    {
      if ( n < 2 ) return 0;
      sort(a,a+n);     
      ll sum = 0;
      int i;
      for(i=n-1;i>=0;i--)
      {
        sum += ((ll)a[i]*((ll)i) - (ll)a[i]*((ll)n-i-1));
      }
      return sum;
    }


void solve(){

    int n ;
    cin>>n ;
    ll a[n] ;

    rep(i,n){
        cin>>a[i] ;
    }

    cout<<AbsDiff(a,n) ;









}


int main(){
 
    #ifndef ONLINE_JUDGE
        freopen("input.txt","r",stdin) ;
        freopen("output.txt","w",stdout) ;
    #endif 
        
        cin.tie(0);
        cout.tie(0);
        ios_base::sync_with_stdio(false);  
    
        int y ;

        y=1 ;
        //cin>>y ;

        while(y--){
 
            solve() ;

        }

        return 0 ;

}
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <memory>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

typedef long long ll;

int read() {
    register int x = 0, f = 0, ch = getchar();
    for (; !isdigit(ch); f = (ch == 45), ch = getchar()) ;
    for (; isdigit(ch); x = x * 10 + ch - 48, ch = getchar()) ;
    return f ? -x : x;
}

void write(int x) {
    if (x < 0) putchar(45), x = -x;
    static int sta[35];
    int top = 0;
    do {
        sta[top++] = x % 10, x /= 10;
    } while (x);
    while (top) putchar(sta[--top] + 48);
}

const int maxn = 2e5 + 9, maxm = 2e5 + 9, inf = 0x3f3f3f3f;

int n, m;
ll a[maxn], b[maxn], v1[maxn], v2[maxn];

std::vector<int> vec[maxn];

ll c[maxn];

#define lowbit(x) ((x) & (-x))

void add(ll x, ll k) {
    for (; x <= n; x += lowbit(x)) c[x] += k;
}

ll query(ll x) {
    ll ret = 0;
    for (; x; x -= lowbit(x)) ret += c[x];
    return ret;
}

int main() {
    // freopen(".in", "r", stdin);
    // freopen(".out", "w", stdout);
    n = read();
    for (int i = 1; i <= n; ++i) v1[i] = (a[i] = read() + i);
    for (int i = 1; i <= n; ++i) v2[i] = (b[i] = read() + i);
    std::sort(v1 + 1, v1 + n + 1); std::sort(v2 + 1, v2 + n + 1);
    bool flag = 0;
    for (int i = 1; i <= n; ++i) if (v1[i] != v2[i]) {
        flag = 1;
        break;
    }
    if (flag) {
        puts("-1");
        return 0;
    }
    int tot = std::unique(v2 + 1, v2 + n + 1) - v2 - 1;
    for (int i = 1; i <= n; ++i) a[i] = std::lower_bound(v2 + 1, v2 + tot + 1, a[i]) - v2;
    for (int i = 1; i <= n; ++i) b[i] = std::lower_bound(v2 + 1, v2 + tot + 1, b[i]) - v2;
    for (int i = n; i >= 1; --i) vec[b[i]].push_back(i);
    for (int i = 1; i <= n; ++i) {
        int x = a[i];
        a[i] = vec[x].back();
        vec[x].pop_back();
    }
    ll ans = 0;
    for (int i = n; i >= 1; --i) {
        ans += query(a[i] - 1);
        add(a[i], 1);
    }
    printf("%lld\n", ans);
    return 0;
}