#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll,ll> PP;
//#define MOD 1000000007
#define MOD 998244353
#define INF 2305843009213693951
//#define INF 810114514
#define PI 3.141592653589
#define setdouble setprecision
#define REP(i,n) for(ll i=0;i<(n);++i)
#define OREP(i,n) for(ll i=1;i<=(n);++i)
#define RREP(i,n) for(ll i=(n)-1;i>=0;--i)
#define all1(i) begin(i),end(i)
#define GOODBYE do { cout << "-1" << endl; return 0; } while (false)
#define MM <<" "<<
#define Endl endl
#define debug true
#define debug2 false

class Fenwick_tree{
    /*
    Copyright (c) 2021 0214sh7
    https://github.com/0214sh7/library/
    */
    private:
    std::vector<long long> BIT;
    
    public:
    
    void add(int a,long long w){
        for(int x=a;x<BIT.size();x|=(x+1)){
            BIT[x]+=w;
        }
    }
    
    void init(int n){
        BIT.clear();
        for(int i=0;i<n;i++){
            BIT.push_back(0);
        }
    }
    
    Fenwick_tree(int n){
        init(n);
    }
    
    long long sum(int a){
        long long r=0;
        for(int x=a-1;x>=0;x=(x&(x+1))-1){
            r+=BIT[x];
        }
        return r;
    }
    
    long long inversion(std::vector<long long> V){
        long long r=0;
        init(V.size());
        for(int i=0;i<V.size();i++){
            add(V[i],1);
            r+=i-sum(V[i]);
        }
        return r;
    }
};

int main(void){
    
    ll N;
    vector<ll> A,B;
    
    cin >> N;
    REP(i,N){
        ll a;
        cin >> a;
        a+=i;
        A.push_back(a);
    }
    REP(i,N){
        ll b;
        cin >> b;
        b+=i;
        B.push_back(b);
    }
    
    //REP(i,N){cout << A[i] << " ";}cout << endl;
    //REP(i,N){cout << B[i] << " ";}cout << endl;
    
    
    vector<pair<ll,ll>> Ax,Bx;
    REP(i,N){
        Ax.push_back({A[i],i});
        Bx.push_back({B[i],i});
    }
    sort(Ax.begin(),Ax.end());
    sort(Bx.begin(),Bx.end());
    
    
    vector<ll> C(N);
    
    REP(i,N){
        if(Ax[i].first!=Bx[i].first){
            cout << -1 << endl;
            return 0;
        }
        
        C[Bx[i].second] = Ax[i].second;
        
    }
    
    //REP(i,N){cout << C[i] << " ";}cout << endl;
    
    Fenwick_tree BIT(N);
    ll Ans = BIT.inversion(C);
    cout << Ans << endl;
    
    return 0;
}

#include<bits/stdc++.h>
#define rep(i,a,b) for(int i = a; i < b; i++)
#define rrep(i,a,b) for (int i = a - 1; i >= b; i--)
#define rng(a) a.begin(), a.end()
#define rrng(a) a.rbegin(), a.rend()
#define pb push_back
#define eb emplace_back
#define fi first
#define se second
#define ll long long
#define dbwt(x) std::cout << fixed << setprecision(15) << x << '\n' 
#define Wt(x) cout << x << '\n'
#define re0 return 0
#define Int(...) int __VA_ARGS__; in(__VA_ARGS__)
#define Ll(...) long long __VA_ARGS__; in(__VA_ARGS__)
#define Str(...) string __VA_ARGS__; in(__VA_ARGS__)
#define Vec(t,a,s) vector<t>a(s); in(a)
#define Vec2(t,a,b,s) vector<t>a(s),b(s);rep(i,0,s)in(a[i],b[i])
#define Vec3(t,a,b,c,s) vector<t>a(s),b(s),c(s);rep(i,0,s)in(a[i],b[i],c[i])
#define Vec4(t,a,b,c,d,s) vector<t>a(s),b(s),c(s),d(s);rep(i,0,s) in(a[i],b[i],c[i],d[i])
#define mvec(t,a,i) vector<t>(a,i)
#define mvec2(t,a,b,i) vector<vector<t>>(a,vector<t>(b,i))
void scan(int& x) {std::cin >> x;}
void scan(ll& x) {std::cin >> x;}
void scan(double& x) {std::cin >> x;}
void scan(std::string& x) {std::cin >> x;}
void scan(std::vector<int>& x) {rep (i, 0, x.size()) std::cin >> x[i];}
void scan(std::vector<ll>& x) {rep (i, 0, x.size()) std::cin >> x[i];}
void in(){}
template<class Head, class... Tail>
void in(Head& head, Tail&... tail) {scan(head); in(tail...);}
template<class T, class U>
bool chmax(T& a, U b) {if(a < b){a=b; return true;} return false;}
template<class T, class U>
bool chmin(T& a, U b) {if(a > b){a=b; return true;} return false;}
using namespace std;
template<class T>
using vvec = vector<vector<T>>;
template<class T>
void emitvec(vector<T> a) {rep (i, 0, a.size()) cout << a[i] << " "; cout << endl;}
struct IoSetup {
   	IoSetup() {
     	cin.tie(nullptr);
     	ios::sync_with_stdio(false);
     	cout << fixed << setprecision(15);
   	}
} iosetup;

//----------------------------------------------------------------//

const ll mx = 10000000000;

int main() {
	Int(N);
	Str(T);

	if (N == 1) {
		if (T == "1") Wt(20000000000);
		else Wt(10000000000);
	}
	else if (N == 2) {
		if (T == "00") Wt(0);
		else if (T == "01") Wt(mx - 1);
		else if (T == "10") Wt(mx);
		else if (T == "11") Wt(mx);
	}
	else {
		string sb = T.substr(0, 3);
		if (sb == "110") {
			bool ok = true;
			rep (i, 0, N) {
				if (i % 3 == 0 && T[i] != '1') ok = false;
				if (i % 3 == 1 && T[i] != '1') ok = false;
				if (i % 3 == 2 && T[i] != '0') ok = false;
			}
			if (!ok) Wt(0);
			else {
				int cnt = N / 3;
				if (N % 3 == 0) Wt(mx - cnt + 1);
				else if (N % 3 == 1) Wt(mx - cnt);
				else if (N % 3 == 2) Wt(mx - cnt);
			}
		}
		else if (sb == "101") {
			bool ok = true;
			rep (i, 0, N) {
				if (i % 3 == 0 && T[i] != '1') ok = false;
				if (i % 3 == 1 && T[i] != '0') ok = false;
				if (i % 3 == 2 && T[i] != '1') ok = false;
			}
			if (!ok) Wt(0);
			else {
				int cnt = N / 3;
				if (N % 3 == 0) Wt(mx - cnt);
				else if (N % 3 == 1) Wt(mx - cnt);
				else if (N % 3 == 2) Wt(mx - cnt);
			}
		}
		else if (sb == "011") {
			bool ok = true;
			rep (i, 0, N) {
				if (i % 3 == 0 && T[i] != '0') ok = false;
				if (i % 3 == 1 && T[i] != '1') ok = false;
				if (i % 3 == 2 && T[i] != '1') ok = false;
			}
			if (!ok) Wt(0);
			else {
				int cnt = N / 3;
				if (N % 3 == 0) Wt(mx - cnt);
				else if (N % 3 == 1) Wt(mx - cnt);
				else if (N % 3 == 2) Wt(mx - cnt - 1);
			}
		}
		else Wt(0);
	}
}