#pragma GCC optimize("Ofast")
#include<bits/stdc++.h>
using namespace std;
//#include<boost/multiprecision/cpp_int.hpp>
//#include<boost/multiprecision/cpp_dec_float.hpp>
//namespace mp=boost::multiprecision;
//#define mulint mp::cpp_int
//#define mulfloat mp::cpp_dec_float_100
struct __INIT{__INIT(){cin.tie(0);ios::sync_with_stdio(false);cout<<fixed<<setprecision(15);}} __init;
#define max3(a,b,c) max(a,max(b,c))
#define min3(a,b,c) min(a,min(b,c))
//constexpr int MOD=1000000007;
constexpr int MOD=998244353;
#define INF (1<<30)
#define LINF (lint)(1LL<<56)
#define endl "\n"
#define rep(i,n) for(lint (i)=0;(i)<(n);(i)++)
#define reprev(i,n) for(lint (i)=(n-1);(i)>=0;(i)--)
#define Flag(x) (1<<(x))
#define Flagcount(x) __builtin_popcountll(x)
#define pint pair<int,int>
#define pdouble pair<double,double>
#define plint pair<lint,lint>
#define fi first
#define se second
typedef long long lint;
int dx[8]={1,1,0,-1,-1,-1,0,1};
int dy[8]={0,1,1,1,0,-1,-1,-1};
const int MAX_N=2e5+5;
//struct edge{lint to,num;};
//vector<int> bucket[MAX_N/1000];

int main(void){
    int N;
    cin >> N;
    string S,T;
    cin >> S >> T;
    int scnt=0,tcnt=0;
    rep(i,N){
        if(S[i]=='1') scnt++;
        if(T[i]=='1') tcnt++;
    }
    if(scnt<tcnt){
        cout << -1 << endl;
        return 0;
    }
    if((scnt-tcnt)%2){
        cout << -1 << endl;
        return 0;
    }
    lint ans=0;
    lint rem=scnt-tcnt;
    set<int> ones;
    rep(i,N) if(S[i]=='1') ones.insert(i);
    rep(i,N){
        if(S[i]==T[i]){
            if(S[i]=='1') ones.erase(ones.begin());
            continue;
        }
        if(S[i]=='1'){
            if(rem==0){
                cout << -1 << endl;
                return 0;
            }
            auto itr=ones.begin();
            itr++;
            int plc=*itr;
            ans+=plc-i;
            S[plc]='0';
            ones.erase(itr);
            ones.erase(i);
            rem-=2;
            if(rem<0){
                cout << -1 << endl;
                return 0;
            }
        }
        else if(T[i]=='1'){
            auto itr=ones.begin();
            int plc=*itr;
            ans+=plc-i;
            S[plc]='0';
            ones.erase(itr);
        }
    }
    cout << ans << endl;
}
#include "bits/stdc++.h"
#include "ext/pb_ds/assoc_container.hpp"
#include "ext/pb_ds/tree_policy.hpp"
using namespace std;

using namespace __gnu_pbds;
template <typename T> using oset = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
// find_by_order(k) -> iterator of kth element (in sorted fashion : low - high)
// order_of_key(x) -> #numbers strictly less than x

#pragma GCC optimize("Ofast","unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma GCC target("avx2")

#define sync ios_base::sync_with_stdio(0); cin.tie(0);
#define all(x) x.begin(),x.end()
#define unq(a) sort(all(a));a.resize(unique(all(a)) - a.begin())
#define fi first
#define se second
#define endl '\n'
 
using pii = pair<int , int>;
using ll = long long;
using ld = long double;
 
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
//mt19937 rng(0);

int main(){
    #ifndef ONLINE_JUDGE 
        freopen("input.txt", "r", stdin);
        freopen("output.txt", "w", stdout);
    #endif
 
    sync

    int t = 1;
    //cin >> t;

    auto get = [&](vector<ll>& x, vector<ll>& y){
      set<ll> hi(all(x));
      set<ll, greater<ll>> lo(all(x));
      ll mx = 1e18;
      for (const ll& v : y){
        auto nx = hi.lower_bound(v);
        if (nx != hi.end()) mx = min(mx , *nx - v);
        auto fx = lo.lower_bound(v);
        if (fx != lo.end()) mx = min(mx , v - *fx);
      }
      return mx;
    };

    while(t--){
      int n;
      cin >> n;
      n = 2 * n;
      vector<ll> r, g, b;
      for (ll bu, i = 0; i < n; i++){
        char c; 
        cin >> bu >> c;
        if (c == 'R') r.push_back(bu);
        else if (c == 'G') g.push_back(bu);
        else b.push_back(bu);
      }
      if ((r.size() & 1) or (b.size() & 1)){
        vector<ll> x, y, z;
        if (r.size() & 1){
          x = r; 
          if (g.size() & 1){
            y = g; z = b;
          }
          else{
            y = b; z = g;
          }
        }
        else{
          x = g; y = b; z = r;
        }
        assert(x.size() & 1); 
        assert(y.size() & 1);
        ll mx = get(x , y);
        if (z.size()){
          vector<array<ll , 2>> up, dwn;

          set<ll> hi(all(x));
          set<ll, greater<ll>> lo(all(x));
          for (int i = 0; i < z.size(); i++){
            ll nw = 1e18;
            auto nx = hi.lower_bound(z[i]);
            if (nx != hi.end()) nw = min(nw , *nx - z[i]);
            auto fx = lo.lower_bound(z[i]);
            if (fx != lo.end()) nw = min(nw , z[i] - *fx);
            up.push_back({nw , i});
          }

          hi = set<ll>(all(y));
          lo = set<ll, greater<ll>>(all(y));
          for (int i = 0; i < z.size(); i++){
            ll nw = 1e18;
            auto nx = hi.lower_bound(z[i]);
            if (nx != hi.end()) nw = min(nw , *nx - z[i]);
            auto fx = lo.lower_bound(z[i]);
            if (fx != lo.end()) nw = min(nw , z[i] - *fx);
            dwn.push_back({nw , i});
          }
          sort(all(up)); sort(all(dwn));
          for (int i = 0; i < up.size(); i++){
            int ix = -1;
            for (int j = 0; j < dwn.size(); j++){
              if (up[i][1] == dwn[j][1]) continue;
              ix = j;
              break;
            }
            assert(ix != -1);
            mx = min(mx , up[i][0] + dwn[ix][0]);
          }
        }
        cout << mx << endl;
      }
      else{
        cout << 0 << endl;
      }
    }
    cerr << "processor time: " << clock() / (double) CLOCKS_PER_SEC << "s    ";
    return 0;
}