import Data.Bits
import Data.List
import Data.IntMap.Strict (IntMap)
import qualified Data.IntMap.Strict as IntMap
import qualified Data.ByteString.Char8 as B
import Control.Monad
import Control.Applicative
import Data.Maybe
import qualified Data.IntMap.Strict as M
import qualified Data.ByteString.Char8 as B
import qualified Data.Vector.Unboxed as V
-- import Data.Vector.Unboxed ((!))
import Data.Tuple
import Data.Ord (Down(..))
import Data.Coerce

query :: B.ByteString -> B.ByteString -> (Int, Bool)
query xs ys = (l, all (\i -> B.index xs (ndg * i) == B.index ys (mdg * i)) [0..g-1])
  where
    n = B.length xs
    m = B.length ys
    g = gcd n m
    l = lcm n m
    ndg = n `div` g
    mdg = m `div` g

main :: IO ()
main = do
  getLine
  xs <- B.getLine
  ys <- B.getLine
  let (i,b) = query xs ys
  putStrLn $ show $ if b then i else -1