cin >> s + 1 >> d; m = strlen(s + 1); n = m + d; for (int i = 1; i <= m; ++i) a[i] = s[i] - 'A'; vector<int> tmp(m + 1); for (int i = 0; i <= m; ++i) tmp[i] = i; f[0][tmp] = 1; for (int i = 0; i < n; ++i) for (auto [s, v] : f[i]) { if (*min_element(s.begin(), s.end()) > d) continue; for (int k = 0; k < 26; ++k) { vector<int> t(m + 1); t[0] = i + 1; for (int i = 1; i <= m; ++i) { if (a[i] == k) t[i] = s[i - 1]; else t[i] = min({ t[i - 1], s[i - 1], s[i] }) + 1; } f[i + 1][t] = (f[i + 1][t] + v) % p; } } int ans = 0; for (int i = 0; i <= n; ++i) for (auto [vec, val] : f[i]) if (vec[m] == d) ans = (ans + val) % p; cout << ans << "\n"; return0; }