import Control.Monad
import Data.Maybe
import qualified Data.ByteString.Char8 as BS
import Data.List

readInt = fst . fromJust . BS.readInt
readIntList = map readInt . BS.words
getInt = readInt <$> BS.getLine
getIntList = readIntList <$> BS.getLine

main = do
    [n,m] <- getIntList
    s <- getLine
    t <- getLine
    let l = lcm n m
        d = gcd n m
        si = n `div` d
        ti = m `div` d
        s' = [s !! (si*i) |i <- [0..d-1]]
        t' = [t !! (ti*i) |i <- [0..d-1]]
    print $ if s' == t' then l else -1