#include <cstdio>
#define maxn 100010
char s[maxn], t[maxn];
int gcd(int a, int b) {
	return b ? gcd(b, a % b) : a;
}
int main() {
	int n, m;
	scanf("%d%d%s%s", &n, &m, s, t);
	int x, y, g;
	long long l;
	g = gcd(n, m);
	l = (long long) n * m / g;
	x = n / g, y = m / g;
	bool valid = true;
	for(int i = 0; i < g; ++i) if(s[i * x] != t[i * y]) valid = false;
	printf("%lld", valid ? l : -1);
}