#include int lex_smaller(char s1[], char s2[]) { int i; for (i = 0; s1[i] != 0 && s2[i] != 0; i++) { if (s1[i] < s2[i]) return 1; else if (s1[i] > s2[i]) return -1; } if (s1[i] == s2[i]) return 0; else if (s1[i] == 0) return 1; else return -1; } // rad[i] = max radius of palindrome with center between s[i-1] and s[i] void Manacher_even(int N, char s[], int rad[]) { int i, j, l, r; for (i = 1, rad[0] = 0, rad[1] = 0, rad[N] = 0; i < N; i = r) { for (l = i - rad[i] - 1, r = i + rad[i]; l >= 0 && r < N && s[l] == s[r]; l--, r++); rad[i] = r - i; for (l = i - 1, r = i + 1; r - i < rad[i] && l > 0 && r < N; l--, r++) { if (l - rad[l] == i - rad[i]) { rad[r] = i + rad[i] - r; break; } else if (l - rad[l] > i - rad[i]) rad[r] = rad[l]; else rad[r] = i + rad[i] - r; } } } int main() { int i, j, N, M, rad[500001]; char S[500001], T[500001]; scanf("%d %d", &N, &M); scanf("%s", S); scanf("%s", T); Manacher_even(M, T, rad); for (i = 0; i < N && i < M && S[i] == T[i]; i++); for (j = 0; j < N && j < M && S[N-j-1] == T[M-j-1]; j++); if (M % 2 != 0 || rad[M/2] != M / 2 || (i == 0 && j == 0)) { printf("-1\n"); fflush(stdout); return 0; } int dist[500001] = {}, q[1000001], head = 500000, tail = 500000; if (i < j) i = j; if (i == M) i--; dist[i] = 1; q[tail++] = i; for (; head < tail; head++) { i = q[head]; if (i > 0 && dist[i-1] == 0) { dist[i-1] = dist[i]; q[head--] = i - 1; } j = i + rad[i]; if (dist[j] == 0) { dist[j] = dist[i] + 1; q[tail++] = j; } } printf("%d\n", dist[M] - 1); fflush(stdout); return 0; }