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

    val gcd = gcd(n,m)
    val lcm = 1L*n/gcd*m

    val a = s.slice((0..n-1) step n/gcd)
    val b = t.slice((0..m-1) step m/gcd)
    if(a == b) {
        println(lcm)
    } else {
        println(-1)
    }
}

fun gcd(a: Int, b: Int): Int {
    if (b == 0) return a
    return gcd(b, a % b)
}