#include <bits/stdc++.h>

using namespace std;

int gcd(int a, int b) {
	for (; b; swap(a, b))
		a %= b;
	return a;
}

int main() {
	int n, m;
	string s, t;
	cin >> n >> m >> s >> t;
	int g = gcd(n, m);
	for (int i = 0; i < g; i++) {
		if (s[n / g * i] != t[m / g * i]) {
			cout << -1 << endl;
			return 0;
		}
	}
	cout << long(n) / g * m << endl;
	return 0;
}
