object Main{
  def main(args:Array[String]):Unit={
    var MN = readLine
    var S = readLine
    var T = readLine
    var res = new calculate(S,T)
   
    println(res.calculate())

  }
}

import scala.collection.mutable.ArrayBuffer

class calculate(s:String,t:String){
  var lens = s.length
  var lent = t.length

   def calculate():Int={
    var lcm = getLCM()
    val g = getGCD()
    var flag = true
    for(i <- 0 until g if flag){
      if(!s.charAt(lens/g*i).toString.equals(t.charAt(lent/g*i).toString)){
        flag = false
        lcm = -1
      }
    }

    lcm
  }



  def getLCM():Int={
    return lens*lent/getGCD
  }

  def getGCD():Int={
    var tempN = lens
    var tempM = lent
    var c = lens % lent
    while(c!=0){
      tempN = tempM
      tempM = c
      c = tempN % tempM
    }
    return tempM
  }
}