#include "bits/stdc++.h" using namespace std; #define rep(i,n) for(int (i)=0;(i)<(int)(n);++(i)) #define rer(i,l,u) for(int (i)=(int)(l);(i)<=(int)(u);++(i)) #define reu(i,l,u) for(int (i)=(int)(l);(i)<(int)(u);++(i)) static const int INF = 0x3f3f3f3f; static const long long INFL = 0x3f3f3f3f3f3f3f3fLL; typedef vector vi; typedef pair pii; typedef vector > vpii; typedef long long ll; template static void amin(T &x, U y) { if(y < x) x = y; } template static void amax(T &x, U y) { if(x < y) x = y; } typedef pair P; struct DP { vector

dp; int n, m; DP(int n, int m) : n(n), m(m), dp((n + 1) * (m + 1) * 4, P{ INF,{-1,-1} }) {} P &operator()(int i, int j, int lasti, int lastj) { return dp[(i * (m + 1) + j) * 4 + lasti * 2 + lastj]; } }; int main() { int n; int m; while(~scanf("%d%d", &n, &m)) { char S[1201], T[1201]; scanf("%s", S); scanf("%s", T); DP dp(n, m); dp(0, 0, 0, 0) = { 0, {-1, -1} }; rep(i, n + 1) rep(j, m + 1) rep(lasti, 2) rep(lastj, 2) { int x = dp(i, j, lasti, lastj).first; if(x == INF) continue; pair p(i * 2 + lasti, j * 2 + lastj); if(i < n && j < m) amin(dp(i + 1, j + 1, 0, 0), P{ x + (S[i] == T[j] ? 0 : 5), p }); if(i < n) amin(dp(i + 1, j, 1, 0), P{ x + (lasti == 0 ? 9 : 2), p }); if(j < m) amin(dp(i, j + 1, 0, 1), P{ x + (lastj == 0 ? 9 : 2), p }); } pair ans{ INF,{} }; rep(lasti, 2) rep(lastj, 2) amin(ans, make_pair(dp(n, m, lasti, lastj).first, make_pair(lasti, lastj))); printf("%d\n", ans.first); string alignment[2]; { int i = n, j = m, lasti = ans.second.first, lastj = ans.second.second; while(i != 0 || j != 0) { int pi, pj; tie(pi, pj) = dp(i, j, lasti, lastj).second; int ni = pi / 2, nj = pj / 2; alignment[0] += ni + 1 == i ? S[ni] : '-'; alignment[1] += nj + 1 == j ? T[nj] : '-'; i = ni, j = nj; lasti = pi % 2, lastj = pj % 2; } rep(k, 2) reverse(alignment[k].begin(), alignment[k].end()); } puts(alignment[0].c_str()); puts(alignment[1].c_str()); } return 0; }