main=do
    (n:m:_)<-map read.words<$>getLine::IO[Int]
    s<-getLine
    t<-getLine
    print $ solve n m s t
--
solve n m s t = if chk ss tt then l else (-1)
    where
        l = lcm n m
        ss = zip [1,1+l`div`n..] s
        tt = zip [1,1+l`div`m..] t
--
chk (s:ss) [] = True
chk [] (t:tt) = True
chk aa@((a,sa):ss) bb@((b,tb):tt)
    | a < b = chk ss bb
    | a > b = chk aa tt
    | sa == tb = chk ss tt
    | otherwise = False