object Main{
  def main(args:Array[String]):Unit={
    var S = "dnsusrayukuaiia"
    var T = "dujrunuma"
    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()
    var res = lcm
    var record:ArrayBuffer[String] = new ArrayBuffer()
    for(i <- 0 to lcm) record = record:+0.toString
    for(i <- 0 until lens){
      var temp = i*(lcm/lens)+1
      record(temp)=s.charAt(i).toString
    }
    var flag = true
    for(i <- 0 until lent if flag){
      var temp = i*(lcm/lent)+1
      if(!record(temp).equals("0") && !record(temp).equals(t.charAt(i).toString)){
        flag = false
        res = -1
      }
    }
    
    res
  }



  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
  }
}