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 = readLine
  val t = readLine
  val xl = lcm(sl, tl)
  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
  println(if (sChars.forall{case (i, sc) => tChars.get(i).fold(true){tc => sc == tc}}) xl else -1)
}