#include <bits/stdc++.h>
using namespace std;
using ll = long long int;
#define inf 1000000000000000LL
#define mod 1000000007LL
#define FOR(i,a,b) for(int i = a; i < b; i++)
const ll Mod = 1000000009LL;
const ll Base = 271LL;

struct hashed_string{
    private:
    string s; ll n;
    vector <ll> prefix;
    vector <ll> pow;
    public:
    hashed_string(string a){
        s = a; n = a.size();
        prefix.assign(n, 0);
        ll sum = 0;
        pow.assign(n+1, 0);
        pow[0] = 1;
        for (int i = 1; i <= n; i++){
            pow[i] = (pow[i-1] * Base) % Mod;
        }
        for (int i = 0; i < n; i++){
            prefix[i] = ((sum * Base) % Mod + s[i] - 'a' + 1) % Mod;
            sum = prefix[i];
        }
    }
    hashed_string(){}
    ll get_hash(ll l, ll r){
        ll x = prefix[r];
        if (l >= 1){
            l--;
            x = ((x - prefix[l] * pow[r - l] % Mod) % Mod + Mod) % Mod;
        }
        return x;
    }
};
void solve() {
    ll n,m;
    cin >> n >> m;
    vector <string> l1( 2 * n);
    for (int i = 0; i < 2 * n; i++) cin >> l1[i];
    vector <hashed_string> v( 2 * n);
    for (int i = 0; i < 2 * n; i++) v[i] = hashed_string(l1[i]);
    auto flag = [&](ll length) -> bool{
        for (int j = 0; j + length - 1 < m; j++){
            set<ll> f1;
            for (int i = 0; i < n; i++){
                f1.insert(v[i].get_hash(j, j + length - 1));
            }
            bool flag = true;
            for (int i = n; i < 2 * n; i++){
                if (f1.find(v[i].get_hash(j, j + length - 1)) != f1.end()){
                    flag = false;
                    break;
                }
            }
            if (flag) return true; 
        }
        return false;
    };
    ll lo = 1;
    ll hi = m;
    while (lo != hi){
        ll mid = (lo + hi) / 2;
        if (flag(mid)){
            hi = mid;
        }  
        else{
            lo = mid + 1;
        }
    }
    cout << lo << "\n";
}

int main() {
    freopen("cownomics.in","r",stdin);
    freopen("cownomics.out","w",stdout);
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    ll t = 1;
    while (t--) {
        solve();
    }

    return 0;
}