def gcd(a, b)
  a, b = b, a if a > b
  until a == 0
    a, b = b % a, a
  end
  return b
end

def lcm(a, b)
  a*b/gcd(a, b)
end

N, M = gets.split.map(&:to_i)
S = gets.chomp
T = gets.chomp

aryS = S.chars
aryT = T.chars

if aryS.size >= aryT.size
  a = aryS
  b = aryT
else
  a = aryT
  b = aryS
end

if N % M == 0 || M % N == 0
  times = a.size / b.size
  i = 0
  is_exist = true
  b.size.times do |j|
    if(a[i] != b[j])
      is_exist = false
    end
    i += times
  end
  if is_exist
    puts a.size
  else
    puts(-1)
  end
else
  if gcd(N, M) == 1
    if a[0] == b[0]
      puts lcm(N, M)
    else
      puts(-1)
    end
  else
    n = a.size
    m = b.size
    l = lcm(n, m)
    g = gcd(n, m)
    times = l / g
    i = 1
    gap_a = n / g
    gap_b = m / g
    is_exist = true
    while i < l
      a_i = (i - 1) / gap_b
      b_i = (i - 1) / gap_a
      if a[a_i] != b[b_i]
        is_exist = false
      end
      i += times
    end
    if is_exist
      puts l
    else
      puts -1
    end
  end
end
