import java.util.Scanner
import scala.util.control.Breaks

object MathAlgo {

  def gcd(a: Long, b: Long): Long = {
    if (b == 0) a
    else gcd(b, a % b)
  }

  def lcm(a: Long, b: Long): Long = {
    a / gcd(a, b) * b
  }
}

object Main {
  def main(args: Array[String]): Unit = {
    val sc = new Scanner(System.in)
    val N, M = sc.nextInt
    val S, T = sc.next

    val base = MathAlgo.gcd(N, M).toInt
    var ans = N / base * M
    val b = new Breaks
    b.breakable {
      for (i <- 0 until base) {
        if (S(S.length / base * i)
          != T(T.length / base * i)) {
          ans = -1
          b.break
        }
      }
    }

    println(ans)
  }
}