import Control.Applicative

f [n, m] = judge 
  where
    sstep = div n $ gcd n m
    tstep = div m $ gcd n m
    judge [] [] = lcm n m
    judge s' t' = if head s' == head t' then judge (drop sstep s') (drop tstep t') else (-1)

main = f <$> (map read . words <$> getLine) <*> getLine <*> getLine >>= print