#include<cstdio>
#define N 100020
#define ll long long
char S[N],T[N];
int gcd(int x,int y) {return y?gcd(y,x%y):x;}
int main()
{
	int n,m;ll ans;
	scanf("%d%d%s%s",&n,&m,S+1,T+1);
	ans=1ll*n/gcd(n,m)*m;
//	if (S[1]!=T[1]) return !printf("-1");
	for (ll i=0;i<n;++i)
		if (i*m%n==0&&S[i+1]!=T[i*m/n+1]) return !printf("-1");
	printf("%lld",ans);
	return 0;
}