#include using namespace std; typedef long long ll; typedef pair< int, int > pii; typedef vector< int > vi; typedef vector< vi > vvi; typedef vector< ll > vl; typedef vector< vl > vvl; typedef vector< pii > vp; typedef vector< vp > vvp; typedef vector< string > vs; typedef vector< double > vd; typedef vector< vd > vvd; template< class T1, class T2 > int upmin( T1 &x, T2 v ){ if( x > v ){ x = v; return 1; } return 0; } template< class T1, class T2 > int upmax( T1 &x, T2 v ){ if( x < v ){ x = v; return 1; } return 0; } const int INF = 0x3f3f3f3f; int N, M; string S; string T; void init(){ cin >> N >> M; cin >> S; cin >> T; } int dp[ 1200 + 1 ][ 1200 + 1 ][ 3 ][ 3 ]; tuple< int, int, int, int > pre[ 1200 + 1 ][ 1200 + 1 ][ 3 ][ 3 ]; int ds[ 1200 + 1 ][ 1200 + 1 ][ 3 ][ 3 ]; void preprocess(){ memset( dp, INF, sizeof( dp ) ); dp[ 0 ][ 0 ][ 0 ][ 0 ] = 0; for( int i = 0; i <= N; ++i ) for( int j = 0; j <= M; ++j ) for( int t1 = 0; t1 < 3; ++t1 ) for( int t2 = 0; t2 < 3; ++t2 ) if( dp[ i ][ j ][ t1 ][ t2 ] < INF ){ if( i < N and j < M and S[ i ] == T[ j ] ){ if( upmin( dp[ i + 1 ][ j + 1 ][ 0 ][ 0 ], dp[ i ][ j ][ t1 ][ t2 ] ) ) pre[ i + 1 ][ j + 1 ][ 0 ][ 0 ] = tie( i, j, t1, t2 ), ds[ i + 1 ][ j + 1 ][ 0 ][ 0 ] = 0; } if( i + 1 <= N and j + 1 <= M ){ if( upmin( dp[ i + 1 ][ j + 1 ][ 0 ][ 0 ], dp[ i ][ j ][ t1 ][ t2 ] + 5 ) ) pre[ i + 1 ][ j + 1 ][ 0 ][ 0 ] = tie( i, j, t1, t2 ), ds[ i + 1 ][ j + 1 ][ 0 ][ 0 ] = 1; } if( i + 1 <= N ){/* if( upmin( dp[ i + 1 ][ j ][ 1 ][ t2 ], dp[ i ][ j ][ t1 ][ t2 ] + ( t1 != 1 ? 9 : 2 ) ) ) pre[ i + 1 ][ j ][ 1 ][ t2 ] = tie( i, j, t1, t2 ), ds[ i + 1 ][ j ][ 1 ][ t2 ] = 2;*/ if( upmin( dp[ i + 1 ][ j ][ t1 ][ 2 ], dp[ i ][ j ][ t1 ][ t2 ] + ( t2 != 2 ? 9 : 2 ) ) ) pre[ i + 1 ][ j ][ t1 ][ 2 ] = tie( i, j, t1, t2 ), ds[ i + 1 ][ j ][ t1 ][ 2 ] = 3; } if( j + 1 <= M ){ if( upmin( dp[ i ][ j + 1 ][ 2 ][ t2 ], dp[ i ][ j ][ t1 ][ t2 ] + ( t1 != 2 ? 9 : 2 ) ) ) pre[ i ][ j + 1 ][ 2 ][ t2 ] = tie( i, j, t1, t2 ), ds[ i ][ j + 1 ][ 2 ][ t2 ] = 4;/* if( upmin( dp[ i ][ j + 1 ][ t1 ][ 1 ], dp[ i ][ j ][ t1 ][ t2 ] + ( t2 != 1 ? 9 : 2 ) ) ) pre[ i ][ j + 1 ][ t1 ][ 1 ] = tie( i, j, t1, t2 ), ds[ i ][ j + 1 ][ t1 ][ 1 ] = 5;*/ } } } void solve(){ int min_cost = INF; for( int i = 0; i < 3; ++i ) for( int j = 0; j < 3; ++j ) upmin( min_cost, dp[ N ][ M ][ i ][ j ] ); cout << min_cost << endl; for( int i = 0; i < 3; ++i ) for( int j = 0; j < 3; ++j ) if( min_cost == dp[ N ][ M ][ i ][ j ] ){ stack< char > s1, s2; for( int a = N, b = M, c = i, d = j; not ( a == 0 and b == 0 ); tie( a, b, c, d ) = pre[ a ][ b ][ c ][ d ] ){ int op = ds[ a ][ b ][ c ][ d ]; switch( op ){ case 0: s1.emplace( S[ a - 1 ] ), s2.emplace( T[ b - 1 ] ); break; case 1: s1.emplace( S[ a - 1 ] ), s2.emplace( T[ b - 1 ] ); break; case 2: s1.emplace( '-' ); break; case 3: s1.emplace( S[ a - 1 ] ), s2.emplace( '-' ); break; case 4: s1.emplace( '-' ), s2.emplace( T[ b - 1 ] ); break; case 5: s2.emplace( '-' ); break; } } string str1, str2; for( ; not s1.empty(); s1.pop(), s2.pop() ) str1 += s1.top(), str2 += s2.top(); cout << str1 << endl; cout << str2 << endl; exit( 0 ); } } signed main(){ ios::sync_with_stdio( 0 ); init(); preprocess(); solve(); return 0; }