#include <bits/stdc++.h>
using namespace std;
#define int long long
#define endl '\n'
const int N = 1e5 + 5;
int n; string s, t;
struct Node {
int p, x; string s;
};
queue<Node> q;
map<string, int> m;
signed main() {
cin>>n>>s>>t;
s += "**", t += "**";
q.push({0, n, s});
while(!q.empty()) {
auto f = q.front(); q.pop();
if(f.s == t) cout<<f.p<<endl, exit(0);
for(int i = 0; i < n + 1; i++) if(i != f.x && i + 1 != f.x) {
string d = f.s;
swap(d[i], d[f.x]), swap(d[i + 1], d[f.x + 1]);
if(m[d]) continue; m[d] = 1;
q.push({f.p + 1, i, d});
}
}
cout<<-1<<endl;
}