import scala.io.StdIn._
object Main extends App {
  def gcd(a: Int, b: Int): Int = if (b == 0) a else gcd(b, a % b)
  def lcm(a: Int, b: Int): Int = a * b / gcd(a, b)

  val Array(sl, tl) = readLine.split(" ").map{_.toInt}
  val s, t = readLine
  val lcmValue= lcm(sl, tl)

  def judge(xl: Int): Boolean = {
    val sChars = (0 until sl).map{i => (xl / sl * i + 1, s.charAt(i))}
    val tChars = (0 until tl).map{i => (xl / tl * i + 1, t.charAt(i))}.toMap
    sChars.forall{case (i, sc) => tChars.get(i).fold(true){tc => sc == tc}}
  }

  val result: Int =
    if (lcmValue % sl == 0 || lcmValue % tl == 0) {
      if (judge(lcmValue)) lcmValue
      else -1
    } else (lcmValue to (sl * tl) by lcmValue).find(judge).getOrElse(-1)
  println(result)
}