Problem:
Score : 300 points

Problem Statement

You are given a string S of length N and another string T of length M.
These strings consist of lowercase English letters.

A string X is called a good string when the following conditions are all met:

Let L be the length of X. L is divisible by both N and M.

Concatenating the 1-st, (\frac{L}{N}+1)-th, (2 \times \frac{L}{N}+1)-th, ..., ((N-1)\times\frac{L}{N}+1)-th characters of X, without changing the order, results in S.

Concatenating the 1-st, (\frac{L}{M}+1)-th, (2 \times \frac{L}{M}+1)-th, ..., ((M-1)\times\frac{L}{M}+1)-th characters of X, without changing the order, results in T.

Determine if there exists a good string. If it exists, find the length of the shortest such string.

Constraints

1 \leq N,M \leq 10^5

S and T consist of lowercase English letters.

|S|=N

|T|=M

Input

Input is given from Standard Input in the following format:

N M
S
T

Output

If a good string does not exist, print -1; if it exists, print the length of the shortest such string.

Sample Input 1

3 2
acp
ae

Sample Output 1

6

For example, the string accept is a good string.
There is no good string shorter than this, so the answer is 6.

Sample Input 2

6 3
abcdef
abc

Sample Output 2

-1

Sample Input 3

15 9
dnsusrayukuaiia
dujrunuma

Sample Output 3

45

Solution in Java8 (OpenJDK 1.8.0) (judge verdict: Wrong Answer)

import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;

class Main{

	//static long[] a;
	static	int N ;
	static	int M ;
	static int[][] dp;

	static	boolean[] flag ;

	public static void main(String[] args) {

		Scanner scanner = new Scanner(System.in);			//文字の入力
		N = scanner.nextInt();
		M = scanner.nextInt();

		long ans = lcm(N, M);

		String s = scanner.next();
		String t = scanner.next();




		if(N >= M){
			int cc = 1;
			int ccc = 1;

			for(int x= 1;x < 5;x++){
				boolean jug = true;
				ans *= x;
				ArrayList<Long> tList = new ArrayList<Long>();
				ArrayList<Long> sList = new ArrayList<Long>();

				for(long i = 0;ccc <= N;){					
						tList.add(i);
				
					i =  (ans/N)*ccc;
					ccc++;

				}
				for(long i = 0;cc <= M;){
					
						tList.add(i);
					

					i = (ans/M)*cc;
					cc++;
				}

				Collections.sort(tList);

				for(int i = 0;i < tList.size()-1;i++){
					if(tList.get(i) == tList.get(i+1)){
						tList.remove(i);
					}
				}



				cc = 0;
				ccc = 0;

				for(int i = 0;i < tList.size();i++){

					long key = tList.get(i);
					if(key == 0){
						if(!(s.substring(0,1).equals(t.substring(0,1)))){
							jug = false;
							break;
						}else{
							cc ++;
							ccc++;
						}
					}
					else if((key % (ans/N)) == 0 && !(key % (ans/M)  == 0 )){
						cc++;
					}
					else if((key % (ans/M)  == 0 && !(key % (ans/N)  == 0 ))){
						ccc++;
					}

					else	if((key % (ans/N) == 0) && (key % (ans/M)  == 0 )){
						if(!(s.substring(cc,cc+1).equals(t.substring(ccc,ccc+1)))){
							//						System.out.println("cc " +cc+" ccc "+ccc+" i "+i);
							jug = false;
							break;

						}else{
							cc ++;
							ccc++;
						}
					}
				}
				if(jug){
					System.out.println(ans);
					return;
				}
			}

			System.out.println("-1");

		}else{
			int cc = 1;
			int ccc = 1;

			for(int x= 1;x < 5;x++){
				boolean jug = true;
				ans *= x;
				ArrayList<Long> tList = new ArrayList<Long>();
				ArrayList<Long> sList = new ArrayList<Long>();

				for(long i = 0;ccc <= M;){
					
						tList.add(i);
					

					i = (ans/M)*ccc;
					ccc++;

				}
				for(long i = 0;cc <= N;){
					
						tList.add(i);
					

					i = (ans/N)*cc;
					cc++;

				}

				Collections.sort(tList);
				for(int i = 0;i < tList.size()-1;i++){
					if(tList.get(i) == tList.get(i+1)){
						tList.remove(i);
					}
				}


				cc = 0;
				ccc = 0;
				for(int i = 0;i < tList.size();i++){

					long key = tList.get(i);
					if(key == 0){
						if(!(t.substring(0,1).equals(s.substring(0,1)))){
							jug = false;
							break;
						}else{
							cc ++;
							ccc++;
						}
					}
					else if((key % (ans/N)) == 0 && !(key % (ans/M)  == 0 )){
						cc++;
					}
					else if((key % (ans/M)  == 0 && !(key % (ans/N)  == 0 ))){
						ccc++;
					}

					else if((key % (ans/N) == 0) && (key % (ans/M)  == 0 )){
						if(!(t.substring(ccc,ccc+1).equals(s.substring(cc,cc+1)))){
							jug = false;
							break;

						}else{
							cc ++;
							ccc++;
						}
					}
				}
				if(jug){
					System.out.println(ans);
					return;
				}


			}
			System.out.println("-1");


		}




	}
	static long lcm(long m, long n) {
		return m * n / gcd(m, n);
	}
	static long gcd(long m, long n) {
		if(m < n) return gcd(n, m);
		if(n == 0) return m;
		return gcd(n, m % n);
	}
}