#include <bits/stdc++.h>
using namespace std;

typedef long long ll;

char S[1<<17], T[1<<17];

ll N, M;

ll gcd(ll a, ll b) { return b ? gcd(b, a%b) : a; }

int main() {
	scanf("%lld%lld", &N, &M);
	scanf("%s%s", S, T);

	ll a = gcd(M, N);
	ll L = N * M / a;

	for (int i = 0; i < a; ++i) if (S[N/a * i] != T[M/a * i]) return printf("-1\n"), 0;
	
	printf("%lld\n", L);
}