#include<bits/stdc++.h>
#define pii pair<int,int>
#define fi first
#define se second
#define ll long long
#define inf (1<<30)
#define INF (1LL<<60)
//#define int long long
//const int mod=998244353;
//const int mod=1e9+7;
const int N=1e5+5,M=5e5+5;
//int head[M],to[M],nxt[M],tot;
using namespace std;

int n,a[N];
bool f[N<<2];
void Clear(){


	return;
}
void work(){
	Clear();
	cin>>n;
	int sum=0;
	for(int i=1;i<=n;i++){
		cin>>a[i];
		sum+=a[i];
//		f[a[i]]=1;
	}
	f[0]=1;
	for(int i=1;i<=n;i++){
		for(int j=sum;j>=a[i];j--){
			f[j]|=f[j-a[i]];
		}
	}
	for(int i=sum/2;i>=0;i--)
		if(f[i]){
			cout<<sum-i<<endl;
			return;
		}
}
signed main(){
	int T=1;
//	scanf("%d",&T);
	while(T--)work();
	return 0;
}














#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define REPR(i, n) for (int i = n - 1; i >= 0; --i)
#define FOR(i, m, n) for (int i = m; i < n; ++i)
#define FORR(i, m, n) for (int i = m; i >= n; --i)
#define ALL(v) (v).begin(),(v).end()
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }
const ll INF=1LL<<60;
const int inf=(1<<30)-1;
const int mod=1e9+7;
int dx[8]={1,0,-1,0,-1,-1,1,1};
int dy[8]={0,1,0,-1,-1,1,-1,1};
int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    int n;cin >> n;
    string s,t;cin >> s >> t;
    vector<int> a(n),b(n);
    REP(i,n){
        a[i]=s[i]-'0';
        b[i]=t[i]-'0';
    }
    int cnt=0;
    REPR(i,n){
        cnt+=a[i]-b[i];
        if(cnt<0){
            cout << -1 << endl;
            return 0;
        }
    }
    if(cnt%2==1){
        cout << -1 << endl;
        return 0;
    }
    ll ans=0;
    stack<int> st;
    queue<int> q;
    REP(i,n){
        if(b[i]==1){
            q.push(i);
        }
        if(a[i]==1){
            if(!st.empty()){
                int p=st.top();
                st.pop();
                ans+=i-p;
            }
            else if(q.empty()){
                st.push(i);
            }
            else{
                int p=q.front();
                q.pop();
                ans+=i-p;
            }
        }
    }
    cout << ans << endl;
}