// Arup Guha
// 4/12/2023
// Solution to 2017 MAPS Problem E: King of Spades
// Note: This is slower than my optimized solution with a rolling hash 
//       and integer hash function, but it passes on Kattis.

import java.util.*;
import java.io.*;

public class kingofspades {

	final public static long MOD = 1000000007L*1000003L;
	final public static long BASE = 101L;
	
	final public static int[] VALS = {1,2,3,4,5,6,7,8,9,10,10,10,10,0,0,0,0};
	
	public static TreeSet<Integer> kList1;
	public static TreeSet<Integer> kList2;
	public static int n;
	public static String[] list1;
	
	public static int m;
	public static String[] list2;
	
	public static void main(String[] args) throws Exception {
	
		BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer tok = new StringTokenizer(stdin.readLine());
		n = Integer.parseInt(tok.nextToken());
		list1 = new String[n];
		m = Integer.parseInt(tok.nextToken());
		list2 = new String[m];
		kList1 = new TreeSet<Integer>();
		kList2 = new TreeSet<Integer>();

		// Read in cards.
		tok = new StringTokenizer(stdin.readLine());
		for (int i=0; i<n; i++) list1[i] = tok.nextToken();
		tok = new StringTokenizer(stdin.readLine());
		for (int i=0; i<m; i++) list2[i] = tok.nextToken();
		
		// Store King of Spades locations.
		for (int i=0; i<n; i++) if (list1[i].equals("KS")) kList1.add(i);
		for (int i=0; i<m; i++) if (list2[i].equals("KS")) kList2.add(i);
		
		// Get cumulative frequency arrays.
		int[][] cf1 = getCF(list1);
		int[][] cf2 = getCF(list2);
		
		int res = 0;
		
		// Try each length.
		for (int len=1; len<=100; len++) {
			
			// Get out!
			if (len > n || len > m) break;
			
			// will map hash in first list to starting position.
			HashMap<Long, LinkedList<Integer>> map = new HashMap<Long, LinkedList<Integer>>();
			
			// sequence is from index s to index s+len-1.
			for (int s=0; s+len<=n; s++) {
				
				// Skip if has K of S.
				Integer nextKS = kList1.higher(s-1);
				if (nextKS != null && nextKS <= s+len-1) continue;
				
				long myh = getHash(cf1, s, s+len-1);
				
				// Add entry, if necessary.
				if (!map.containsKey(myh)) {
					LinkedList<Integer> tmp = new LinkedList<Integer>();
					map.put(myh, tmp);
				}
				
				// Now add this item to myh's list.
				map.get(myh).add(s);
			}
			
			// Now look at sequences of length len in the second list, starting at s.
			for (int s=0; s+len<=m; s++) {
				
				// Skip if it has K of S.
				Integer nextKS = kList2.higher(s-1);
				if (nextKS != null && nextKS <= s+len-1) continue;
				
				long myh = getHash(cf2, s, s+len-1);
				
				// A hit!
				if (map.containsKey(myh)) {
					LinkedList<Integer> tmp = map.get(myh);
					
					// See if we have a match. Key to break out once we find just one.
					for (Integer x: tmp) {
						int score = verify(x, cf1, s, cf2, len);
						res = Math.max(res, score);
						if (score != 0) break;
					}
				}
			}
		} // end len loop.
		
		// Ta da!
		System.out.println(res);
	}
	
	// Just verifies the results of the hash by looking at the CF arrays.
	public static int verify(int s1, int[][] cf1, int s2, int[][] cf2, int len) {
		
		int score = 0;
		for (int i=0; i<cf1.length; i++) {
			int cnt1 = cf1[i][s1+len]-cf1[i][s1];
			int cnt2 = cf2[i][s2+len]-cf2[i][s2];
			
			// Some kind or suit count doesn't match.
			if (cnt1 != cnt2) return 0;
			
			// Update score.
			score += cnt1*VALS[i];
		}
		
		// We get the score for both sets.
		return 2*score;
	}
	
	// Returns the cumulative frequency arrays for this list.
	public static int[][] getCF(String[] list) {
	
		int len = list.length;
		int[][] cf = new int[17][len+1];
		for (int i=0; i<len; i++) {
			
			// Set to previous value.
			for (int j=0; j<17; j++) cf[j][i+1] = cf[j][i];
			
			int idx1 = convert(list[i].charAt(0));
			int idx2 = convert(list[i].charAt(1));
			
			// Add 1 at these two spots.
			cf[idx1][i+1]++;
			cf[idx2][i+1]++;
		}
	
		return cf;
	}
	
	// My hash function for the range of cards in cf from sI to eI, inclusive.
	public static long getHash(int[][] cf, int sI, int eI) {
		long res = 0;
		for (int i=0; i<17; i++)
			res = (BASE*res + cf[i][eI+1]-cf[i][sI])%MOD;
		return res;
	}
	
	// My code for which index to store each kind and suit.
	public static int convert(char c) {
		if (c == 'A') return 0;
		if (c >= '2' && c <= '9') return c - '1';
		if (c == 'T') return 9;
		if (c == 'J') return 10;
		if (c == 'Q') return 11;
		if (c == 'K') return 12;
		if (c == 'S') return 13;
		if (c == 'H') return 14;
		if (c == 'D') return 15;
		return 16;
	}
}