#include using namespace std; #define rep(i,n) for(ll i=0;i=0;i--) #define perl(i,r,l) for(ll i=r-1;i>=l;i--) #define fi first #define se second #define pb push_back #define ins insert #define pqueue(x) priority_queue,greater> #define all(x) (x).begin(),(x).end() #define CST(x) cout<; using vvl=vector>; using pl=pair; using vpl=vector; using vvpl=vector; const ll MOD=1000000007; const ll MOD9=998244353; const int inf=2e9+1; const ll INF=4e18; const ll dy[8]={-1,0,1,0,1,1,-1,-1}; const ll dx[8]={0,-1,0,1,1,-1,1,-1}; template inline bool chmax(T &a, T b) { return ((a < b) ? (a = b, true) : (false)); } template inline bool chmin(T &a, T b) { return ((a > b) ? (a = b, true) : (false)); } vector< int > manacher(const string &s) { vector< int > radius(s.size()); int i = 0, j = 0; while(i < s.size()) { while(i - j >= 0 && i + j < s.size() && s[i - j] == s[i + j]) { ++j; } radius[i] = j; int k = 1; while(i - k >= 0 && i + k < s.size() && k + radius[i - k] < j) { radius[i + k] = radius[i - k]; ++k; } i += k; j -= k; } return radius; } int main(){ ll n,m;cin >> n >> m; string s;cin >> s; string t;cin >> t; if(m%2)cout << -1 << endl,exit(0); rep(i,t.size()){ if(t[i]!=t[t.size()-1-i])cout << -1 << endl,exit(0); } t=t.substr(0,m/2); m/=2; vl rad(m); { string nt="$"; rep(i,m){ nt+=t[i];nt+="$"; } auto man=manacher(nt); rep(i,m-1)rad[i]=man[i*2+2]/2; } //rep(i,m)cout << rad[i] <<" ";cout << endl; vl dist(m,inf); ll st=-1; { rep(i,s.size()){ if(i>=t.size())break; if(s[i]!=t[i])break; chmax(st,i); } rev(all(s)); rep(i,s.size()){ if(i>=t.size())break; if(s[i]!=t[i])break; chmax(st,i); } } if(st==-1)cout << -1 << endl,exit(0); dist[st]=1; vvpl g(m); rep(i,m-1)g[i+1].push_back({i,0}); rep(i,m-1){ if(rad[i])g[i].push_back({i+rad[i],1}); } deque que;que.push_back(st); while(!que.empty()){ ll v=que.front();que.pop_front(); for(auto p:g[v]){ if(p.second==0){ if(dist[p.first]>dist[v]){ dist[p.first]=dist[v]; que.push_front(p.first); } } else{ if(dist[p.first]>dist[v]+1){ dist[p.first]=dist[v]+1; que.push_back(p.first); } } } } //rep(i,m)cout << dist[i] <<" ";cout << endl; if(dist[m-1]>=inf)cout << -1 << endl; else cout << dist[m-1] << endl; }