#include <iostream>
using namespace std;
string s1, s2;
int gcd(int x, int y){
	if (y == 0)
		return x;
	return gcd(y, x % y);
}
int main(){
	int n, m;
	cin >> n >> m;
	cin >> s1 >> s2;
	int x = gcd(n, m);
	for (int i = 1; i <= x; ++ i)
		if (s1[(i - 1) * (n / x)] != s2[(i - 1) * (m / x)]){
			cout << -1 << endl;
			return 0;
		}
	cout << 1LL * n * m / x << endl;
}
