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: Runtime Error)

#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のほうがいい。

	int N , M ;
	cin >> N >> M;
	string S , T;
	cin >> S >> T; //S長さN, T長さM
	//最小公倍数
	ll L = N*M /__gcd(N,M);

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

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

		}else if(i % ( L / M ) == 0){
			for_T[i] = T[i/(L/M)];
		}
	}
	vector<int> ans(0);
	for(auto x:for_S){
		ans.push_back(x.first);
	}
	for(auto x:ans){
		if(!(for_T.count(x))){
			//そのキーに対応するのがなければok
		}else{
			//対応するのがあれば、一致してればおけ
			if(for_T[x] == for_S[x]){
				//ok
			}else{
				cout << -1 << endl;
				return 0;
			}
		}
	}
	cout << L << endl;
	
}