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 C++14 (GCC 5.4.1) (judge verdict: Wrong Answer)

#include <bits/stdc++.h>

using namespace std;
using ll =long long;
using db = double;
using vll = vector<long long>;

template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
#define che(a,string) cout<<"//"<<string <<"=="<<(a)<<"//" <<endl;
#define SORT(a) sort((a).begin(),(a).end())
#define rSORT(a) reverse((a).begin(),(a).end())
#define For(i, a, b)    for(int i = (a) ; i < (b) ; ++i)
#define rep(i, n)       For(i, 0, n)
#define leftunique(a) {SORT(a);(a).erase(unique((a).begin(),(a).end()),(a).end());}
//leftuniqueは配列から同じ要素のものを取り除く 例 4 6 2 1 3 4 1 2 なら 1 2 3 4 6 になる
#define debug(x)  cout << #x << " = " << (x) << endl;
bool IsInt(double a){int b = a/1;if(a==b){return true;}else {return false;}}
bool coY() {cout <<"Yes"<<endl;}
bool coN(){cout <<"No"<<endl;}
const int mod = 1e9 + 7;
const ll INF = 1LL << 60;
//ここから書き始める

int main()
{
	cout <<setprecision(10);
	//例えば(N,M) (2,4)のとき、8文字にしても4文字にしても、被る（同じ要素番号）ところは変わらないので4のほうがいい。

	ll N , M ;
	cin >> N >> M;
	string S , T;
	cin >> S >> T; //S長さN, T長さM

	//最小公倍数
	ll G = __gcd(N,M);
	ll L = N*M / G;

	if(S==T){
		cout << L << endl;
		return 0 ;
	}
	map<ll,char> for_S;
	map<ll,char> for_T;

	rep(i,N){
		if(i==0){
			for_S[i] = S[i];
		}else if(i % (L/N) == 0){
			for_S[i] = S[i/(L/N)];
		}
	}
	rep(i,M){
		if(i==0){
			for_T[i] = T[i];

		}else if(i % ( L / M ) == 0){
			for_T[i] = T[i/(L/M)];
		}
	}
	vector<ll> ans(0);
	for(auto x:for_S){
		//x.firstについてfor_tに存在しているか
		//存在しているならば、一致しているか
		if(for_T.count(x.first)){
			if(for_T[x.first] == x.second){
				//一致してればおけ
			}else {
				cout << -1 << endl;
				return 0;
			}
		}
	}
	for(auto x:for_T){
		//x.firstについてfor_tに存在しているか
		//存在しているならば、一致しているか
		if(for_S.count(x.first)){
			if(for_S[x.first] == x.second){
				//一致してればおけ
			}else {
				cout << -1 << endl;
				return 0;
			}
		}
	}
	cout << L << endl;
	
}

//-1になるときの処理が足りてないらしい