#include <bits/stdc++.h> using namespace std; int main() { int n, m,i(0),j(0); string s, t; cin >> n >> m >> s >> t; while(i < n-m && j < m){ if(s[i] == t[j]){ i++; j++; }else { i++; } } if(j != m ) cout << -1 << endl; else cout << i << endl; return 0; }