#include <iostream>
#include <vector>
#include <queue>
#include <unordered_map>
#include <climits>
using namespace std;
int find_min_time(string S, string T) {
int n = S.length(), m = T.length();
unordered_map<char, vector<int>> char_pos;
for (int i = 0; i < n; i++) {
char_pos[S[i]].push_back(i);
}
for (char c : T) {
if (char_pos.find(c) == char_pos.end()) {
return -1;
}
}
vector<vector<int>> dp(n, vector<int>(m, INT_MAX));
queue<tuple<int, int, int>> q;
for (int start_pos : char_pos[T[0]]) {
q.push({start_pos, 0, 1});
dp[start_pos][0] = 1;
}
while (!q.empty()) {
auto [s_pos, t_pos, time] = q.front();
q.pop();
if (t_pos == m - 1) {
return time;
}
char next_char = T[t_pos + 1];
if (s_pos - 1 >= 0 && dp[s_pos - 1][t_pos] > time + 1) {
dp[s_pos - 1][t_pos] = time + 1;
q.push({s_pos - 1, t_pos, time + 1});
}
if (s_pos + 1 < n && dp[s_pos + 1][t_pos] > time + 1) {
dp[s_pos + 1][t_pos] = time + 1;
q.push({s_pos + 1, t_pos, time + 1});
}
for (int pos : char_pos[next_char]) {
if (pos != s_pos && dp[pos][t_pos + 1] > time + abs(pos - s_pos)) {
dp[pos][t_pos + 1] = time + abs(pos - s_pos);
q.push({pos, t_pos + 1, dp[pos][t_pos + 1]});
}
}
}
return -1;
}
int main() {
int n, m;
string S, T;
cin >> n >> m;
cin >> S;
cin >> T;
cout << find_min_time(S, T) << endl;
return 0;
}