fun gcd(x: Int, y: Int): Int {
  if (x < y) {
    return gcd(y, x)
  }
  val r = x % y
  if (r == 0) {
    return y
  } else {
    return gcd(y, r)
  }
}

fun main(args: Array<String>) {
  val (n, m) = readLine()!!.split(" ").map { it.toInt() }
  val s = readLine()!!
  val t = readLine()!!
  val r = gcd(n, m)
  for (k in 0 until r) {
    if (s[k * (n / r)] != t[k * (m / r)]) {
      println(-1)
      return
    }
  }
  println(n.toLong() * m.toLong() / r.toLong())
}
