#include <iostream>
#include <vector>
#include <cstring>
#include <string>
#include <algorithm>
#include <iomanip>
#include <cmath>
#include <cassert>
#include <functional>
#include <queue>
#include <sstream>
#include <map>
using namespace std;

const int MAX_V = 1000;
using Capacity = int;
using Cost = int;
const auto inf = numeric_limits<Capacity>::max() / 8;

struct Edge {
	int dst;
	Capacity cap, cap_orig;
	Cost cost;
	int revEdge; bool isRev;
	Edge(int dst, Capacity cap, Cost cost, int revEdge, bool isRev)
		:dst(dst), cap(cap), cap_orig(cap), cost(cost), revEdge(revEdge), isRev(isRev) {
	}
};

struct PrimalDual {
	int n;
	vector<vector<Edge> > g;
	PrimalDual(int n_) : n(n_), g(vector<vector<Edge> >(n_)) {}
	void add_edge(int src, int dst, Capacity cap, Cost cost) { // ?????????
		g[src].emplace_back(dst, cap, cost, g[dst].size(), false);
		g[dst].emplace_back(src, 0, -cost, g[src].size() - 1, true);
	}
	Cost solve(int s, int t, int f) {
		Cost res = 0;
		static Cost h[MAX_V], dist[MAX_V];
		static int prevv[MAX_V], preve[MAX_V];
		for(int i = 0; i < n; i++) {
			h[i] = 0;
		}
		while(f > 0) {
			typedef pair<Cost, int> pcv;
			priority_queue<pcv, vector<pcv>, greater<pcv> > q;
			for(int i = 0; i < n; i++) {
				dist[i] = inf;
			}
			dist[s] = 0;
			q.emplace(pcv(0, s));
			while(q.size()) {
				pcv p = q.top(); q.pop();
				int v = p.second;
				if(dist[v] < p.first) continue;
				for(int i = 0; i < g[v].size(); i++) {
					Edge &e = g[v][i];
					if(e.cap > 0 && dist[e.dst] > dist[v] + e.cost + h[v] - h[e.dst]) {
						dist[e.dst] = dist[v] + e.cost + h[v] - h[e.dst];
						prevv[e.dst] = v;
						preve[e.dst] = i;
						q.emplace(pcv(dist[e.dst], e.dst));
					}
				}
			}
			if(dist[t] == inf) {
				return -1;
			}
			for(int v = 0; v < n; v++) {
				h[v] += dist[v];
			}
			// s-t ????????????????????£??????????????????
			int d = f;
			for(int v = t; v != s; v = prevv[v]) {
				d = min(d, g[prevv[v]][preve[v]].cap);
			}
			f -= d;
			res += d * h[t];
			for(int v = t; v != s; v = prevv[v]) {
				Edge &e = g[prevv[v]][preve[v]];
				e.cap -= d;
				g[v][e.revEdge].cap += d;
			}
		}
		return res;
	}
	// ??????????????????=???????????????-?????¨??????????????¨???
	void view() {
		for(int i = 0; i < g.size(); i++) {
			for(int j = 0; j < g[i].size(); j++) {
				if(!g[i][j].isRev) {
					Edge& e = g[i][j];
					printf("%3d->%3d (flow:%d)\n", i, e.dst, e.cap_orig - e.cap);
				}
			}
		}
	}
};


string dic[400];
int dp[1000][1000];

int f(string& s, string& t) {
	int n = s.size(), m = t.size();
	for(int i = 0; i < n + 1; i++) {
		for(int j = 0; j < m + 1; j++) {
			dp[i][j] = 1000000;
		}
	}
	dp[0][0] = 0;
	for(int i = 0; i <= n; i++) {
		for(int j = 0; j <= m; j++) {
			if(i < n) {
				dp[i + 1][j] = min(dp[i + 1][j], dp[i][j] + 1);
			}
			if(j < m) {
				dp[i][j + 1] = min(dp[i][j + 1], dp[i][j] + 1);
			}
			if(i < n && j < m) {
				int c = s[i] != t[j];
				dp[i + 1][j + 1] = min(dp[i + 1][j + 1], dp[i][j] + c);
			}
		}
	}
	return dp[n][m];
}


int main() {
	cin.tie(0);
	ios::sync_with_stdio(false);

	vector<string> s;
	map<string, int> map;
	int N, M;
	cin >> N >> M;
	cin.ignore();
	for(int i = 0; i < N; i++) {
		string a;
		getline(cin, a);
		stringstream ss(a);
		while(ss >> a) {
			s.push_back(a);
			map[a]++;
		}
	}
	sort(s.begin(), s.end());
	s.erase(unique(s.begin(), s.end()), s.end());

	for(int i = 0; i < M; i++) {
		cin >> dic[i];
	}

	N = s.size();
	int S = N + M, T = N + M + 1;
	PrimalDual pd(N + M + 2);
	for(int i = 0; i < N; i++) {
		for(int j = 0; j < M; j++) {
			int cost = f(s[i], dic[j]) * map[s[i]];
			//cout << S[i][j] + " " + dic[k] << " " << cost << endl;
			pd.add_edge(i, N + j, 1, cost);
		}
	}
	for(int j = 0; j < N; j++) {
		pd.add_edge(S, j, 1, 0);
	}
	for(int j = 0; j < M; j++) {
		pd.add_edge(N + j, T, 1, 0);
	}
	cout << pd.solve(S, T, N) << endl;
}

Measured memory_bytes: 14740