#include <algorithm>
#include <iostream>
#include <numeric>
#include <string>
#include <tuple>
#include <utility>
#include <vector>

#define rep(i, a, b) for (int i = int(a); i < int(b); i++)
using namespace std;
using ll = long long int;
using P = pair<ll, ll>;

// clang-format off
#ifdef _DEBUG_
#define dump(...) do{ cerr << __LINE__ << ":\t" << #__VA_ARGS__ << " = "; debug_print(__VA_ARGS__); } while(false)
template<typename T, typename... Ts> void debug_print(const T &t, const Ts &...ts) { cerr << t; ((cerr << ", " << ts), ...); cerr << endl; }
#else
#define dump(...) do{ } while(false)
#endif
template<typename T> vector<T> make_v(size_t a, T b) { return vector<T>(a, b); }
template<typename... Ts> auto make_v(size_t a, Ts... ts) { return vector<decltype(make_v(ts...))>(a, make_v(ts...)); }
template<typename T> bool chmin(T &a, const T& b) { if (a > b) {a = b; return true; } return false; }
template<typename T> bool chmax(T &a, const T& b) { if (a < b) {a = b; return true; } return false; }
template<typename T, typename... Ts> void print(const T& t, const Ts&... ts) { cout << t; ((cout << ' ' << ts), ...); cout << '\n'; }
template<typename... Ts> void input(Ts&... ts) { (cin >> ... >> ts); }
template<typename T> istream &operator,(istream &in, T &t) { return in >> t; }
// clang-format on

int main() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    int n;
    string s, t;
    input(n, s, t);
    if (count(s.begin(), s.end(), '1') != count(t.begin(), t.end(), '1')) {
        print(-1);
        return 0;
    }
    int ans = 0;
    int a = 0, b = 0;
    rep(i, 0, n) {
        if (s[i] == t[i]) continue;
        a = max(i, a);
        b = max(i, b);
        while (a < n && s[a] == '1') a++;
        while (b < n && t[b] == '1') b++;
        if (s[i] == '1') {
            swap(s[i], s[a]);
            ans++;
        }
        if (t[i] == '1') {
            swap(t[i], t[b]);
            ans++;
        }
    }
    print(ans);

    return 0;
}

#include <bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define all(v) (v).begin(), (v).end()
#define prec(n) fixed<<setprecision(n)
using namespace std;
typedef long long ll;
typedef unsigned long long int ull ;

const int MOD=1e9+7;
const char nl = '\n';

int check (int n ) {
	int k1 = n ;
  	int c = 0 ;
  	while( k1 != 0 ){
    	int tmp = k1 % 10 ;
      	if ( tmp == 7 ) 
          	c = 1 ;
     	k1 /= 10 ;
    }
  	int k2 = n ;
  	int c1 = 0 ;
  	while( k2 != 0 ){
    	int tmp = k2 % 8 ;
      	if ( tmp == 7 ) 
          	c1 = 1 ;
     	k2 /= 8 ;
    }
  	if ( c1 == 1 || c == 1 )
      	return 0 ;
  	else
      	return 1 ;
  	
}

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
   	int n  ;
  	cin >> n ;
  	int ans = 0 ;
  	for (int i = 1 ; i <= n ; i++)
      	if ( check(i) == 1 )
          	ans++;
  	cout << ans; 
  	
    return 0;
}