#include<bits/stdc++.h>
using namespace std;
string s1,s2;
typedef long long ll;
int main()
{
	ll N,M;
	cin>>N>>M>>s1>>s2;
	ll g=__gcd(N,M);
	ll L = N*M/g;
	bool flag=true;
	for(ll i=0,j=0;i<N&&j<M;i+=N/g,j+=M/g)
	{
		if(s1[i]!=s2[j])	flag=false;
	}
	if(flag)
		cout<<L<<endl;
	else cout<<"-1"<<endl;
	return 0;
}