#include #define rep(i, n) for (int i = 0; i < (n); ++i) using std::cin; using std::cout; using std::vector; using std::string; using ll = long long; int main() { int n; cin >> n; string s, t; cin >> s >> t; rep(i, n) if (i%4 >= 2){ s[i] ^= 'A'^'B'; t[i] ^= 'A'^'B'; } if (s[0] != t[0] or s.back() != t.back()) { puts("-1"); return 0; } vector pos_s, pos_t; rep(i, n) { if (s[i] != s[i+1]) pos_s.push_back(i); if (t[i] != t[i+1]) pos_t.push_back(i); } if (pos_s.size() != pos_t.size()) { puts("-1"); return 0; } ll ans = 0; rep(i, pos_s.size()) { ans += abs(pos_s[i]-pos_t[i]); } cout << ans << '\n'; return 0; }