LCSLCSLCS

아직 제출이 없습니다시간 제한10초메모리 제한1024 MB

문제

Suppose we have two non-empty strings AA and BB (A,B500\lvert A \rvert, \lvert B \rvert \le 500) of capital English letters, and two integers nn and mm such that 1n,m10151 \le n, m \le 10^{15}.

Let string AnA^n be a concatenation of nn copies of string AA. Let string BmB^m be a concatenation of mm copies of string BB. Your task is to find the longest common subsequence of AnA^n and BmB^m.

입력

On the first line, there are two integers nn and mm (1n,m10151 \le n, m \le 10^{15}).

On the second line, there is a non-empty string AA with length at most 500500.

On the third line, there is a non-empty string BB with length at most 500500.

Both strings consist of capital English letters.

출력

Output one integer: the length of the longest common subsequence of AnA^n and BmB^m.