function toInt (d) {
  return parseInt(d)
}

function toFloat (f) {
  return parseFloat(f)
}

function Main (input) {
  const data = input.split("\n")
  const fields = data[0].split(' ').map(toInt)
  const N = fields[0]
  const M = fields[1]
  const S = data[1]
  const T = data[2]

  var step = N
  if (N > M) {
    step = M
  }

  for (var i = step; i <= N * M; i += step) {
    if (i % N === 0 && i % M === 0) {
      var X = []
      var f = false

      for (var j = 0; j < N; j++) {
        X[j * i / N] = S.charAt(j)
      }

      for (var j = 0; j < M; j++) {
        var ts = T.charAt(j)

        if (X[j * i / M]) {
          if (ts !== X[j * i / M]) {
            f = true
          }
        } else {
          X[j * i / M] = T.charAt(j)
        }
      }

      if (f) {
        continue;
      }

      console.log(i)
      return
    }
  }

  console.log(-1);
}

Main(require("fs").readFileSync("/dev/stdin", "utf8"))