import java.util.*

fun main(args: Array<String>) {
  val sc = Scanner(System.`in`)
  val n = sc.nextLong()
  val m = sc.nextLong()
  val s = sc.next()
  val t = sc.next()

  if (s[0] != t[0]) {
    println(-1)
    return
  }

  val nn = n / gcd(n, m).toInt()
  val mm = m / gcd(n, m).toInt()
  var ok = true
  for (i in 0 until gcd(n, m).toInt()) {
    val ni = (i * nn).toInt()
    val mi = (i * mm).toInt()
    val si = s[ni]
    val ti = t[mi]
    if (si != ti) {
      ok = false
      break
    }
  }

  if (ok) {
    val lcm = lcm(n, m)
    println(lcm)
    return
  }
  println(-1)

}

// 最大公約数
private fun gcd(a: Long, b: Long): Long {
  if (b > 0) {
    return gcd(b, a % b)
  }
  return a
}

// 最小公倍数
private fun lcm(a: Long, b: Long): Long {
    return a / gcd(a, b) * b
}