def two_abbreviations(arr,s,t)
  len = arr[0].lcm(arr[1])
  arr[0].gcd(arr[1]).times do |i|
    return -1 if t[i * len / arr[0]] != s[i * len / arr[1]]
  end
  return len
end

if __FILE__ == $0
  arr = gets.chomp!.split(' ').map(&:to_i)
  s = gets.chomp!
  t = gets.chomp!
  print two_abbreviations(arr,s,t)
end