#include<bits/stdc++.h>
using namespace std;
#define MN 100000
char a[MN+5],b[MN+5];
int gcd(int x,int y){return y?gcd(y,x%y):x;}
int main()
{
	int n,m,i,g;long long ans;
	scanf("%d%d%s%s",&n,&m,a,b);
	g=gcd(n,m);ans=1LL*n*m/g;
	for(i=0;i<g;++i)if(a[i*n/g]!=b[i*m/g])break;
	printf("%lld",i<g?-1:ans);
}
