import kotlin.system.exitProcess

fun main(args: Array<String>) {
	val (n, m) = readLine()!!.split(' ').map { it.toLong() }
	val s = readLine()!!
	val t = readLine()!!

	val l = lcm(n, m)
	val dic = mutableMapOf<Long, Char>()

	for ((i, c) in s.withIndex()) {
		val j = l / n * i
		dic[j] = c
	}

	for ((i, c) in t.withIndex()) {
		val j = l / m * i
		if (dic[j] != null && dic[j] != c) {
			println(-1)
			exitProcess(0)
		}
	}
	println(l)
}

fun gcd(a: Long, b: Long): Long {
	return when {
		a < b -> {
			gcd(b, a)
		}
		b == 0L -> {
			a
		}
		else -> {
			gcd(b, a % b)
		}
	}
}

fun lcm(a: Long, b: Long): Long {
	return (a * b) / gcd(a, b)
}
