fun main(args: Array<String>) {
    agc028a()
}

fun agc028a() {
    val (n, m) = readLine()!!.split(" ").map { it.toInt() }
    val s = readLine()!!
    val t = readLine()!!

    val gcd = gcd(n.toLong(), m.toLong())
    val lcm = n / gcd * m

    val all = (0 until gcd).all { s[(it * n / gcd).toInt()] == t[(it * m / gcd).toInt()] }
    val answer = if (all) lcm else -1

    println(answer)
}

private tailrec fun gcd(a: Long, b: Long): Long {
    return if (b == 0L) a else gcd(b, a % b)
}
