#include using namespace std; #define all(a) begin(a), end(a) #define rall(a) rbegin(a), rend(a) #define uniq(a) (a).erase(unique(all(a)), (a).end()) #define SZ(x) ((int)(x).size()) #define pb(x) push_back(x) #define eb(x) emplace_back(x) #define vsum(x) reduce(all(x)) #define vmax(a) *max_element(all(a)) #define vmin(a) *min_element(all(a)) #define LB(c, x) distance((c).begin(), lower_bound(all(c), (x))) #define UB(c, x) distance((c).begin(), upper_bound(all(c), (x))) #define mp make_pair #define endl '\n' using ll = long long; using ull = unsigned long long; using ld = long double; using Pi = pair; using Pl = pair; using Vi = vector; using Vl = vector; using Vc = vector; using VVi = vector>; using VVl = vector>; using VVc = vector>; template using P = pair; template using V = vector; template using VV = V>; constexpr ll inf = 1000000000ll; constexpr ll INF = 4000000004000000000LL; constexpr ld eps = 1e-15; constexpr ld PI = 3.141592653589793; constexpr int popcnt(ull x) { return __builtin_popcountll(x); } template using mat = vector>; constexpr ll dy[9] = {0, 1, 0, -1, 1, 1, -1, -1, 0}; constexpr ll dx[9] = {1, 0, -1, 0, 1, -1, -1, 1, 0}; constexpr ll sign(ll a) { return (a > 0) - (a < 0); } constexpr ll fdiv(ll a, ll b) { return a / b - ((a ^ b) < 0 && a % b); } constexpr ll cdiv(ll a, ll b) { return -fdiv(-a, b); } constexpr ull bit(int n) { return 1ull << n; } template constexpr T mypow(T x, ll n) { T ret = 1; while (n) { if (n & 1) ret *= x; x *= x; n >>= 1; } return ret; } constexpr ll modpow(ll x, ll n, ll mod) { ll ret = 1; while (n) { if (n & 1) ret *= x; x *= x; n >>= 1; x %= mod; ret %= mod; } return ret; } template T xor64(T lb, T ub) { static ull x = 88172645463325252ull; x ^= x << 7; return lb + (x ^= x >> 9) % (ub - lb); } constexpr ll safemod(ll x, ll mod) { return (x % mod + mod) % mod; } template constexpr T sq(const T &a) { return a * a; } template using priority_queue_rev = priority_queue, greater>; template bool chmax(T &a, const U &b) { return a < b ? a = b, true : false; } template bool chmin(T &a, const U &b) { return a > b ? a = b, true : false; } template T make_vector(T &&a) { return a; } template auto make_vector(int h, Ts &&... ts) { return vector(h, make_vector(ts...)); } template ostream &operator<<(ostream &os, const pair &a) { os << "(" << a.first << ", " << a.second << ")"; return os; } template ostream &operator<<(ostream &os, const tuple &a) { os << "(" << get<0>(a) << ", " << get<1>(a) << ", " << get<2>(a) << ")"; return os; } template ostream &operator<<(ostream &os, const vector &a) { os << "("; for (auto itr = a.begin(); itr != a.end(); ++itr) os << *itr << (next(itr) != a.end() ? ", " : ""); os << ")"; return os; } template ostream &operator<<(ostream &os, const set &a) { os << "("; for (auto itr = a.begin(); itr != a.end(); ++itr) os << *itr << (next(itr) != a.end() ? ", " : ""); os << ")"; return os; } template ostream &operator<<(ostream &os, const multiset &a) { os << "("; for (auto itr = a.begin(); itr != a.end(); ++itr) os << *itr << (next(itr) != a.end() ? ", " : ""); os << ")"; return os; } template ostream &operator<<(ostream &os, const map &a) { os << "("; for (auto itr = a.begin(); itr != a.end(); ++itr) os << *itr << (next(itr) != a.end() ? ", " : ""); os << ")"; return os; } template void print(const T &a) { cout << a << endl; } template void print(const vector &v) { for (auto &e : v) cout << e << " "; cout << endl; } template void scan(vector &a) { for (auto &i : a) cin >> i; } struct timer { clock_t start_time; void start() { start_time = clock(); } int lap() { // return x ms. return (clock() - start_time) * 1000 / CLOCKS_PER_SEC; } }; template struct Edge { int from, to; T cost; int idx; Edge() = default; Edge(int from, int to, T cost = 1, int idx = -1) : from(from), to(to), cost(cost), idx(idx) {} operator int() const { return to; } }; template struct Graph { vector>> g; int es; Graph() = default; explicit Graph(int n) : g(n), es(0) {} size_t size() const { return g.size(); } void add_directed_edge(int from, int to, T cost = 1) { g[from].emplace_back(from, to, cost, es++); } void add_edge(int from, int to, T cost = 1) { g[from].emplace_back(from, to, cost, es); g[to].emplace_back(to, from, cost, es++); } void read(int M, int padding = -1, bool weighted = false, bool directed = false) { for (int i = 0; i < M; i++) { int a, b; cin >> a >> b; a += padding; b += padding; T c = T(1); if (weighted) cin >> c; if (directed) add_directed_edge(a, b, c); else add_edge(a, b, c); } } }; #ifdef ONLINE_JUDGE #define dump(...) (void(0)) #else void debug() { cerr << endl; } template void debug(Head &&head, Tail &&... tail) { cerr << head; if (sizeof...(Tail)) cerr << ", "; debug(tail...); } #define dump(...) cerr << __LINE__ << ": " << #__VA_ARGS__ << " = ", debug(__VA_ARGS__) #endif struct rep { struct itr { ll v; itr(ll v) : v(v) {} void operator++() { ++v; } ll operator*() const { return v; } bool operator!=(itr i) const { return v < *i; } }; ll l, r; rep(ll l, ll r) : l(l), r(r) {} rep(ll r) : rep(0, r) {} itr begin() const { return l; }; itr end() const { return r; }; }; struct per { struct itr { ll v; itr(ll v) : v(v) {} void operator++() { --v; } ll operator*() const { return v; } bool operator!=(itr i) const { return v > *i; } }; ll l, r; per(ll l, ll r) : l(l), r(r) {} per(ll r) : per(0, r) {} itr begin() const { return r - 1; }; itr end() const { return l - 1; }; }; struct io_setup { static constexpr int PREC = 20; io_setup() { cout << fixed << setprecision(PREC); cerr << fixed << setprecision(PREC); }; } iOS; template struct modint { ll val; modint(ll val = 0) : val(val >= 0 ? val % MOD : (MOD - (-val) % MOD) % MOD) {} static ll mod() { return MOD; } modint inv() const { ll a = val, b = MOD, u = 1, v = 0, t; while (b > 0) { t = a / b; swap(a -= t * b, b); swap(u -= t * v, v); } return modint(u); } modint pow(ll k) const { modint ret = 1, mul = val; while (k) { if (k & 1) ret *= mul; mul *= mul; k >>= 1; } return ret; } modint &operator+=(const modint &a) { if ((val += a.val) >= MOD) val -= MOD; return *this; } modint &operator-=(const modint &a) { if ((val += MOD - a.val) >= MOD) val -= MOD; return *this; } modint &operator*=(const modint &a) { (val *= a.val) %= MOD; return *this; } modint &operator/=(const modint &a) { return *this *= a.inv(); } modint operator+() const { return *this; } modint operator-() const { return modint(-val); } friend bool operator==(const modint &a, const modint &b) { return a.val == b.val; } friend bool operator!=(const modint &a, const modint &b) { return rel_ops::operator!=(a, b); } friend modint operator+(const modint &a, const modint &b) { return modint(a) += b; } friend modint operator-(const modint &a, const modint &b) { return modint(a) -= b; } friend modint operator*(const modint &a, const modint &b) { return modint(a) *= b; } friend modint operator/(const modint &a, const modint &b) { return modint(a) /= b; } friend istream &operator>>(istream &is, modint &a) { ll val; is >> val; a = modint(val); return is; } friend ostream &operator<<(ostream &os, const modint &a) { return os << a.val; } }; struct rolling_hash { static constexpr ull MOD = bit(61) - 1; static vector pbase; vector hash; static void resize_pbase(int n) { int sz = pbase.size(); if (sz > n) return; pbase.resize(n + 1); for (int i : rep(sz - 1, n)) pbase[i + 1] = mul(pbase[i], pbase[1]); } template static T calc_mod(T val) { val = (val & MOD) + (val >> 61); if (val >= MOD) val -= MOD; return val; } static ull mul(ull a, ull b) { return calc_mod((__uint128_t)a * b); } static ull concat(ull lhs, ull rhs, int rn) { resize_pbase(rn); return calc_mod(mul(lhs, pbase[rn]) + rhs); } rolling_hash(const string &src) : hash(src.size() + 1) { for (int i : rep(src.size())) hash[i + 1] = calc_mod(mul(hash[i], pbase[1]) + src[i]); resize_pbase(src.size()); } template rolling_hash(const vector &src) : hash(src.size() + 1) { for (int i : rep(src.size())) hash[i + 1] = calc_mod(mul(hash[i], pbase[1]) + src[i]); resize_pbase(src.size()); } ull get_hash(int l, int r) const { return calc_mod(MOD - mul(hash[l], pbase[r - l]) + hash[r]); } }; vector rolling_hash::pbase{1, xor64(MOD >> 1, MOD)}; template ll bisect(ll ok, ll ng, F f) { while (abs(ok - ng) > 1) { ll mid = (ok + ng) / 2; (f(mid) ? ok : ng) = mid; } return ok; } struct oibfs { struct edge { int to, cost; }; vector> g; oibfs(int n) : g(n) {} void add_edge(int from, int to, int cost) { g[from].push_back({to, cost}); } pair, vector> get(int s) const { vector dist(g.size(), INT_MAX), prev(g.size(), -1); using P = pair; deque

deq; dist[s] = 0; deq.emplace_front(0, s); while (!deq.empty()) { auto [d, from] = deq.front(); deq.pop_front(); if (dist[from] < d) continue; for (auto [to, cost] : g[from]) { int nd = dist[from] + cost; if (nd < dist[to]) { dist[to] = nd; prev[to] = from; if (cost == 0) deq.emplace_front(nd, to); if (cost == 1) deq.emplace_back(nd, to); } } } return {dist, prev}; } }; int main() { ll n, m; cin >> n >> m; string s, t; cin >> s >> t; rolling_hash ord(t); reverse(all(t)); rolling_hash rev(t); reverse(all(t)); oibfs bfs(t.size() + 1); for (ll i : rep(1, t.size())) { ll m = bisect(0, t.size(), [&](ll x) -> bool { return i >= x && t.size() - i >= x && ord.get_hash(i - x, i) == rev.get_hash(t.size() - i - x, t.size() - i); }); bfs.add_edge(i, i + m, 1); } for (ll i : rep(t.size())) bfs.add_edge(i + 1, i, 0); auto f = [&](string s) -> ll { ll from; for (from = 0; from < min(s.size(), t.size()); ++from) { if (s[from] != t[from]) break; } return from; }; ll from = f(s); reverse(all(s)); chmax(from, f(s)); ll ans = bfs.get(from).first[t.size()]; if (ans == INT_MAX) cout << -1 << endl; else cout << ans << endl; }