import java.util.*
import kotlin.math.acos
import kotlin.math.cos
import kotlin.math.round
import kotlin.math.sin

fun main(args: Array<String>) {
    val (n, m) = readLongList()
    val s = readString()
    val t = readString()

    val l = lcm(n, m)
    val tmp = mutableMapOf<Long, Char>()
    s.forEachIndexed { index, c ->
        tmp[index * l / n] = c
    }

    t.forEachIndexed { index, c ->
        if(tmp[index * l / m]?.let { it != c } == true) {
            println(-1)
            return
        }
    }
    println(l)
}

// read
fun readString() = readLine()!!
fun readInt() = readLine()!!.toInt()
fun readLong() = readLine()!!.toLong()
fun readStringList() = readLine()!!.split(" ")
fun readIntList() = readLine()!!.split(" ").map(String::toInt)
fun readLongList() = readLine()!!.split(" ").map(String::toLong)

// 最大公約数
fun gcd(p: Long, q: Long): Long = if (p % q == 0L) q else gcd(q, p % q)

// 最小公倍数
fun lcm(p: Long, q: Long): Long = p / gcd(p, q) * q