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: Accepted)

//ll lcm(ll a, ll b){
//	ll g = gcd(a,b);
//	return a/g*b;
//}
//void no(){
//	cout << "-1\n";
//	exit(0);
//}
//void _(){
//	ll n,m;
//	cin >> n >> m;
//	string s,t;
//	cin >> s >> t;
//	ll k = lcm(n,m);
//	ll j1 = k/m;
//	ll j2 = k/n;
//	for(ll i = 0; i < n; ++i){
//		if(i%j1 == 0){
//			if(t[i/j1*j2] != s[i])
//				no();
//		}
//	}
//	print(k);
//}
//
//
// ================================================ 
// ^^^	ABOVE is what I wrote in my editor	^^^
// ------------------------------------------------ 
// vvv	BELOW is automatically generated 	vvv
// ================================================ 
#include <numeric>
#include <set>
#include <iomanip>
#include <cassert>
#include <vector>
#include <iostream>
#include <string>
#define all(v) (v).begin(),(v).end()
#define sz(v) int(v.size())
using namespace std;
typedef long long ll;
ll gcd(ll a, ll b){
    return b==0?a:gcd(b,a%b);
}
template<typename IT>
ostream &print(IT b, IT e, string sep = " ", ostream &stream = cout){
    for(IT it = b; it != e; ++it){
        stream << *it;
        if(it+1 != e) stream << sep;
    } 
    return stream;
}
template<typename IT> istream &read(IT b, IT e, istream &stream = cin){
    for(IT it = b; it != e; ++it)
        stream >> *it; 
    return stream;
}
template<typename T> ostream &operator<<(ostream &stream, vector<T> &t){
    return print(all(t)," ",stream);
}
template<typename T> ostream &operator<<(ostream &stream, vector<T> &&t){
    return print(all(t)," ",stream);
}
template<typename T> istream &operator>>(istream &stream, vector<T> &t){
    return read(all(t),stream);
}
template <typename T> 
void print(T x, string end = "\n"){
    cout << x << end;
}
template <typename t> 
void print(vector<t> arr,string sep=" "){
    if(arr.empty()){
        return;
    }
    print(arr.begin(),arr.end(),sep);
    cout << '\n';
}
template <typename T> 
void print(vector<vector<T> > arr){
    for(int i = 0; i < sz(arr); ++i){
        cout << "[" << arr[i] << "]";
        if(i+1 < sz(arr))
            cout << ", ";
    }
    cout << '\n';
}
template<typename T>
T _();
const ll MOD = 1e9+7;
template<typename T>
vector<T> operator+(const vector<T> &lhs, const vector<T> &rhs){
    vector<T> ans(lhs);
    assert(ans.size() == rhs.size());
    for(int i = 0; i < rhs.size(); ++i)
        ans[i] += rhs[i];
    return ans;
}
template<typename T>
vector<T> operator+=(vector<T> &lhs, const vector<T> &rhs){
    return lhs = lhs+rhs;
    //vector<T> ans(lhs);
    //assert(ans.size() == rhs.size());
    //for(int i = 0; i < rhs.size(); ++i)
    //ans[i] += rhs[i];
    //return ans;
}
template<typename T>
vector<T> operator-(const vector<T> lhs, const vector<T> rhs){
    vector<T> ans(all(lhs));
    assert(ans.size() == rhs.size());
    for(int i = 0; i < rhs.size(); ++i)
        ans[i] -= rhs[i];
    return ans;
}
template<typename T>
vector<T> operator-=(vector<T> &lhs, const vector<T> &rhs){
    return lhs = lhs-rhs;
    //vector<T> ans(lhs);
    //assert(ans.size() == rhs.size());
    //for(int i = 0; i < rhs.size(); ++i)
    //ans[i] += rhs[i];
    //return ans;
}
template<typename T>
vector<T> append(vector<T> &lhs, const vector<T> rhs){
    lhs.insert(lhs.end(),all(rhs));
    return lhs;
    //vector<T> ans(lhs);
    //assert(ans.size() == rhs.size());
    //for(int i = 0; i < rhs.size(); ++i)
    //ans[i] += rhs[i];
    //return ans;
}
template<typename T>
T max(const vector<T> arr){
    assert(!arr.empty());
    T ans = arr[0];
    for(auto &cur : arr)
        ans = max(ans,cur);
    return ans;
}
template<typename T>
T max(const set<T> s){
    assert(!s.empty());
    return *--s.end();
}
template<typename T, typename G>
vector<T> set_max(vector<T> &base, const vector<G> &comp){
    assert(base.size() == comp.size());
    for(int i = 0; i < (int)comp.size(); ++i)
        base[i] = max(base[i],comp[i]);
    return base;
}
template<typename T>
ll gcd(vector<T> arr){
    if(arr.empty())
        return 0;
    ll ans = arr[0];
    for(int i = 1; i < sz(arr); ++i)
        ans = gcd(ans,arr[i]);
    return ans;
}
ll lcm(ll a, ll b){
	ll g = gcd(a,b);
	return a/g*b;
}
void no(){
	cout << "-1\n";
	exit(0);
}
void _(){
	ll n,m;
	cin >> n >> m;
	string s,t;
	cin >> s >> t;
	ll k = lcm(n,m);
	ll j1 = k/m;
	ll j2 = k/n;
	for(ll i = 0; i < n; ++i){
		if(i%j1 == 0){
			if(t[i/j1*j2] != s[i])
				no();
		}
	}
	print(k);
}

int main(){
    ios_base::sync_with_stdio(false); cin.tie(0);
    cout << fixed << setprecision(15);
    _();
}