var toInt = x=>parseInt(x,10);
var toIntArr = arr=>arr.map(x=>toInt(x));
var abs = x=>x>0?x:-x;
function min(){var rest=arguments,v=rest[0];for(var i=1;i<rest.length;i++)if(rest[i]<v)v=rest[i];return v}
function max(){var rest=arguments,v=rest[0];for(var i=1;i<rest.length;i++)if(rest[i]>v)v=rest[i];return v}
function gcd(){var rest=arguments,v=rest[0],temp=(a,b)=>b?temp(b,a%b):a;for(var i=1;i<rest.length;i++)v=temp(v,rest[i]);return v}
Main(require("fs").readFileSync("/dev/stdin", "utf8"));
 
function Main(input){
	var input = input.split('\n');
	var nm = toIntArr(input[0].split(' '));
	var n = nm[0];
	var m = nm[1];
	var s = input[1];
	var t = input[2];

	var g = gcd(n,m);
	var n1 = n/g;
	var m1 = m/g;

	var k = n1 * m;
	var l = n1 * m1;

	for(var i = 0; i < g; i++){
		if(s[n1*i] !== t[m1*i]){
			console.log(-1);
			return 0;
		}
	}

	var ans;

	console.log(k);

	return 0;
}

