#include <bits/stdc++.h>

using namespace std;

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

int main() {
	long long n, m;
	string s, t;
	cin >> n >> m >> s >> t;

	long long g = gcd(n, m);
	for (long long i = 0, j = 0;i < n && j < m;i += n / g, j += m / g)
		if (s[i] != t[j]) {
			cout << -1;

			return 0;
		}

	cout << n * m / g;

	return 0;
}