object Main extends App {
  import scala.io.StdIn._


  val Array(n, m) = readLine.trim.split(' ').map(_.toInt)
  val s = readLine.trim
  val t = readLine.trim
  val g = gcd(n, m)
  val (a, b) = (n / g, m / g)
  println(
    if ((0 until g).forall(y ⇒ s(y * a) == t(y * b))){
      a * b * g
    }else {
      -1
    }
  )
  def gcd(a:Int, b:Int):Int = {
    b match {
      case 0 ⇒ a
      case _ ⇒ gcd(b, a % b)
    }
  }
  implicit class ScopeFunc[T](val value: T){
    def let[A](func: T ⇒ A): A = func(value)
    def also(func: T ⇒ Unit): T = {
      func(value)
      value
    }
  }
}