#include using LL = long long; const int N = 2e5 + 7; int n, a[N], ca, b[N], cb; char s[N], t[N]; int main() { scanf("%d%s%s", &n, s + 1, t + 1); if(s[1] != t[1] || s[n] != t[n]) { puts("-1"); return 0; } for(int i = 2; i <= n; i += 2) { s[i] = 'A' + 'B' - s[i]; t[i] = 'A' + 'B' - t[i]; } for(int i = 2; i <= n; ++i) { if(s[i] != s[i - 1]) a[++ca] = i; if(t[i] != t[i - 1]) b[++cb] = i; } if(ca != cb) { puts("-1"); return 0; } LL ans = 0; for(int i = 1; i <= ca; ++i) ans += std::abs(a[i]- b[i]); printf("%lld\n", ans); return 0; }