cho.sh
Notes
Loading...

Guitar Chord

Time limit

2s

Memory limit

128 MB

Problem

In music, note names repeat in the following ascending order of 12 names.

A, A#, B, C, C#, D, D#, E, F, F#, G, G#

The note one step above G# is A, and the note five steps below B is F#. Therefore, moving 12 steps from any note returns to a note with the same name. In this problem, notes with the same name are considered the same regardless of octave.

A guitar has N strings, and each string is tuned to one of the 12 notes above. The sound made by plucking a string without pressing any fret is called that string's open note. If you press fret i and pluck the string, the note becomes i steps higher than the open note. A string whose open note is C# produces E when fret 3 is pressed.

A chord is a set of distinct notes played at the same time. To play a chord, every string must produce one of the notes in the chord, and every note in the chord must be produced by at least one string.

The difficulty of a way to play a chord is determined only by the strings whose frets are pressed. Take the largest pressed fret number, subtract the smallest pressed fret number, and add 1. Open strings are not included in this calculation. If a chord can be played using only open strings, its difficulty is 0.

Given the number N of guitar strings, the open note of each string, and the M notes in a chord, find the minimum possible difficulty.

Input

The first line contains N and M. N is the number of guitar strings, and M is the number of notes in the chord.

The second line contains N notes, the open notes of the strings. The third line contains M notes, the notes that form the chord.

The chord notes are distinct. Every note in the input is one of the 12 note names listed in the statement.

Output

Print the minimum possible difficulty.

Constraints

  • 1 ≤ M ≤ N ≤ 6

Hint

Open strings are ignored when computing difficulty. It can be optimal to let some strings cover chord notes as open strings while choosing pressed frets on the remaining strings so their range is as small as possible. Compare fret numbers using the fact that note names repeat every 12 steps.