import std.stdio;
import std.algorithm;
import std.conv;
import std.numeric;
import std.math;
import std.string;

void main()
{
	auto tokens = split(chomp(readln()));
	auto N = to!uint(tokens[0]);
	auto M = to!uint(tokens[1]);
	string S = chomp(readln());
	string T = chomp(readln());

	ulong lcm = (N * M) / gcd(N, M);
	if (S[0] != T[0]) writeln("-1");
	else
	{
		uint nr = cast(uint)(lcm / N);
		uint mr = cast(uint)(lcm / M);
		bool flag = true;
		foreach (i; 1..N)
		{
			uint p1= i * nr;
			if (p1 % mr != 0) continue;
			foreach (j; 1..M)
			{
				uint p2 = j * mr;
				if (p1 == p2)
				{
					if (S[i] != T[j])
					{
						flag = false;
						break;
					}
				}
			}
		}
		uint r = nr > mr ? nr * N : mr * M;
		if (flag) writeln(r);
		else writeln("-1");
	}

	stdout.flush();
}