#include<bits/stdc++.h>
using namespace std;
#define int long long
signed main()
{
	int num;
	scanf("%lld",&num);
	string str1,str2;
	cin>>str1>>str2;
	int cur=0,ans=0;
	vector<int>v;
	for(int i=0;i<num;i++)
	{
		if(str1[i]=='1')
		v.push_back(i);
	}
	for(int i=0;i<num;i++)
	{
		if(str1[i]==str2[i]) continue;
		while(cur<v.size()&&v[cur]<=i) cur++;
		if(cur>=v.size())
		{
			printf("-1");
			return 0;	
		}
		ans+=v[cur]-i;
		str1[v[cur]]='0';
		cur++;	
	}
	cout<<ans<<endl;
}
#include <bits/stdc++.h>
using namespace std;
#define i64 int64_t
#define u64 uint64_t
const i64 inf = 1e17;
const int dx[] = {0, 1, 0, -1, 1, 1, -1, -1};
const int dy[] = {1, 0, -1, 0, 1, -1, 1, -1};
#define endl "\n"
#define all(a) a.begin(),a.end()
#define overload(_1,_2,_3,_4,name,...) name
#define _rep1(n) for(int i = 0; i < (n); i++)
#define _rep2(i,n) for(int i = 0; i < (n); i++)
#define _rep3(i,a,b) for(int i = (a); i < (b); i++)
#define _rep4(i,a,b,c) for(int i = (a); i < (b); i += (c))
#define rep(...) overload(__VA_ARGS__,_rep4,_rep3,_rep2,_rep1)(__VA_ARGS__)
#define _rrep1(n) for(int i = (n) - 1; i >= 0; i--)
#define _rrep2(i,n) for(int i = (n) - 1; i >= 0; i--)
#define _rrep3(i,a,b) for(int i = (b) - 1; i >= (a); i--)
#define _rrep4(i,a,b,c) for(int i = (b) - 1; i >= (a); i -= (c))
#define rrep(...) overload(__VA_ARGS__,_rrep4,_rrep3,_rrep2,_rrep1)(__VA_ARGS__)
#define ForEach(a,b) for_each(a.begin(),a.end(),b)
#define NextAfter(x) x = nextafter(x, INFINITY)
template <class T> bool chmin(T& a, T b){ if(a > b){ a = b; return 1; } return 0; }
template <class T> bool chmax(T& a, T b){ if(a < b){ a = b; return 1; } return 0; }
struct Edge { int to; i64 cost; Edge(int to, i64 cost) : to(to), cost(cost) {} };
using Graph = vector<vector<Edge>>;

bool check(string &x, i64 n, i64 m) {
  i64 val = 0;
  rep(x.size()) {
    if (val > (m-(x.at(i)-'0')+n-1)/n) {
      val = m+1;
      break;
    }
    val = val * n + (x.at(i) - '0');
  }
  return val <= m;
}

void Main() {
  string x;
  i64 m;
  cin >> x >> m;
  if (x.size() == 1) {
		if (stoi(x) <= m)
			cout << 1 << endl;
		else
			cout << 0 << endl;
		return;
	}
  i64 lb_init = *max_element(all(x)) - '0';
  i64 lb = lb_init, ub = m+1;
  while (ub - lb > 1) {
    i64 mid = (lb + ub)/2;
    if (check(x, mid, m))
      lb = mid;
    else
      ub = mid;
  }
  cout << lb - lb_init << endl;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << setprecision(10) << fixed;
  Main();
}