#include <bits/stdc++.h>

using namespace std;

pair<int, string> solve(string &s1, string &s2, int i = 0, int j = 0)
{
    if (i == s1.size() || j == s2.size())
        return {0, ""};

    if (s1[i] == s2[j])
    {
        auto [length, sub] = solve(s1, s2, i + 1, j + 1);
        return {1 + length, string(1, s1[i]) + sub};
    }

    auto [length1, sub1] = solve(s1, s2, i + 1, j);
    auto [length2, sub2] = solve(s1, s2, i, j + 1);
    if (length1 > length2) {
        return {length1, sub1};
    }
    return {length2, sub2};
}

int main()
{
    int n, m;
    cin >> n >> m;
    string s1, s2;
    cin >> s1 >> s2;

    auto [size, sub] = solve(s1, s2);
    cout << size << endl;
    cout << sub << endl;

    return 0;
}