object Main {
  import java.util.Scanner
  import scala.annotation.tailrec

  def readStrings(n: Int)(implicit sc: Scanner): Array[String] = Array.fill(n) { sc.next() }
  def readInts(n: Int)(implicit sc: Scanner): Array[Int] = Array.fill(n) { sc.nextInt() }
  def readLongs(n: Int)(implicit sc: Scanner): Array[Long] = Array.fill(n) { sc.nextLong() }
  def readLineStrings(implicit sc: Scanner): Array[String] = sc.nextLine().split(' ')
  def readLineInts(implicit sc: Scanner): Array[Int] = readLineStrings.map(_.toInt)
  def readLineLongs(implicit sc: Scanner): Array[Long] = readLineStrings.map(_.toLong)

  def main(args: Array[String]): Unit = solve(new Scanner(System.in))

  def solve(implicit sc: Scanner): Unit = {
    @tailrec def gcd(a: Long, b: Long): Long = if (b == 0) a else gcd(b, a % b)

    val Array(n, m) = readLineLongs
    val S = sc.nextLine()
    val T = sc.nextLine()

    val gcdLength = gcd(n, m)
    val lcm = m * n / gcdLength
    val nStep = (n / gcdLength).toInt
    val mStep = (m / gcdLength).toInt
    val compareIndices = (0 until gcdLength.toInt).map { i => (i * nStep, i * mStep) }
    if (compareIndices.forall { case (x, y) => S.charAt(x) == T.charAt(y) }) {
      println(lcm)
    } else {
      println(-1)
    }
  }
}