#include <bits/stdc++.h>
//#include <ext/pb_ds/assoc_container.hpp>
//using namespace __gnu_pbds;
using namespace std;
typedef long long ll;
typedef long double ld;
//typedef tree<int,null_type,less<int >,rb_tree_tag,tree_order_statistics_node_update>indexed_set;
template<class T1, class T2>
ostream &operator <<(ostream &os, pair<T1,T2>&p);
template <class T>
ostream &operator <<(ostream &os, vector<T>&v);
template <class T>
ostream &operator <<(ostream &os, set<T>&v);
template <class T1, class T2>
ostream &operator <<(ostream &os, map<T1,T2>&v);
#ifdef APURBA
#define HERE cout<<"here - "<<__LINE__<<"\n";
#define debug(args...) { string _s = #args; replace(_s.begin(), _s.end(), ',', ' '); stringstream _ss(_s); istream_iterator<string> _it(_ss); err(_it, args); }
void err(istream_iterator<string> it)
{
    cout<<endl;
}
template<typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args)
{
    cout << *it << " = " << a << " ";
    err(++it, args...);
}
#else
#define HERE
#define debug(args...)
#endif
const int N = 400000 +5;
const ll mod = 1e9 + 7;
typedef pair<int,int> pii;

int mp[N];
vector<int>adj[N];
bool vis[N];

void dfs(int s, int &cnt, int &cnt2)
{
    cnt+= adj[s].size();
    cnt2++;
    vis[s]=1;
    for(int i:adj[s])
    {
        if(!vis[i])
            dfs(i,cnt,cnt2);
    }
}

void TEST_CASES(int cas)
{
    int n;
    cin>>n;
    int now = 1;
    for(int i=0;i<n;i++)
    {
        int x,y;
        cin>>x>>y;
        int u,v;
        if(mp[x]==0)
        {
            mp[x]= now;
            now++;
        }
        if(mp[y]==0)
        {
            mp[y] = now;
            now++;
        }
        adj[mp[x]].push_back(mp[y]);
        adj[mp[y]].push_back(mp[x]);
    }
    int ans = 0;
    for(int i=1;i<now;i++)
    {
        if(vis[i]==0)
        {
            int cnt =0, cnt2 = 0;
            dfs(i,cnt , cnt2);
            assert(cnt%2==0);
            cnt/=2;
            if(cnt==cnt2-1)
            {
                ans+= cnt2-1;
            }
            else{
                ans+= cnt2;
            }
        }
    }
    cout<<ans<<"\n";



}




/*

*/

int32_t main()
{
#ifndef APURBA
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
#endif
    //freopen("input.txt","r",stdin);
    //freopen("out1.txt","w",stdout);
    int t=1,cas=0;
    //cin>>t;
    while(t--)
    {
        TEST_CASES(++cas);
    }
    return 0;
}



template<class T1, class T2>
ostream &operator <<(ostream &os, pair<T1,T2>&p)
{
    os<<"{"<<p.first<<", "<<p.second<<"} ";
    return os;
}
template <class T>
ostream &operator <<(ostream &os, vector<T>&v)
{
    os<<"[ ";
    for(int i=0; i<v.size(); i++)
    {
        os<<v[i]<<" " ;
    }
    os<<" ]";
    return os;
}

template <class T>
ostream &operator <<(ostream &os, set<T>&v)
{
    os<<"[ ";
    for(T i:v)
    {
        os<<i<<" ";
    }
    os<<" ]";
    return os;
}
template <class T1, class T2>
ostream &operator <<(ostream &os, map<T1,T2>&v)
{
    for(auto i:v)
    {
        os<<"Key : "<<i.first<<" , Value : "<<i.second<<endl;
    }
    return os;
}

#include<bits/stdc++.h>
using namespace std;

#include <ext/pb_ds/assoc_container.hpp> 
#include <ext/pb_ds/tree_policy.hpp> 
using namespace __gnu_pbds; 

#define ll long long
#define db double
#define el "\n"
#define ld long double
#define rep(i,n) for(int i=0;i<n;i++)
#define rev(i,n) for(int i=n;i>=0;i--)
#define rep_a(i,a,n) for(int i=a;i<n;i++)
#define all(ds) ds.begin(), ds.end()
#define ff first
#define ss second
#define pb push_back
#define mp make_pair
typedef vector< long long > vi;
typedef pair<long long, long long> ii;
typedef priority_queue <ll> pq;
#define o_set tree<ll, null_type,less<ll>, rb_tree_tag,tree_order_statistics_node_update> 

const ll mod = 1000000007;
const ll INF = (ll)1e18;
const ll MAXN = 200006;

ll po(ll x, ll n){ 
    ll ans=1;
    while(n>0){ if(n&1) ans=(ans*x)%mod; x=(x*x)%mod; n/=2;}
    return ans;
}

int fen[MAXN];

void upd(int pos){
    while(pos<MAXN){
        fen[pos]++;
        pos|=(pos+1);
    }
}

ll qr(int pos){
    ll ret = 0;
    while(pos>=0){
        ret+=fen[pos];
        pos&=(pos+1);
        pos--;
    }
    return ret;
}

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);

    int T=1;
    //cin >> T;
    while(T--){
    
        int n;
        cin>>n;

        ll a[n], b[n];
        rep(i,n) cin>>a[i];
        rep(i,n) cin>>b[i];

        rep(i,n){
            a[i]+=i;
            b[i]+=i;
        }

        map<ll, vector<int> > m;

        rev(i,n-1){
            m[a[i]].pb(i);
        }


        int f=0;
        int nap=0;
        rep(i,n){
            if(m.find(b[i])==m.end()){
                f=1;
                break;
            }

            a[m[b[i]].back()]=nap++;
            m[b[i]].pop_back();

            if(m[b[i]].empty()) m.erase(b[i]);
        }


        if(f){
            cout<<-1<<el;
            return 0;
        }

        ll ans = 0;
        rep(i,n){
            ans += qr(MAXN)-qr(a[i]);
            upd(a[i]);
        }

        cout<<ans<<el;


    }
    cerr << "Time : " << 1000 * ((double)clock()) / (double)CLOCKS_PER_SEC << "ms\n";
    return 0;
}