Musical Plagiarism

Given a song as a sequence of notes and a suspect excerpt, decide whether the excerpt appears in the song under some transposition (key change).

Medium6String matchingArrayImplementationStringNo attempts yetTime limit2sMemory limit512 MB

Problem

A note is the basic unit of traditional Western music. Every note has a frequency, and two notes whose fundamental frequencies stand in a power of two ratio (one is half the other, one is twice the other, and so on) sound almost the same to the ear. Every note related that way therefore carries the same name.

There are twelve basic notes. Ordered by increasing frequency, each note sits the same distance from the previous one, and that distance is called a semitone. Seven of the twelve are written with the letters A, B, C, D, E, F and G. The table below gives the distance in semitones between those seven notes.

NotesA-BB-CC-DD-EE-FF-GG-A
Semitones2122122

Five notes are left without a letter name: the one between A and B, the one between C and D, the one between D and E, the one between F and G, and the one between G and A.

Two accidentals modify a note. A sharp, written #, raises the note by one semitone, and a flat, written b, lowers it by one semitone. A modified note is written as the note name followed by the symbol of the accidental. This notation writes all twelve notes.

The figure below labels part of a piano keyboard with the names described above.

A melody is written as a sequence of notes. For instance, A A D C# C# D E E E F# A D G# A is a well known melody. Because the semitone spacing is the same everywhere, the same melody can be written starting from another note. We then say the melody is in another key.

B B E D# D# E Gb Gb Gb G# B E A# B

Your neighbour is a composer who suspects that somebody plagiarised one of her songs. Given the melody of her song and a suspect excerpt, write a program that decides whether the excerpt occurs in the song in some key.

Input

The input holds several test cases. The first line of a test case has two integers M and T (1M1000001 \le M \le 100000, 1T100001 \le T \le 10000, TMT \le M). M is the number of notes in the song and T is the number of notes in the suspect excerpt. The next two lines hold the M notes of the song and the T notes of the excerpt, in that order.

Notes on the same line are separated by spaces. Each note is one of A, B, C, D, E, F or G, optionally followed by a modifier: # for a sharp or b for a flat.

The last test case is followed by a line that holds only two zeros separated by a space.

Output

For each test case print one line holding a single character: S if the excerpt really was plagiarised from the song, or N otherwise.