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

def gcm(a,b)
  (a*b).times do |i|
    return i+1 if (i+1)%a == 0 && (i+1)%b == 0
  end
end

def lcm(a,b)
  a.times do |i|
    return a-i if a%(a-i) == 0 && b%(a-i) == 0
  end
end

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